Changeset View
Changeset View
Standalone View
Standalone View
lib/shared/avatar-utils.js
// @flow | // @flow | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import * as React from 'react'; | |||||
import stringHash from 'string-hash'; | import stringHash from 'string-hash'; | ||||
import { userIdentifiedByETHAddress } from './account-utils.js'; | |||||
import { selectedThreadColors } from './color-utils.js'; | import { selectedThreadColors } from './color-utils.js'; | ||||
import { threadOtherMembers } from './thread-utils.js'; | import { threadOtherMembers } from './thread-utils.js'; | ||||
import genesis from '../facts/genesis.js'; | import genesis from '../facts/genesis.js'; | ||||
import { useENSAvatar } from '../hooks/ens-cache.js'; | |||||
import { threadInfoSelector } from '../selectors/thread-selectors.js'; | import { threadInfoSelector } from '../selectors/thread-selectors.js'; | ||||
import type { ClientEmojiAvatar, ClientAvatar } from '../types/avatar-types.js'; | import type { | ||||
ClientEmojiAvatar, | |||||
ClientAvatar, | |||||
ResolvedClientAvatar, | |||||
} from '../types/avatar-types.js'; | |||||
import { | import { | ||||
type RawThreadInfo, | type RawThreadInfo, | ||||
type ThreadInfo, | type ThreadInfo, | ||||
threadTypes, | threadTypes, | ||||
} from '../types/thread-types.js'; | } from '../types/thread-types.js'; | ||||
import type { UserInfos } from '../types/user-types.js'; | import type { UserInfos, UserInfo } from '../types/user-types.js'; | ||||
import { useSelector } from '../utils/redux-utils.js'; | import { useSelector } from '../utils/redux-utils.js'; | ||||
const defaultAnonymousUserEmojiAvatar: ClientEmojiAvatar = { | const defaultAnonymousUserEmojiAvatar: ClientEmojiAvatar = { | ||||
color: selectedThreadColors[4], | color: selectedThreadColors[4], | ||||
emoji: '👤', | emoji: '👤', | ||||
type: 'emoji', | type: 'emoji', | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 87 Lines • ▼ Show 20 Lines | if (thread.containingThreadID && thread.containingThreadID !== genesis.id) { | ||||
return containingThreadInfo.avatar | return containingThreadInfo.avatar | ||||
? containingThreadInfo.avatar | ? containingThreadInfo.avatar | ||||
: getDefaultAvatar(containingThreadInfo.id, containingThreadInfo.color); | : getDefaultAvatar(containingThreadInfo.id, containingThreadInfo.color); | ||||
} | } | ||||
return getDefaultAvatar(thread.id, thread.color); | return getDefaultAvatar(thread.id, thread.color); | ||||
} | } | ||||
function useENSResolvedAvatar( | |||||
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 ensAvatarURI = useENSAvatar(ethAddress); | |||||
const resolvedAvatar = React.useMemo(() => { | |||||
if (avatarInfo.type !== 'ens') { | |||||
return avatarInfo; | |||||
} | |||||
if (ensAvatarURI) { | |||||
return { | |||||
type: 'image', | |||||
uri: ensAvatarURI, | |||||
}; | |||||
} | |||||
return defaultAnonymousUserEmojiAvatar; | |||||
}, [ensAvatarURI, avatarInfo]); | |||||
return resolvedAvatar; | |||||
} | |||||
export { | export { | ||||
defaultAnonymousUserEmojiAvatar, | |||||
getAvatarForUser, | getAvatarForUser, | ||||
getUserAvatarForThread, | getUserAvatarForThread, | ||||
useGetAvatarForThread, | useGetAvatarForThread, | ||||
useENSResolvedAvatar, | |||||
}; | }; |