import { useQuery } from "@tanstack/react-query"; import { getDocument } from "@/services/documents"; import { queryKeys } from "@/lib/query-keys"; import { useRuntimeContext } from "@/providers/runtime-context-provider"; /** * Fetch a single document via DescribeDocument. The list endpoint omits the * `metadata` field, so anything that needs to render or edit metadata must * use this hook rather than reading from the listDocuments cache. */ export function useDocument( knowledgeBaseName: string, docId: string | null | undefined, options?: { subspace?: string; enabled?: boolean }, ) { const { context, ready } = useRuntimeContext(); const { subspace, enabled = true } = options ?? {}; return useQuery({ queryKey: queryKeys.document( context.dataScopeKey, knowledgeBaseName, docId, subspace, ), queryFn: () => getDocument(knowledgeBaseName, docId as string, { subspace }), enabled: ready && !!knowledgeBaseName && !!docId && enabled, staleTime: 30_000, }); }