Download src/hooks/useDocuments.ts from asnannp/DocDoeAI: direct link, hf CLI and curl.
- Browser
- Download file 1.95 kB
-
https://huggingface.co/spaces/asnannp/DocDoeAI/resolve/main/src/hooks/useDocuments.ts
- Command line
-
hf download hf://spaces/asnannp/DocDoeAI/src/hooks/useDocuments.ts
-
curl -L -o useDocuments.ts https://huggingface.co/spaces/asnannp/DocDoeAI/resolve/main/src/hooks/useDocuments.ts
1.95 kB
| "use client"; | |
| import { useCallback, useEffect, useState } from "react"; | |
| import { listDocuments } from "@/lib/api/documents"; | |
| import { frontendDataEvents } from "@/lib/frontend-local-store"; | |
| import type { ApiDocument } from "@/lib/api/types"; | |
| export function useDocuments() { | |
| const [documents, setDocuments] = useState<ApiDocument[]>([]); | |
| const [selectedDocumentId, setSelectedDocumentId] = useState(""); | |
| const [isLoading, setIsLoading] = useState(true); | |
| const [error, setError] = useState<string | null>(null); | |
| const refreshDocuments = useCallback(async () => { | |
| setIsLoading(true); | |
| setError(null); | |
| try { | |
| const data = await listDocuments(); | |
| setDocuments(data); | |
| setSelectedDocumentId((current) => current || data[0]?.id || ""); | |
| } catch (caughtError) { | |
| setError( | |
| caughtError instanceof Error | |
| ? caughtError.message | |
| : "Could not load documents.", | |
| ); | |
| } finally { | |
| setIsLoading(false); | |
| } | |
| }, []); | |
| useEffect(() => { | |
| let isActive = true; | |
| const loadDocuments = () => listDocuments() | |
| .then((data) => { | |
| if (!isActive) return; | |
| setDocuments(data); | |
| setSelectedDocumentId((current) => current || data[0]?.id || ""); | |
| }) | |
| .catch((caughtError: unknown) => { | |
| if (!isActive) return; | |
| setError( | |
| caughtError instanceof Error | |
| ? caughtError.message | |
| : "Could not load documents.", | |
| ); | |
| }) | |
| .finally(() => { | |
| if (!isActive) return; | |
| setIsLoading(false); | |
| }); | |
| void loadDocuments(); | |
| window.addEventListener(frontendDataEvents.documents, loadDocuments); | |
| return () => { | |
| isActive = false; | |
| window.removeEventListener(frontendDataEvents.documents, loadDocuments); | |
| }; | |
| }, []); | |
| return { | |
| documents, | |
| selectedDocumentId, | |
| setSelectedDocumentId, | |
| isLoading, | |
| error, | |
| refreshDocuments, | |
| }; | |
| } | |