Optimistically update chat invite button status (#10870)

This commit is contained in:
DS Boyce
2026-06-11 09:48:23 -07:00
committed by GitHub
parent b0eaecb01b
commit 91053e3f70
2 changed files with 63 additions and 1 deletions
+49
View File
@@ -76,6 +76,55 @@ export function invalidateJoinLinkPreviewsForCode(
})
}
/**
* Optimistically set whether the viewer has requested to join the link with the
* given code, across any cached join link preview queries. Used right after a
* successful join request (requested = true) or withdrawal (requested = false)
* so the UI ("Requested" vs "Request to join") updates immediately, without
* waiting on a server refetch that can lag behind the write.
*/
export function setJoinLinkPreviewRequestedForCode(
queryClient: QueryClient,
code: string,
requested: boolean,
) {
queryClient.setQueriesData<ChatBskyGroupGetJoinLinkPreviews.OutputSchema>(
{
predicate: query => {
const [root, args] = query.queryKey as Partial<
StructuredQueryKey<{codes?: string[]}>
>
return (
root === joinLinkPreviewQueryKeyRoot &&
Array.isArray(args?.codes) &&
args.codes.includes(code)
)
},
},
old => {
if (!old) return old
return {
...old,
joinLinkPreviews: old.joinLinkPreviews.map(preview => {
if (
ChatBskyGroupDefs.isJoinLinkPreviewView(preview) &&
preview.code === code
) {
return {
...preview,
viewer: {
...preview.viewer,
requestedAt: requested ? new Date().toISOString() : undefined,
},
}
}
return preview
}),
}
},
)
}
/**
* Invalidate any join link preview queries that resolved to the given convo.
* The code isn't always known to the viewer (e.g. when they're a regular