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 * as React from 'react'; | ||||
import stringHash from 'string-hash'; | import stringHash from 'string-hash'; | ||||
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 { useENSAvatar } from '../hooks/ens-cache.js'; | ||||
import { threadInfoSelector } from '../selectors/thread-selectors.js'; | import { threadInfoSelector } from '../selectors/thread-selectors.js'; | ||||
import { getETHAddressForUserInfo } from '../shared/account-utils.js'; | import { getETHAddressForUserInfo } from '../shared/account-utils.js'; | ||||
import type { | import type { | ||||
ClientEmojiAvatar, | ClientEmojiAvatar, | ||||
ClientAvatar, | ClientAvatar, | ||||
ResolvedClientAvatar, | ResolvedClientAvatar, | ||||
GenericUserInfoWithAvatar, | |||||
} from '../types/avatar-types.js'; | } from '../types/avatar-types.js'; | ||||
import { threadTypes } from '../types/thread-types-enum.js'; | import { threadTypes } from '../types/thread-types-enum.js'; | ||||
import { type RawThreadInfo, type ThreadInfo } from '../types/thread-types.js'; | import { type RawThreadInfo, type ThreadInfo } from '../types/thread-types.js'; | ||||
import type { UserInfos, UserInfo } from '../types/user-types.js'; | import type { UserInfos } 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 216 Lines • ▼ Show 20 Lines | function getDefaultAvatar(hashKey: string, color?: string): ClientEmojiAvatar { | ||||
const avatarIndex = stringHash(hashKey) % defaultEmojiAvatars.length; | const avatarIndex = stringHash(hashKey) % defaultEmojiAvatars.length; | ||||
return { | return { | ||||
...defaultEmojiAvatars[avatarIndex], | ...defaultEmojiAvatars[avatarIndex], | ||||
color: color ? color : defaultEmojiAvatars[avatarIndex].color, | color: color ? color : defaultEmojiAvatars[avatarIndex].color, | ||||
}; | }; | ||||
} | } | ||||
function getAvatarForUser( | function getAvatarForUser(user: ?GenericUserInfoWithAvatar): ClientAvatar { | ||||
user: ?{ +avatar?: ?ClientAvatar, +username?: ?string, ... }, | |||||
): ClientAvatar { | |||||
if (user?.avatar) { | if (user?.avatar) { | ||||
return user.avatar; | return user.avatar; | ||||
} | } | ||||
if (!user?.username) { | if (!user?.username) { | ||||
return defaultAnonymousUserEmojiAvatar; | return defaultAnonymousUserEmojiAvatar; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | const containingThreadInfo = useSelector(state => | ||||
containingThreadID ? threadInfoSelector(state)[containingThreadID] : null, | containingThreadID ? threadInfoSelector(state)[containingThreadID] : null, | ||||
); | ); | ||||
return getAvatarForThread(thread, containingThreadInfo); | return getAvatarForThread(thread, containingThreadInfo); | ||||
} | } | ||||
function useENSResolvedAvatar( | function useENSResolvedAvatar( | ||||
avatarInfo: ClientAvatar, | avatarInfo: ClientAvatar, | ||||
userInfo: ?UserInfo, | userInfo: ?GenericUserInfoWithAvatar, | ||||
): ResolvedClientAvatar { | ): ResolvedClientAvatar { | ||||
const ethAddress = React.useMemo( | const ethAddress = React.useMemo( | ||||
() => getETHAddressForUserInfo(userInfo), | () => getETHAddressForUserInfo(userInfo), | ||||
[userInfo], | [userInfo], | ||||
); | ); | ||||
const ensAvatarURI = useENSAvatar(ethAddress); | const ensAvatarURI = useENSAvatar(ethAddress); | ||||
Show All 27 Lines |