From 125575bd81e5648a39c2926f7e1a83dc35846580 Mon Sep 17 00:00:00 2001 From: ~latter-bolden Date: Mon, 20 Nov 2023 17:04:56 -0500 Subject: [PATCH] dms: actually reload dm window on join and clear unread on leave --- ui/src/state/chat/chat.ts | 44 ++++++++++++++++++++++---------------- ui/src/state/chat/utils.ts | 30 ++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 18 deletions(-) diff --git a/ui/src/state/chat/chat.ts b/ui/src/state/chat/chat.ts index 5b664276da..4cc3cde80a 100644 --- a/ui/src/state/chat/chat.ts +++ b/ui/src/state/chat/chat.ts @@ -48,6 +48,8 @@ import emptyMultiDm, { appendWritToLastPage, buildCachedWrit, buildWritPage, + removePendingFromCache, + removeUnreadFromCache, } from './utils'; export interface State { @@ -745,9 +747,8 @@ export function useMarkDmReadMutation() { }); } -const emptyUnreads: DMUnreads = {}; -const dmUnreadsKey = ['dm', 'unreads']; export function useDmUnreads() { + const dmUnreadsKey = ['dms', 'unreads']; const { mutate: markDmRead } = useMarkDmReadMutation(); const invalidate = useRef( _.debounce( @@ -790,10 +791,14 @@ export function useDmUnreads() { path: '/unreads', scry: '/unreads', onEvent: eventHandler, + options: { + retryOnMount: true, + refetchOnMount: true, + }, }); return { - data: data || emptyUnreads, + data: data || {}, ...query, }; } @@ -876,25 +881,17 @@ export function useDmRsvpMutation() { mutationFn, onMutate: (variables) => { const { ship, accept } = variables; - queryClient.setQueryData( - ['dms', 'unreads'], - (unreads: DMUnreads | undefined) => { - if (!unreads) { - return unreads; - } - - const newUnreads = { ...unreads }; - - if (!accept) { - delete newUnreads[ship]; - } - return newUnreads; - } - ); + // optimistic updates + if (accept) { + removePendingFromCache(queryClient, ship); + } else { + removeUnreadFromCache(queryClient, ship); + } }, onSettled: (_data, _error, variables) => { queryClient.invalidateQueries(['dms', 'unreads']); + queryClient.invalidateQueries(['dms', 'pending']); queryClient.invalidateQueries(['dms', 'dms']); queryClient.invalidateQueries(['dms', variables.ship]); }, @@ -1038,8 +1035,19 @@ export function useMutliDmRsvpMutation() { return useMutation({ mutationFn, + onMutate: (variables) => { + const { id, accept } = variables; + + // optimistic updates + if (accept) { + removePendingFromCache(queryClient, id); + } else { + removeUnreadFromCache(queryClient, id); + } + }, onSettled: (_data, _error, variables) => { queryClient.invalidateQueries(['dms', 'unreads']); + queryClient.invalidateQueries(['dms', 'pending']); queryClient.invalidateQueries(['dms', 'multi']); queryClient.invalidateQueries(['dms', variables.id]); }, diff --git a/ui/src/state/chat/utils.ts b/ui/src/state/chat/utils.ts index 61797412de..5b1660e999 100644 --- a/ui/src/state/chat/utils.ts +++ b/ui/src/state/chat/utils.ts @@ -7,6 +7,7 @@ import { } from '@/types/channel'; import { Club, + DMUnreads, PagedWrits, ReplyDelta, Writ, @@ -15,6 +16,7 @@ import { WritMemo, WritSeal, } from '@/types/dms'; +import { QueryClient } from '@tanstack/react-query'; import { udToDec } from '@urbit/api'; import { formatUd, unixToDa } from '@urbit/aura'; import bigInt from 'big-integer'; @@ -33,6 +35,34 @@ export default function emptyMultiDm(): Club { }; } +export function removePendingFromCache(queryClient: QueryClient, id: string) { + queryClient.setQueryData( + ['dms', 'pending'], + (pending: string[] | undefined) => { + if (!pending) { + return pending; + } + return pending.filter((p) => p !== id); + } + ); +} + +export function removeUnreadFromCache(queryClient: QueryClient, id: string) { + queryClient.setQueryData( + ['dms', 'unreads'], + (unreads: DMUnreads | undefined) => { + if (!unreads) { + return unreads; + } + + const newUnreads = { ...unreads }; + delete newUnreads[id]; + + return newUnreads; + } + ); +} + interface PageParam { time: BigInteger; direction: string;