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,8 +4,8 @@ import * as React from 'react'; import { ENSCacheContext } from '../components/ens-cache-provider.react.js'; -import { userIdentifiedByETHAddress } from '../shared/account-utils.js'; import { stringForUser } from '../shared/user-utils.js'; +import { getEthAddressForUserInfo } from '../utils/ens-helpers.js'; type BaseUserInfo = { +username?: ?string, ... }; function useENSNames(users: $ReadOnlyArray): T[] { @@ -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/avatar-utils.js b/lib/shared/avatar-utils.js --- a/lib/shared/avatar-utils.js +++ b/lib/shared/avatar-utils.js @@ -4,7 +4,6 @@ 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'; @@ -21,6 +20,7 @@ threadTypes, } from '../types/thread-types.js'; import type { UserInfos, UserInfo } from '../types/user-types.js'; +import { getEthAddressForUserInfo } from '../utils/ens-helpers.js'; import { useSelector } from '../utils/redux-utils.js'; const defaultAnonymousUserEmojiAvatar: ClientEmojiAvatar = { @@ -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 @@ -8,6 +8,17 @@ users: $ReadOnlyArray, ) => Promise; +function getEthAddressForUserInfo(userInfo: ?BaseUserInfo): ?string { + if (!userInfo) { + return null; + } + const { username } = userInfo; + const ethAddress = + username && userIdentifiedByETHAddress(userInfo) ? username : null; + + return ethAddress; +} + async function getENSNames( ensCache: ENSCache, users: $ReadOnlyArray, @@ -16,9 +27,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; @@ -72,4 +81,4 @@ }); } -export { getENSNames }; +export { getEthAddressForUserInfo, getENSNames };