Skip to content
Open
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
27 changes: 18 additions & 9 deletions apps/web/hooks/use-processing-documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ export function useProcessingDocuments() {
staleTime: 0,
})

const docs =
(
data as
| { documents?: Array<{ id?: string | null; status?: string | null }> }
| undefined
)?.documents ?? []
// Memoized on `data` (kept referentially stable between polls by React
// Query's structural sharing) so `processingMap` only changes identity
// when the poll payload actually changes — the effect below depends on it.
const docs = useMemo(
() =>
(
data as
| {
documents?: Array<{ id?: string | null; status?: string | null }>
}
| undefined
)?.documents ?? [],
[data],
)

const processingMap = useMemo(() => {
const map = new Map<string, string>()
Expand All @@ -52,7 +60,6 @@ export function useProcessingDocuments() {
}
}
return map
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [docs])

// Detect docs that just finished (present in previous poll, absent now).
Expand Down Expand Up @@ -80,8 +87,10 @@ export function useProcessingDocuments() {
clearTimeout(t1)
clearTimeout(t2)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [processingMap.keys, queryClient.refetchQueries])
// `processingMap` (not `processingMap.keys` — that's the shared
// Map.prototype method, identical for every map, so the effect would
// never re-run and finished docs would never trigger a refresh).
}, [processingMap, queryClient])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This cleanup now runs every time processingMap changes, so a later poll can cancel the delayed refetches that were scheduled for a document that just finished. For example, if document A disappears and the hook schedules the 1s/4s refreshes, then another upload or sync changes the processing list before those timers fire, React runs this cleanup and clears both timers. Consider tracking pending timer IDs in a ref and clearing them only on unmount, or otherwise avoid canceling finished-document refreshes on unrelated processingMap changes.


return processingMap
}