diff --git a/src/state/cache/profile-shadow.ts b/src/state/cache/profile-shadow.ts index f387306f4e..e649b435c8 100644 --- a/src/state/cache/profile-shadow.ts +++ b/src/state/cache/profile-shadow.ts @@ -53,6 +53,17 @@ const shadows: WeakMap< > = new WeakMap() const emitter = new EventEmitter() +export function useOnUpdateProfileShadow( + onUpdate: (props: {did: string; shadow: Partial}) => void, +) { + useEffect(() => { + emitter.addListener('shadow-update', onUpdate) + return () => { + emitter.removeListener('shadow-update', onUpdate) + } + }, [onUpdate]) +} + export function useProfileShadow< TProfileView extends bsky.profile.AnyProfileView, >(profile: TProfileView): Shadow { @@ -206,10 +217,11 @@ export function updateProfileShadow( } batchedUpdates(() => { emitter.emit(did, value) + emitter.emit('shadow-update', {did, shadow: value}) }) } -function mergeShadow( +export function mergeShadow( profile: TProfileView, shadow: Partial, ): Shadow { diff --git a/src/state/messages/convo/agent.ts b/src/state/messages/convo/agent.ts index e89965eb45..6fc941d776 100644 --- a/src/state/messages/convo/agent.ts +++ b/src/state/messages/convo/agent.ts @@ -20,6 +20,7 @@ import { isNetworkError, } from '#/lib/strings/errors' import {Logger} from '#/logger' +import {mergeShadow, type ProfileShadow} from '#/state/cache/profile-shadow' import { ACTIVE_POLL_INTERVAL, BACKGROUND_POLL_INTERVAL, @@ -131,6 +132,19 @@ export class Convo { recipients: ChatBskyActorDefs.ProfileViewBasic[] | undefined snapshot: ConvoState | undefined + mergeProfileShadow(did: string, shadow: Partial) { + this.recipients?.map((recipient, i) => { + if (recipient.did === did) { + const merged = mergeShadow(recipient, shadow) + this.recipients![i] = merged + } + }) + const related = this.relatedProfiles.get(did) + if (related) { + this.relatedProfiles.set(did, mergeShadow(related, shadow)) + } + } + constructor(params: ConvoParams) { this.id = nanoid(3) this.convoId = params.convoId diff --git a/src/state/messages/convo/index.tsx b/src/state/messages/convo/index.tsx index 14a297e6b9..4732a898e6 100644 --- a/src/state/messages/convo/index.tsx +++ b/src/state/messages/convo/index.tsx @@ -11,6 +11,7 @@ import {useFocusEffect} from '@react-navigation/native' import {useQueryClient} from '@tanstack/react-query' import {useAppState} from '#/lib/appState' +import {useOnUpdateProfileShadow} from '#/state/cache/profile-shadow' import {Convo} from '#/state/messages/convo/agent' import { type ConvoParams, @@ -112,6 +113,10 @@ export function ConvoProvider({ }, [isActive, convo, convoId, markAsRead]), ) + useOnUpdateProfileShadow(({did, shadow}) => { + convo.mergeProfileShadow(did, shadow) + }) + useEffect(() => { return convo.on(event => { switch (event.type) {