mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-21 18:55:16 +02:00
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:
parent
683a4c17dd
commit
ec79142d52
4 changed files with 38 additions and 45 deletions
31
surfsense_web/hooks/use-zero-document-type-counts.ts
Normal file
31
surfsense_web/hooks/use-zero-document-type-counts.ts
Normal 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]);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue