From 3ca41e4efb24809dcc5e5a5c3e678eb561fd7ad6 Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Wed, 22 May 2024 18:14:15 +0100 Subject: [PATCH] =?UTF-8?q?[=F0=9F=90=B4]=20Invalidate=20list=20convos=20q?= =?UTF-8?q?uery=20on=20block=20(#4171)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * more memoization * invalidate listconvos query on block --- .../queries/messages/list-converations.ts | 38 ++++++++++--------- src/state/queries/profile.ts | 2 + 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/state/queries/messages/list-converations.ts b/src/state/queries/messages/list-converations.ts index 25ac9a16..493ee0d1 100644 --- a/src/state/queries/messages/list-converations.ts +++ b/src/state/queries/messages/list-converations.ts @@ -46,27 +46,29 @@ export function useUnreadMessageCount() { }) const moderationOpts = useModerationOpts() - const count = - convos.data?.pages - .flatMap(page => page.convos) - .filter(convo => convo.id !== currentConvoId) - .reduce((acc, convo) => { - const otherMember = convo.members.find( - member => member.did !== currentAccount?.did, - ) + const count = useMemo(() => { + return ( + convos.data?.pages + .flatMap(page => page.convos) + .filter(convo => convo.id !== currentConvoId) + .reduce((acc, convo) => { + const otherMember = convo.members.find( + member => member.did !== currentAccount?.did, + ) - if (!otherMember || !moderationOpts) return acc + if (!otherMember || !moderationOpts) return acc - // TODO could shadow this outside this hook and get optimistic block state - const moderation = moderateProfile(otherMember, moderationOpts) - const shouldIgnore = - convo.muted || - moderation.blocked || - otherMember.did === 'missing.invalid' - const unreadCount = !shouldIgnore && convo.unreadCount > 0 ? 1 : 0 + const moderation = moderateProfile(otherMember, moderationOpts) + const shouldIgnore = + convo.muted || + moderation.blocked || + otherMember.did === 'missing.invalid' + const unreadCount = !shouldIgnore && convo.unreadCount > 0 ? 1 : 0 - return acc + unreadCount - }, 0) ?? 0 + return acc + unreadCount + }, 0) ?? 0 + ) + }, [convos.data, currentAccount?.did, currentConvoId, moderationOpts]) return useMemo(() => { return { diff --git a/src/state/queries/profile.ts b/src/state/queries/profile.ts index 3e253591..af8718c5 100644 --- a/src/state/queries/profile.ts +++ b/src/state/queries/profile.ts @@ -25,6 +25,7 @@ import {STALE} from '#/state/queries' import {resetProfilePostsQueries} from '#/state/queries/post-feed' import {updateProfileShadow} from '../cache/profile-shadow' import {useAgent, useSession} from '../session' +import {RQKEY as RQKEY_LIST_CONVOS} from './messages/list-converations' import {RQKEY as RQKEY_MY_BLOCKED} from './my-blocked-accounts' import {RQKEY as RQKEY_MY_MUTED} from './my-muted-accounts' @@ -414,6 +415,7 @@ export function useProfileBlockMutationQueue( updateProfileShadow(queryClient, did, { blockingUri: finalBlockingUri, }) + queryClient.invalidateQueries({queryKey: RQKEY_LIST_CONVOS}) }, })