Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dms: smoother joins & leaves #3017

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we actually should move these keys up and reuse them everywhere we use the unreads key

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed. Rather than doing it one off here, I'll open a separate PR to extract keys more consistently.

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
Loading