Skip to content

Commit

Permalink
DM3 name fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhupesh-mfsi committed Nov 23, 2023
1 parent a64ec09 commit 1818656
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,11 @@ export function ConfigureProfile() {
useEffect(() => {
if (
state.connection.account?.ensName &&
state.connection.account?.ensName.endsWith(
globalConfig.USER_ENS_SUBDOMAIN(),
!state.connection.account?.ensName.endsWith(
globalConfig.ADDR_ENS_SUBDOMAIN(),
)
) {
setExistingDm3Name(state.connection.account.ensName);
fetchExistingDM3Name(setExistingDm3Name);
fetchExistingDM3Name(state, setExistingDm3Name);
}
}, [state.connection.account?.ensName]);

Expand Down
23 changes: 19 additions & 4 deletions packages/messenger-widget/src/components/ConfigureProfile/bl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import {
ModalStateType,
} from '../../utils/enum-type-utils';
import { claimSubdomain, removeAlias } from 'dm3-lib-offchain-resolver-api';
import { createAlias } from 'dm3-lib-delivery-api';
import { createAlias, getAliasChain } from 'dm3-lib-delivery-api';
import { Connection } from '../../interfaces/web3';
import { globalConfig, ethersHelper, stringify } from 'dm3-lib-shared';
import { ethers } from 'ethers';
import { closeLoader, startLoader } from '../Loader/Loader';
import { setContactHeightToMaximum } from '../Contacts/bl';
import { checkEnsDM3Text } from '../../utils/ens-utils';
import { getLastDm3Name } from '../../utils/common-utils';

export const PROFILE_INPUT_FIELD_CLASS =
'profile-input font-weight-400 font-size-14 border-radius-6 w-100 line-height-24';
Expand Down Expand Up @@ -347,10 +348,24 @@ export const validateEnsName = (username: string): boolean => {
return ethers.utils.isValidName(username);
};

export const fetchExistingDM3Name = (setExistingDm3Name: Function) => {
export const fetchExistingDM3Name = async (
state: GlobalState,
setExistingDm3Name: Function,
) => {
try {
const dm3Name = '';
setExistingDm3Name(dm3Name ? dm3Name : null);
if (state.connection.account && state.connection.provider) {
const dm3Names: any = await getAliasChain(
state.connection.account,
state.connection.provider,
);
let dm3Name;
if (dm3Names && dm3Names.length) {
dm3Name = getLastDm3Name(dm3Names);
}
setExistingDm3Name(dm3Name ? dm3Name : null);
} else {
setExistingDm3Name(null);
}
} catch (error) {
setExistingDm3Name(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { globalConfig } from 'dm3-lib-shared';
import { hasUserProfile } from 'dm3-lib-profile';
import { HideFunctionProps } from '../../interfaces/props';
import menuIcon from '../../assets/images/menu.svg';
import { getAliasChain } from 'dm3-lib-delivery-api';
import { getLastDm3Name } from '../../utils/common-utils';

export function RightHeader(props: HideFunctionProps) {
// fetches context storage
Expand Down Expand Up @@ -85,7 +87,14 @@ export function RightHeader(props: HideFunctionProps) {
: state.connection.account?.ensName,
});
} else {
const dm3Name = '';
const dm3Names: any = await getAliasChain(
state.connection.account,
state.connection.provider,
);
let dm3Name;
if (dm3Names && dm3Names.length) {
dm3Name = getLastDm3Name(dm3Names);
}
dispatch({
type: CacheType.AccountName,
payload: dm3Name
Expand Down
10 changes: 9 additions & 1 deletion packages/messenger-widget/src/utils/common-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SendDependencies, Message } from 'dm3-lib-messaging';
import { Account, ProfileKeys } from 'dm3-lib-profile';
import { log } from 'dm3-lib-shared';
import { globalConfig, log } from 'dm3-lib-shared';
import { StorageEnvelopContainer } from 'dm3-lib-storage';
import { submitMessage } from '../adapters/messages';
import {
Expand Down Expand Up @@ -172,6 +172,14 @@ export const showContactList = (dispatch: React.Dispatch<Actions>) => {
});
};

export const getLastDm3Name = (nameList: string[]) => {
let index = -1;
index = nameList.findIndex((data) =>
data.endsWith(globalConfig.USER_ENS_SUBDOMAIN()),
);
return index > -1 ? nameList[index] : null;
};

// Constants
export const REACT_APP_SUPPORTED_CHAIN_ID = 5;

Expand Down

0 comments on commit 1818656

Please sign in to comment.