Changeset View
Changeset View
Standalone View
Standalone View
lib/shared/avatar-utils.js
// @flow | // @flow | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
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 { threadInfoSelector } from '../selectors/thread-selectors.js'; | |||||
import type { ClientEmojiAvatar, ClientAvatar } from '../types/avatar-types.js'; | import type { ClientEmojiAvatar, ClientAvatar } 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 } from '../types/user-types.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', | ||||
}; | }; | ||||
const defaultEmojiAvatars: $ReadOnlyArray<ClientEmojiAvatar> = [ | const defaultEmojiAvatars: $ReadOnlyArray<ClientEmojiAvatar> = [ | ||||
Show All 37 Lines | ): ClientAvatar { | ||||
if (!user?.username) { | if (!user?.username) { | ||||
return defaultAnonymousUserEmojiAvatar; | return defaultAnonymousUserEmojiAvatar; | ||||
} | } | ||||
return getDefaultAvatar(user.username); | return getDefaultAvatar(user.username); | ||||
} | } | ||||
function useGetAvatarForThread( | |||||
thread: RawThreadInfo | ThreadInfo, | |||||
): ClientAvatar { | |||||
const containingThreadID = thread?.containingThreadID; | |||||
const containingThreadInfo = useSelector(state => | |||||
containingThreadID ? threadInfoSelector(state)[containingThreadID] : null, | |||||
); | |||||
if (thread.avatar) { | |||||
return thread.avatar; | |||||
} | |||||
if (thread.containingThreadID && thread.containingThreadID !== genesis.id) { | |||||
invariant(containingThreadInfo, 'containingThreadInfo should be set'); | |||||
return containingThreadInfo.avatar | |||||
? containingThreadInfo.avatar | |||||
: getDefaultAvatar(containingThreadInfo.id, containingThreadInfo.color); | |||||
} | |||||
return getDefaultAvatar(thread.id, thread.color); | |||||
} | |||||
function getUserAvatarForThread( | function getUserAvatarForThread( | ||||
threadInfo: RawThreadInfo | ThreadInfo, | threadInfo: RawThreadInfo | ThreadInfo, | ||||
viewerID: ?string, | viewerID: ?string, | ||||
userInfos: UserInfos, | userInfos: UserInfos, | ||||
): ClientAvatar { | ): ClientAvatar { | ||||
if (threadInfo.type === threadTypes.PRIVATE) { | if (threadInfo.type === threadTypes.PRIVATE) { | ||||
invariant(viewerID, 'viewerID should be set for PRIVATE threads'); | invariant(viewerID, 'viewerID should be set for PRIVATE threads'); | ||||
return getAvatarForUser(userInfos[viewerID]); | return getAvatarForUser(userInfos[viewerID]); | ||||
} | } | ||||
const memberInfos = threadOtherMembers(threadInfo.members, viewerID) | const memberInfos = threadOtherMembers(threadInfo.members, viewerID) | ||||
.map(member => userInfos[member.id] && userInfos[member.id]) | .map(member => userInfos[member.id] && userInfos[member.id]) | ||||
.filter(Boolean); | .filter(Boolean); | ||||
if (memberInfos.length === 0) { | if (memberInfos.length === 0) { | ||||
return defaultAnonymousUserEmojiAvatar; | return defaultAnonymousUserEmojiAvatar; | ||||
} | } | ||||
return getAvatarForUser(memberInfos[0]); | return getAvatarForUser(memberInfos[0]); | ||||
} | } | ||||
export { getAvatarForUser, getUserAvatarForThread }; | export { getAvatarForUser, useGetAvatarForThread, getUserAvatarForThread }; |