Library: load import folder files on demand, memoize search

The library page passed every importable folder's full file list (up to 5,000
entries each, from the worker's scan) into a client component, though it is
only used after clicking Import. With three such folders that was 84% of a
1.3 MB page, re-sent on every refresh after a job finished.

- listLocalFolders() returns path, commit and counts only; the Import button
  fetches one folder's files from GET /api/local-folders?path=…
- addModel() checks that an import folder exists without loading its files.
- LibraryView builds each model's search text once and memoizes the visible
  rows and facet counts; the facet sort uses one Intl.Collator.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EooZ9biu7kFNB4eDJrwQc5
This commit is contained in:
Claude committed 2026-11-01 20:49:18 +00:00
1 parent 76d851896e
commit 7239ba2692
9 files changed
+110 -31

No files matched your search

+16 -3
View File
@@ -27,7 +27,9 @@ import {
type Filters,
matchesFilters,
matchesQuery,
queryTerms,
type SearchableModel,
searchText,
} from "@/lib/search";
import { FacetFilter } from "./facet-filter";
@@ -45,7 +47,7 @@ const FACET_LABEL: Record<Facet, string> = {
const statusLabel = (v: string) => MODEL_STATUS[v as ModelStatus]?.label ?? v;
function toSearchable(r: LibraryRow): SearchableModel {
return {
const m: SearchableModel = {
repoId: r.repoId,
author: r.author,
families: r.families,
@@ -64,6 +66,7 @@ function toSearchable(r: LibraryRow): SearchableModel {
? "update available"
: null,
};
return { ...m, text: searchText(m) };
}
/** Search + facet filters (kept in the URL) over the library table. */
@@ -106,9 +109,19 @@ export function LibraryView({ rows }: { rows: LibraryRow[] }) {
sync("", EMPTY_FILTERS);
};
// Search text and counts are computed once per change of rows, query or filters, not on every
// render (live job progress re-renders this table while downloads run).
const searchable = useMemo(() => rows.map(toSearchable), [rows]);
const tagColors = useMemo(() => new Map(rows.flatMap((r) => r.tags.map((t) => [t.name, t.color] as const))), [rows]);
const visible = rows.filter((_, i) => matchesQuery(searchable[i], query) && matchesFilters(searchable[i], filters));
const terms = useMemo(() => queryTerms(query), [query]);
const visible = useMemo(
() => rows.filter((_, i) => matchesQuery(searchable[i], terms) && matchesFilters(searchable[i], filters)),
[rows, searchable, terms, filters],
);
const facetOptions = useMemo(
() => Object.fromEntries(FACETS.map((facet) => [facet, facetCounts(searchable, facet, terms, filters)])) as Record<Facet, [string, number][]>,
[searchable, terms, filters],
);
const anyActive = query.trim() !== "" || FACETS.some((f) => filters[f].length > 0);
const pages = Math.max(1, Math.ceil(visible.length / pageSize));
const current = Math.min(page, pages - 1);
@@ -147,7 +160,7 @@ export function LibraryView({ rows }: { rows: LibraryRow[] }) {
<FacetFilter
key={facet}
label={FACET_LABEL[facet]}
values={facetCounts(searchable, facet, query, filters)}
values={facetOptions[facet]}
selected={filters[facet]}
onChange={(v) => updateFilter(facet, v)}
format={facet === "status" ? statusLabel : undefined}