diff --git a/surfsense_web/app/dashboard/[search_space_id]/documents/(manage)/page.tsx b/surfsense_web/app/dashboard/[search_space_id]/documents/(manage)/page.tsx index 54fd490a1..c2ddf6f71 100644 --- a/surfsense_web/app/dashboard/[search_space_id]/documents/(manage)/page.tsx +++ b/surfsense_web/app/dashboard/[search_space_id]/documents/(manage)/page.tsx @@ -20,7 +20,7 @@ import { DocumentsFilters } from "./components/DocumentsFilters"; import { DocumentsTableShell, type SortKey } from "./components/DocumentsTableShell"; import { PaginationControls } from "./components/PaginationControls"; import { ProcessingIndicator } from "./components/ProcessingIndicator"; -import type { ColumnVisibility, Document } from "./components/types"; +import type { ColumnVisibility } from "./components/types"; function useDebounced(value: T, delay = 250) { const [debounced, setDebounced] = useState(value); @@ -60,39 +60,30 @@ export default function DocumentsTable() { const { data: rawTypeCounts } = useAtomValue(documentTypeCountsAtom); const { mutateAsync: deleteDocumentMutation } = useAtomValue(deleteDocumentMutationAtom); - // Filter out SURFSENSE_DOCS from active types for regular documents API - const regularDocumentTypes = useMemo( - () => activeTypes.filter((t) => t !== "SURFSENSE_DOCS"), - [activeTypes] - ); - - // Check if only SURFSENSE_DOCS is selected (skip regular docs query) - const onlySurfsenseDocsSelected = activeTypes.length === 1 && activeTypes[0] === "SURFSENSE_DOCS"; - - // Build query parameters for fetching documents (excluding SURFSENSE_DOCS type) + // Build query parameters for fetching documents const queryParams = useMemo( () => ({ search_space_id: searchSpaceId, page: pageIndex, page_size: pageSize, - ...(regularDocumentTypes.length > 0 && { document_types: regularDocumentTypes }), + ...(activeTypes.length > 0 && { document_types: activeTypes }), }), - [searchSpaceId, pageIndex, pageSize, regularDocumentTypes] + [searchSpaceId, pageIndex, pageSize, activeTypes] ); - // Build search query parameters (excluding SURFSENSE_DOCS type) + // Build search query parameters const searchQueryParams = useMemo( () => ({ search_space_id: searchSpaceId, page: pageIndex, page_size: pageSize, title: debouncedSearch.trim(), - ...(regularDocumentTypes.length > 0 && { document_types: regularDocumentTypes }), + ...(activeTypes.length > 0 && { document_types: activeTypes }), }), - [searchSpaceId, pageIndex, pageSize, regularDocumentTypes, debouncedSearch] + [searchSpaceId, pageIndex, pageSize, activeTypes, debouncedSearch] ); - // Use query for fetching documents (disabled when only SURFSENSE_DOCS is selected) + // Use query for fetching documents const { data: documentsResponse, isLoading: isDocumentsLoading, @@ -102,10 +93,10 @@ export default function DocumentsTable() { queryKey: cacheKeys.documents.globalQueryParams(queryParams), queryFn: () => documentsApiService.getDocuments({ queryParams }), staleTime: 3 * 60 * 1000, // 3 minutes - enabled: !!searchSpaceId && !debouncedSearch.trim() && !onlySurfsenseDocsSelected, + enabled: !!searchSpaceId && !debouncedSearch.trim(), }); - // Use query for searching documents (disabled when only SURFSENSE_DOCS is selected) + // Use query for searching documents const { data: searchResponse, isLoading: isSearchLoading, @@ -115,114 +106,20 @@ export default function DocumentsTable() { queryKey: cacheKeys.documents.globalQueryParams(searchQueryParams), queryFn: () => documentsApiService.searchDocuments({ queryParams: searchQueryParams }), staleTime: 3 * 60 * 1000, // 3 minutes - enabled: !!searchSpaceId && !!debouncedSearch.trim() && !onlySurfsenseDocsSelected, + enabled: !!searchSpaceId && !!debouncedSearch.trim(), }); - // Determine if we should show SurfSense docs (when no type filter or SURFSENSE_DOCS is selected) - const showSurfsenseDocs = - activeTypes.length === 0 || activeTypes.includes("SURFSENSE_DOCS" as DocumentTypeEnum); - - // Use query for fetching SurfSense docs - const { - data: surfsenseDocsResponse, - isLoading: isSurfsenseDocsLoading, - refetch: refetchSurfsenseDocs, - } = useQuery({ - queryKey: ["surfsense-docs", debouncedSearch, pageIndex, pageSize], - queryFn: () => - documentsApiService.getSurfsenseDocs({ - queryParams: { - page: pageIndex, - page_size: pageSize, - title: debouncedSearch.trim() || undefined, - }, - }), - staleTime: 3 * 60 * 1000, // 3 minutes - enabled: showSurfsenseDocs, - }); - - // Transform SurfSense docs to match the Document type - const surfsenseDocsAsDocuments: Document[] = useMemo(() => { - if (!surfsenseDocsResponse?.items) return []; - return surfsenseDocsResponse.items.map((doc) => ({ - id: doc.id, - title: doc.title, - document_type: "SURFSENSE_DOCS", - document_metadata: { source: doc.source }, - content: doc.content, - created_at: doc.created_at || doc.updated_at || new Date().toISOString(), - search_space_id: -1, // Special value for global docs - })); - }, [surfsenseDocsResponse]); - - // Merge type counts with SURFSENSE_DOCS count - const typeCounts = useMemo(() => { - const counts = { ...(rawTypeCounts || {}) }; - if (surfsenseDocsResponse?.total) { - counts.SURFSENSE_DOCS = surfsenseDocsResponse.total; - } - return counts; - }, [rawTypeCounts, surfsenseDocsResponse?.total]); - // Extract documents and total based on search state - const regularDocuments = debouncedSearch.trim() + const documents = debouncedSearch.trim() ? searchResponse?.items || [] : documentsResponse?.items || []; - const regularTotal = debouncedSearch.trim() - ? searchResponse?.total || 0 - : documentsResponse?.total || 0; + const total = debouncedSearch.trim() ? searchResponse?.total || 0 : documentsResponse?.total || 0; - // Merge regular documents with SurfSense docs - const documents = useMemo(() => { - // If filtering by type and not including SURFSENSE_DOCS, only show regular docs - if (activeTypes.length > 0 && !activeTypes.includes("SURFSENSE_DOCS" as DocumentTypeEnum)) { - return regularDocuments; - } - // If filtering only by SURFSENSE_DOCS, only show surfsense docs - if (activeTypes.length === 1 && activeTypes[0] === "SURFSENSE_DOCS") { - return surfsenseDocsAsDocuments; - } - // Otherwise, merge both (surfsense docs first) - return [...surfsenseDocsAsDocuments, ...regularDocuments]; - }, [regularDocuments, surfsenseDocsAsDocuments, activeTypes]); + const loading = debouncedSearch.trim() ? isSearchLoading : isDocumentsLoading; + const error = debouncedSearch.trim() ? searchError : documentsError; - const total = useMemo(() => { - if (activeTypes.length > 0 && !activeTypes.includes("SURFSENSE_DOCS" as DocumentTypeEnum)) { - return regularTotal; - } - if (activeTypes.length === 1 && activeTypes[0] === "SURFSENSE_DOCS") { - return surfsenseDocsResponse?.total || 0; - } - return regularTotal + (surfsenseDocsResponse?.total || 0); - }, [regularTotal, surfsenseDocsResponse?.total, activeTypes]); - - const loading = useMemo(() => { - // If only SURFSENSE_DOCS selected, only check surfsense loading - if (onlySurfsenseDocsSelected) { - return isSurfsenseDocsLoading; - } - // Otherwise check both regular docs and surfsense docs loading - const regularLoading = debouncedSearch.trim() ? isSearchLoading : isDocumentsLoading; - return regularLoading || (showSurfsenseDocs && isSurfsenseDocsLoading); - }, [ - onlySurfsenseDocsSelected, - isSurfsenseDocsLoading, - debouncedSearch, - isSearchLoading, - isDocumentsLoading, - showSurfsenseDocs, - ]); - - const error = useMemo(() => { - // If only SURFSENSE_DOCS selected, no regular docs errors - if (onlySurfsenseDocsSelected) { - return null; - } - return debouncedSearch.trim() ? searchError : documentsError; - }, [onlySurfsenseDocsSelected, debouncedSearch, searchError, documentsError]); - - // Display server-filtered results directly - const displayDocs = documents || []; + // Display results directly + const displayDocs = documents; const displayTotal = total; const pageStart = pageIndex * pageSize; const pageEnd = Math.min(pageStart + pageSize, displayTotal); @@ -242,33 +139,16 @@ export default function DocumentsTable() { if (isRefreshing) return; setIsRefreshing(true); try { - const refetchPromises: Promise[] = []; - // Only refetch regular documents if not in "only surfsense docs" mode - if (!onlySurfsenseDocsSelected) { - if (debouncedSearch.trim()) { - refetchPromises.push(refetchSearch()); - } else { - refetchPromises.push(refetchDocuments()); - } + if (debouncedSearch.trim()) { + await refetchSearch(); + } else { + await refetchDocuments(); } - if (showSurfsenseDocs) { - refetchPromises.push(refetchSurfsenseDocs()); - } - await Promise.all(refetchPromises); toast.success(t("refresh_success") || "Documents refreshed"); } finally { setIsRefreshing(false); } - }, [ - debouncedSearch, - refetchSearch, - refetchDocuments, - refetchSurfsenseDocs, - showSurfsenseDocs, - onlySurfsenseDocsSelected, - t, - isRefreshing, - ]); + }, [debouncedSearch, refetchSearch, refetchDocuments, t, isRefreshing]); // Set up smart polling for active tasks - only polls when tasks are in progress const { summary } = useLogsSummary(searchSpaceId, 24, { @@ -385,7 +265,7 @@ export default function DocumentsTable() {