feat(fix): document type filtering

This commit is contained in:
DESKTOP-RTLN3BA\$punk 2025-10-21 21:53:55 -07:00
parent fec8deabcc
commit 18adf79649
7 changed files with 623 additions and 705 deletions

View file

@ -26,7 +26,7 @@ import {
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import type { ColumnVisibility, Document } from "./types";
import type { ColumnVisibility } from "./types";
const fadeInScale: Variants = {
hidden: { opacity: 0, scale: 0.95 },
@ -35,8 +35,7 @@ const fadeInScale: Variants = {
};
export function DocumentsFilters({
allDocuments,
visibleDocuments: _visibleDocuments,
typeCounts: typeCountsRecord,
selectedIds,
onSearch,
searchValue,
@ -46,8 +45,7 @@ export function DocumentsFilters({
columnVisibility,
onToggleColumn,
}: {
allDocuments: Document[];
visibleDocuments: Document[];
typeCounts: Record<string, number>;
selectedIds: Set<number>;
onSearch: (v: string) => void;
searchValue: string;
@ -61,16 +59,16 @@ export function DocumentsFilters({
const inputRef = useRef<HTMLInputElement>(null);
const uniqueTypes = useMemo(() => {
const set = new Set<string>();
for (const d of allDocuments) set.add(d.document_type);
return Array.from(set).sort();
}, [allDocuments]);
return Object.keys(typeCountsRecord).sort();
}, [typeCountsRecord]);
const typeCounts = useMemo(() => {
const map = new Map<string, number>();
for (const d of allDocuments) map.set(d.document_type, (map.get(d.document_type) ?? 0) + 1);
for (const [type, count] of Object.entries(typeCountsRecord)) {
map.set(type, count);
}
return map;
}, [allDocuments]);
}, [typeCountsRecord]);
return (
<motion.div

View file

@ -40,40 +40,59 @@ export default function DocumentsTable() {
const [sortKey, setSortKey] = useState<SortKey>("title");
const [sortDesc, setSortDesc] = useState(false);
const [selectedIds, setSelectedIds] = useState<Set<number>>(new Set());
const [typeCounts, setTypeCounts] = useState<Record<string, number>>({});
// Use server-side pagination and search
const { documents, total, loading, error, fetchDocuments, searchDocuments, deleteDocument } =
useDocuments(searchSpaceId, {
page: pageIndex,
pageSize: pageSize,
});
// Use server-side pagination, search, and filtering
const {
documents,
total,
loading,
error,
fetchDocuments,
searchDocuments,
deleteDocument,
getDocumentTypeCounts,
} = useDocuments(searchSpaceId, {
page: pageIndex,
pageSize: pageSize,
});
// Fetch document type counts on mount and when search space changes
useEffect(() => {
if (searchSpaceId && getDocumentTypeCounts) {
getDocumentTypeCounts().then(setTypeCounts);
}
}, [searchSpaceId, getDocumentTypeCounts]);
// Refetch when pagination changes or when search/filters change
useEffect(() => {
if (searchSpaceId) {
if (debouncedSearch.trim()) {
// Use search endpoint if there's a search query
searchDocuments?.(debouncedSearch, pageIndex, pageSize);
searchDocuments?.(
debouncedSearch,
pageIndex,
pageSize,
activeTypes.length > 0 ? activeTypes : undefined
);
} else {
// Use regular fetch if no search
fetchDocuments?.(pageIndex, pageSize);
fetchDocuments?.(pageIndex, pageSize, activeTypes.length > 0 ? activeTypes : undefined);
}
}
}, [pageIndex, pageSize, debouncedSearch, searchSpaceId, fetchDocuments, searchDocuments]);
}, [
pageIndex,
pageSize,
debouncedSearch,
activeTypes,
searchSpaceId,
fetchDocuments,
searchDocuments,
]);
// Client-side filtering for document types only
// Note: This could also be moved to the backend for better performance
const filtered = useMemo(() => {
let result = documents || [];
if (activeTypes.length > 0) {
result = result.filter((d) => activeTypes.includes(d.document_type));
}
return result;
}, [documents, activeTypes]);
// Display filtered results
const displayDocs = filtered;
const displayTotal = activeTypes.length > 0 ? filtered.length : total;
// Display server-filtered results directly
const displayDocs = documents || [];
const displayTotal = total;
const pageStart = pageIndex * pageSize;
const pageEnd = Math.min(pageStart + pageSize, displayTotal);
@ -88,11 +107,16 @@ export default function DocumentsTable() {
const refreshCurrentView = useCallback(async () => {
if (debouncedSearch.trim()) {
await searchDocuments?.(debouncedSearch, pageIndex, pageSize);
await searchDocuments?.(
debouncedSearch,
pageIndex,
pageSize,
activeTypes.length > 0 ? activeTypes : undefined
);
} else {
await fetchDocuments?.(pageIndex, pageSize);
await fetchDocuments?.(pageIndex, pageSize, activeTypes.length > 0 ? activeTypes : undefined);
}
}, [debouncedSearch, pageIndex, pageSize, searchDocuments, fetchDocuments]);
}, [debouncedSearch, pageIndex, pageSize, activeTypes, searchDocuments, fetchDocuments]);
const onBulkDelete = async () => {
if (selectedIds.size === 0) {
@ -133,8 +157,7 @@ export default function DocumentsTable() {
className="w-full px-6 py-4"
>
<DocumentsFilters
allDocuments={documents || []}
visibleDocuments={displayDocs}
typeCounts={typeCounts}
selectedIds={selectedIds}
onSearch={setSearch}
searchValue={search}