diff --git a/lib/hooks/ens-cache.js b/lib/hooks/ens-cache.js --- a/lib/hooks/ens-cache.js +++ b/lib/hooks/ens-cache.js @@ -4,7 +4,7 @@ import * as React from 'react'; import { ENSCacheContext } from '../components/ens-cache-provider.react.js'; -import { userIdentifiedByETHAddress } from '../shared/account-utils.js'; +import { getETHAddressForUserInfo } from '../shared/account-utils.js'; import { stringForUser } from '../shared/user-utils.js'; type BaseUserInfo = { +username?: ?string, ... }; @@ -18,9 +18,7 @@ if (!user) { return user; } - const { username } = user; - const ethAddress = - username && userIdentifiedByETHAddress(user) ? username : null; + const ethAddress = getETHAddressForUserInfo(user); const cachedResult = ethAddress && ensCache ? ensCache.getCachedNameForAddress(ethAddress) diff --git a/lib/shared/account-utils.js b/lib/shared/account-utils.js --- a/lib/shared/account-utils.js +++ b/lib/shared/account-utils.js @@ -89,6 +89,19 @@ : false; } +function getETHAddressForUserInfo( + userInfo: ?{ +username?: ?string, ... }, +): ?string { + if (!userInfo) { + return null; + } + const { username } = userInfo; + const ethAddress = + username && userIdentifiedByETHAddress(userInfo) ? username : null; + + return ethAddress; +} + export { usernameMaxLength, oldValidUsernameRegexString, @@ -100,4 +113,5 @@ validHexColorRegex, accountHasPassword, userIdentifiedByETHAddress, + getETHAddressForUserInfo, }; diff --git a/lib/shared/avatar-utils.js b/lib/shared/avatar-utils.js --- a/lib/shared/avatar-utils.js +++ b/lib/shared/avatar-utils.js @@ -4,12 +4,12 @@ import * as React from 'react'; import stringHash from 'string-hash'; -import { userIdentifiedByETHAddress } from './account-utils.js'; import { selectedThreadColors } from './color-utils.js'; import { threadOtherMembers } from './thread-utils.js'; import genesis from '../facts/genesis.js'; import { useENSAvatar } from '../hooks/ens-cache.js'; import { threadInfoSelector } from '../selectors/thread-selectors.js'; +import { getETHAddressForUserInfo } from '../shared/account-utils.js'; import type { ClientEmojiAvatar, ClientAvatar, @@ -136,15 +136,10 @@ avatarInfo: ClientAvatar, userInfo: ?UserInfo, ): ResolvedClientAvatar { - const ethAddress = React.useMemo(() => { - let address = null; - if (userInfo && avatarInfo.type === 'ens') { - const { username } = userInfo; - address = - username && userIdentifiedByETHAddress(userInfo) ? username : null; - } - return address; - }, [avatarInfo.type, userInfo]); + const ethAddress = React.useMemo( + () => getETHAddressForUserInfo(userInfo), + [userInfo], + ); const ensAvatarURI = useENSAvatar(ethAddress); diff --git a/lib/utils/ens-helpers.js b/lib/utils/ens-helpers.js --- a/lib/utils/ens-helpers.js +++ b/lib/utils/ens-helpers.js @@ -1,7 +1,7 @@ // @flow import { ENSCache } from './ens-cache.js'; -import { userIdentifiedByETHAddress } from '../shared/account-utils.js'; +import { getETHAddressForUserInfo } from '../shared/account-utils.js'; type BaseUserInfo = { +username?: ?string, ... }; export type GetENSNames = ( @@ -16,9 +16,7 @@ if (!user) { return user; } - const { username } = user; - const ethAddress = - username && userIdentifiedByETHAddress(user) ? username : null; + const ethAddress = getETHAddressForUserInfo(user); const cachedResult = ethAddress ? ensCache.getCachedNameForAddress(ethAddress) : null;