Skip to content

Commit

Permalink
Merge pull request #3017 from tloncorp/lb/fix-rsvp
Browse files Browse the repository at this point in the history
dms: smoother joins & leaves
  • Loading branch information
latter-bolden authored Nov 20, 2023
2 parents be53bd1 + 125575b commit 9ce0c9f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
44 changes: 26 additions & 18 deletions ui/src/state/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import emptyMultiDm, {
appendWritToLastPage,
buildCachedWrit,
buildWritPage,
removePendingFromCache,
removeUnreadFromCache,
} from './utils';

export interface State {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
};
}
Expand Down Expand Up @@ -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]);
},
Expand Down Expand Up @@ -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]);
},
Expand Down
30 changes: 30 additions & 0 deletions ui/src/state/chat/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@/types/channel';
import {
Club,
DMUnreads,
PagedWrits,
ReplyDelta,
Writ,
Expand All @@ -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';
Expand All @@ -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;
Expand Down

0 comments on commit 9ce0c9f

Please sign in to comment.