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

This commit is contained in:
khannurien
2026-06-29 07:52:56 +00:00
parent f162d23ce3
commit aab5b5bfd5
12 changed files with 227 additions and 38 deletions

View File

@@ -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]);
}