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 './thread-utils.js'; | import { selectedThreadColors } from './thread-utils.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 } from '../types/avatar-types.js'; | ||||
import { type ThreadType, threadTypes } from '../types/thread-types.js'; | import { type ThreadType, threadTypes } from '../types/thread-types.js'; | ||||
import { useResolvedOptionalThreadInfo } from '../utils/entity-helpers.js'; | import { useResolvedOptionalThreadInfo } from '../utils/entity-helpers.js'; | ||||
import type { EntityTextComponent } from '../utils/entity-text.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 All 38 Lines | ): ClientAvatar { | ||||
if (!user?.username) { | if (!user?.username) { | ||||
return defaultAnonymousUserEmojiAvatar; | return defaultAnonymousUserEmojiAvatar; | ||||
} | } | ||||
return getDefaultAvatar(user.username); | return getDefaultAvatar(user.username); | ||||
} | } | ||||
function getAvatarForThreadEntity( | |||||
threadEntity: EntityTextComponent, | |||||
): ?ClientAvatar { | |||||
if ( | |||||
threadEntity.type !== 'thread' || | |||||
typeof threadEntity === 'string' || | |||||
threadEntity.display !== 'uiName' | |||||
) { | |||||
return null; | |||||
} | |||||
const { uiName } = threadEntity; | |||||
if (typeof uiName === 'string') { | |||||
return null; | |||||
} | |||||
const userEntity = uiName.find(innerEntity => !innerEntity.isViewer); | |||||
return getAvatarForUser(userEntity); | |||||
} | |||||
function useGetAvatarForThread( | function useGetAvatarForThread( | ||||
thread: ?{ | thread: ?{ | ||||
+id: string, | +id: string, | ||||
+type: ThreadType, | +type: ThreadType, | ||||
+avatar?: ?ClientAvatar, | +avatar?: ?ClientAvatar, | ||||
+color: string, | +color: string, | ||||
+parentThreadID: ?string, | +parentThreadID: ?string, | ||||
... | ... | ||||
Show All 32 Lines | ): ClientAvatar { | ||||
if (thread.type === threadTypes.PRIVATE) { | if (thread.type === threadTypes.PRIVATE) { | ||||
return getAvatarForUser(viewer); | return getAvatarForUser(viewer); | ||||
} | } | ||||
return getDefaultAvatar(thread.id, thread.color); | return getDefaultAvatar(thread.id, thread.color); | ||||
} | } | ||||
export { getAvatarForUser, useGetAvatarForThread }; | export { getAvatarForUser, getAvatarForThreadEntity, useGetAvatarForThread }; |