Files
Kaname/src/lib/local-folders.ts
T
Claude 7239ba2692 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
2026-11-01 20:49:18 +00:00

36 lines
1.6 KiB
TypeScript

import "server-only";
import { asc, eq, notInArray } from "drizzle-orm";
import { db } from "@/lib/db/client";
import { localFolders, models, workerState } from "@/lib/db/schema";
export type LocalFolder = typeof localFolders.$inferSelect;
export type LocalFolderSummary = Pick<LocalFolder, "path" | "commit" | "fileCount" | "totalBytes">;
/**
* Folders in MODELS_DIR that no model uses (from the worker's last scan), without their file lists:
* those can hold thousands of entries and are only needed once you click Import (getLocalFolder).
*/
export async function listLocalFolders(): Promise<LocalFolderSummary[]> {
const d = db();
return d
.select({ path: localFolders.path, commit: localFolders.commit, fileCount: localFolders.fileCount, totalBytes: localFolders.totalBytes })
.from(localFolders)
.where(notInArray(localFolders.path, d.select({ p: models.localPath }).from(models)))
.orderBy(asc(localFolders.path));
}
/** One folder with its file list. */
export async function getLocalFolder(path: string): Promise<LocalFolder | undefined> {
return db().query.localFolders.findFirst({ where: eq(localFolders.path, path) });
}
export async function localFolderExists(path: string): Promise<boolean> {
const row = await db().query.localFolders.findFirst({ columns: { path: true }, where: eq(localFolders.path, path) });
return !!row;
}
/** Ask the worker to scan MODELS_DIR now (it picks this up with its next heartbeat). */
export async function requestFolderScan(): Promise<void> {
await db().update(workerState).set({ scanRequestedAt: new Date() }).where(eq(workerState.id, "worker"));
}