refactor: optimize polling and improve optimistic update stability (#753)

Co-authored-by: Syeda Anshrah Gillani <Gillani@cloudment.io>
This commit is contained in:
SyedaAnshrahGillani 2026-03-04 08:01:25 +05:00 • committed by GitHub
parent 48845bb072
commit 5d9e142596
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -47,8 +47,8 @@ export const fetchSubscriptionStatus = (
return statusMap
},
queryKey: ["subscription-status"],
refetchInterval: 5000, // Refetch every 5 seconds
staleTime: 4000, // Consider data stale after 4 seconds
refetchInterval: 60 * 1000, // Refetch every 1 minute
staleTime: 55 * 1000, // Consider data stale after 55 seconds
enabled: isEnabled,
})
@ -132,18 +132,41 @@ export const useDeleteDocument = (selectedProject: string) => {
["documents-with-memories", selectedProject],
(old: unknown) => {
if (!old || typeof old !== "object") return old
const typedOld = old as {
pages?: Array<{ documents?: DocumentWithMemories[] }>
// Handle Infinite Query structure (TanStack Query v5 uses 'pages')
if (
"pages" in old &&
Array.isArray((old as Record<string, unknown>).pages)
) {
const typedOld = old as {
pages: Array<{ documents?: DocumentWithMemories[] }>
}
return {
...typedOld,
pages: typedOld.pages.map((page) => ({
...page,
documents: page.documents?.filter(
(doc: DocumentWithMemories) => doc.id !== documentId,
),
})),
}
}
return {
...typedOld,
pages: typedOld.pages?.map((page) => ({
...page,
documents: page.documents?.filter(
// Handle Standard Query structure
if (
"documents" in old &&
Array.isArray((old as Record<string, unknown>).documents)
) {
const typedOld = old as { documents: DocumentWithMemories[] }
return {
...typedOld,
documents: typedOld.documents.filter(
(doc: DocumentWithMemories) => doc.id !== documentId,
),
})),
}
}
return old
},
)