refactor: replace document type counts atom with real-time hook

- Removed the `documentTypeCountsAtom` and its associated logic from the document query atoms.
- Introduced `useZeroDocumentTypeCounts` hook to provide real-time document type counts, enhancing responsiveness as documents are indexed.
- Updated components to utilize the new hook for fetching document type counts, ensuring instant updates in the UI.
This commit is contained in:
Anish Sarkar 2026-03-27 12:04:01 +05:30
parent 683a4c17dd
commit ec79142d52
4 changed files with 38 additions and 45 deletions

View file

@ -0,0 +1,31 @@
"use client";
import { useQuery } from "@rocicorp/zero/react";
import { useMemo } from "react";
import { queries } from "@/zero/queries";
/**
* Real-time document type counts derived from Zero's live document sync.
* Updates instantly as documents are created, deleted, or change type.
*/
export function useZeroDocumentTypeCounts(
searchSpaceId: number | string | null
): Record<string, number> | undefined {
const numericId = searchSpaceId != null ? Number(searchSpaceId) : null;
const [zeroDocuments] = useQuery(
queries.documents.bySpace({ searchSpaceId: numericId ?? -1 })
);
return useMemo(() => {
if (!zeroDocuments || numericId == null) return undefined;
const counts: Record<string, number> = {};
for (const doc of zeroDocuments) {
if (doc.id != null && doc.title != null && doc.title !== "") {
counts[doc.documentType] = (counts[doc.documentType] || 0) + 1;
}
}
return counts;
}, [zeroDocuments, numericId]);
}