From 6de6684f81691faea6cae7b749a623dc2e526328 Mon Sep 17 00:00:00 2001 From: Anish Sarkar <104695310+AnishSarkar22@users.noreply.github.com> Date: Sun, 1 Mar 2026 22:58:02 +0530 Subject: [PATCH] refactor: improve type safety for `use-comments.ts` - Updated the `resetBatchGate` function to accept a parameter for immediate resolution, enhancing batch processing control. - Changed the return type of the `useQuery` hook in `useComments` to specify `GetCommentsResponse`, improving type safety and clarity. - Adjusted the initial batch gate invocation to resolve immediately, streamlining the comments fetching process. --- surfsense_web/hooks/use-comments.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/surfsense_web/hooks/use-comments.ts b/surfsense_web/hooks/use-comments.ts index c02f9fe16..2f7128149 100644 --- a/surfsense_web/hooks/use-comments.ts +++ b/surfsense_web/hooks/use-comments.ts @@ -1,5 +1,6 @@ import { useQuery, useQueryClient } from "@tanstack/react-query"; import { useEffect, useRef } from "react"; +import type { GetCommentsResponse } from "@/contracts/types/chat-comments.types"; import { chatCommentsApiService } from "@/lib/apis/chat-comments-api.service"; import { cacheKeys } from "@/lib/query-client/cache-keys"; @@ -22,20 +23,20 @@ let _batchTargetIds = new Set(); let _batchReady: Promise | null = null; let _resolveBatchReady: (() => void) | null = null; -function resetBatchGate() { +function resetBatchGate(resolveImmediately = false) { _batchReady = new Promise((r) => { _resolveBatchReady = r; + if (resolveImmediately) r(); }); } // Open the initial gate immediately (no batch pending yet) -resetBatchGate(); -_resolveBatchReady?.(); +resetBatchGate(true); export function useComments({ messageId, enabled = true }: UseCommentsOptions) { const queryClient = useQueryClient(); - return useQuery({ + return useQuery({ queryKey: cacheKeys.comments.byMessage(messageId), queryFn: async () => { // Wait for the batch gate so the useEffect in useBatchCommentsPreload @@ -46,7 +47,7 @@ export function useComments({ messageId, enabled = true }: UseCommentsOptions) { if (_batchInflight && _batchTargetIds.has(messageId)) { await _batchInflight; - const cached = queryClient.getQueryData(cacheKeys.comments.byMessage(messageId)); + const cached = queryClient.getQueryData(cacheKeys.comments.byMessage(messageId)); if (cached) return cached; }