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.
This commit is contained in:
Anish Sarkar 2026-03-01 22:58:02 +05:30
parent 6d00b0debf
commit 6de6684f81

View file

@ -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<number>();
let _batchReady: Promise<void> | null = null;
let _resolveBatchReady: (() => void) | null = null;
function resetBatchGate() {
function resetBatchGate(resolveImmediately = false) {
_batchReady = new Promise<void>((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<GetCommentsResponse>({
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<GetCommentsResponse>(cacheKeys.comments.byMessage(messageId));
if (cached) return cached;
}