"use client"; import { useQuery } from "@tanstack/react-query"; import type { DocumentSearchRequest, DocumentSearchResponse } from "@/types/search"; const fetchDocumentSearch = async (payload: DocumentSearchRequest): Promise => { const response = await fetch("/api/search/documents", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }); if (!response.ok) { const message = (await response.json().catch(() => null))?.error ?? "搜索失败,请稍后再试"; throw new Error(message); } return response.json() as Promise; }; export function useDocumentSearch(payload: DocumentSearchRequest | null, enabled: boolean) { return useQuery({ queryKey: ["document-search", payload], queryFn: () => { if (!payload) { throw new Error("缺少搜索参数"); } return fetchDocumentSearch(payload); }, enabled: enabled && Boolean(payload?.workspaceId), staleTime: 30_000, gcTime: 60_000, }); }