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

+23 -11
View File
@@ -10,7 +10,8 @@ import { formatBytes, shortSha } from "@/lib/format";
import { cn } from "@/lib/utils";
import { AddModelForm, type ImportFolder } from "./add-model-form";
type Folder = ImportFolder & { totalBytes: number };
/** A folder found by the scan, without its file list (fetched when you click Import). */
type Folder = { path: string; commit: string | null; fileCount: number; totalBytes: number };
/** The "Add model" / "Close" toggle (shown in the library hero). */
export function AddModelToggle({ open, onToggle }: { open: boolean; onToggle: () => void }) {
@@ -37,7 +38,9 @@ export function AddModelPanel({
setOpen: (open: boolean) => void;
}) {
const router = useRouter();
const [importing, setImporting] = useState<Folder | null>(null);
const [importing, setImporting] = useState<ImportFolder | null>(null);
// The folder whose file list is being fetched for Import.
const [fetching, setFetching] = useState<string | null>(null);
// Closed from outside (the toggle in the hero): forget the folder being imported.
const [wasOpen, setWasOpen] = useState(open);
if (open !== wasOpen) {
@@ -51,6 +54,22 @@ export function AddModelPanel({
setImporting(null);
}
async function startImport(folder: Folder) {
setFetching(folder.path);
try {
const res = await fetch(`/api/local-folders?path=${encodeURIComponent(folder.path)}`, { cache: "no-store" });
const body = await res.json().catch(() => ({}));
if (!res.ok) {
toast.error(body.error?.message ?? `Could not load the folder (${res.status})`);
return;
}
setImporting(body as ImportFolder);
setOpen(true);
} finally {
setFetching(null);
}
}
async function rescan() {
setScanning(true);
try {
@@ -109,15 +128,8 @@ export function AddModelPanel({
{f.fileCount} file{f.fileCount === 1 ? "" : "s"} · {formatBytes(f.totalBytes)}
{f.commit && <> · hf commit {shortSha(f.commit)}</>}
</span>
<Button
size="sm"
variant="outline"
onClick={() => {
setImporting(f);
setOpen(true);
}}
>
Import
<Button size="sm" variant="outline" disabled={fetching != null} onClick={() => void startImport(f)}>
{fetching === f.path ? "Loading…" : "Import"}
</Button>
</li>
))}