v3: various improvements and fixes to real-time updates
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 40s
All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 40s
This commit is contained in:
@@ -22,12 +22,13 @@ interface DumpListSyncOptions {
|
||||
* - deletedDumpIds growing → filter
|
||||
* - lastDumpEvent "updated" → update in-place, or remove if now private
|
||||
* - lastDumpEvent for a not-in-list dump → prepend if ownerId matches and visible
|
||||
* - lastCommentEvent created/deleted → adjust the dump's commentCount
|
||||
*/
|
||||
export function useDumpListSync(
|
||||
setDumps: (fn: (prev: Dump[]) => Dump[]) => void,
|
||||
options?: DumpListSyncOptions,
|
||||
): void {
|
||||
const { deletedDumpIds, lastDumpEvent } = useWS();
|
||||
const { deletedDumpIds, lastDumpEvent, lastCommentEvent } = useWS();
|
||||
|
||||
// Keep refs up-to-date so closures in effects are never stale.
|
||||
const setDumpsRef = useRef(setDumps);
|
||||
@@ -69,4 +70,23 @@ export function useDumpListSync(
|
||||
return [dump, ...prev];
|
||||
});
|
||||
}, [lastDumpEvent]);
|
||||
|
||||
// commentCount counts non-deleted comments, so created → +1, deleted → −1.
|
||||
// Edits don't change the count. Events only fire for public dumps.
|
||||
useEffect(() => {
|
||||
if (!lastCommentEvent) return;
|
||||
const { type, dumpId } = lastCommentEvent;
|
||||
if (type !== "created" && type !== "deleted") return;
|
||||
const delta = type === "created" ? 1 : -1;
|
||||
setDumpsRef.current((prev) => {
|
||||
const idx = prev.findIndex((d) => d.id === dumpId);
|
||||
if (idx === -1) return prev;
|
||||
const next = [...prev];
|
||||
next[idx] = {
|
||||
...next[idx],
|
||||
commentCount: Math.max(0, next[idx].commentCount + delta),
|
||||
};
|
||||
return next;
|
||||
});
|
||||
}, [lastCommentEvent]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user