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 @@ -8,6 +8,7 @@ import type { ClientEmojiAvatar, ClientAvatar } from '../types/avatar-types.js'; import { type ThreadType, threadTypes } from '../types/thread-types.js'; import { useResolvedOptionalThreadInfo } from '../utils/entity-helpers.js'; +import type { EntityTextComponent } from '../utils/entity-text.js'; import { useSelector } from '../utils/redux-utils.js'; const defaultAnonymousUserEmojiAvatar: ClientEmojiAvatar = { @@ -62,6 +63,27 @@ 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( thread: ?{ +id: string, @@ -110,4 +132,4 @@ return getDefaultAvatar(thread.id, thread.color); } -export { getAvatarForUser, useGetAvatarForThread }; +export { getAvatarForUser, getAvatarForThreadEntity, useGetAvatarForThread }; diff --git a/lib/utils/entity-helpers.js b/lib/utils/entity-helpers.js --- a/lib/utils/entity-helpers.js +++ b/lib/utils/entity-helpers.js @@ -8,6 +8,7 @@ useENSNamesForEntityText, entityTextToRawString, } from './entity-text.js'; +import { getAvatarForThreadEntity } from '../shared/avatar-utils.js'; import type { ThreadInfo, ResolvedThreadInfo } from '../types/thread-types.js'; import { values } from '../utils/objects.js'; @@ -35,6 +36,7 @@ return { ...threadInfo, uiName: entityTextToRawString([resolvedThreadEntity]), + avatar: getAvatarForThreadEntity(resolvedThreadEntity), }; }), [threadInfos, withENSNames], @@ -71,6 +73,7 @@ return { ...threadInfo, uiName: entityTextToRawString([resolvedThreadEntity]), + avatar: getAvatarForThreadEntity(resolvedThreadEntity), }; }); }, [threadInfos, withENSNames]); diff --git a/lib/utils/entity-text.js b/lib/utils/entity-text.js --- a/lib/utils/entity-text.js +++ b/lib/utils/entity-text.js @@ -7,6 +7,7 @@ import { useENSNames } from '../hooks/ens-cache.js'; import { threadNoun } from '../shared/thread-utils.js'; import { stringForUser } from '../shared/user-utils.js'; +import type { ClientAvatar } from '../types/avatar-types.js'; import { threadTypes, type ThreadType, @@ -19,6 +20,7 @@ +type: 'user', +id: string, +username?: ?string, + +avatar?: ?ClientAvatar, +isViewer?: ?boolean, +possessive?: ?boolean, // eg. `user's` instead of `user` }; @@ -53,7 +55,11 @@ +hex: string, }; -type EntityTextComponent = UserEntity | ThreadEntity | ColorEntity | string; +export type EntityTextComponent = + | UserEntity + | ThreadEntity + | ColorEntity + | string; export type EntityText = $ReadOnlyArray;