Files
Kaname/src/app/(app)/add-model-panel.tsx
T
Pascal LinxweilerandClaude Opus 5.5 80ccf800df Import folders without a Hub repo as local models
The Import form gets "Add as local model": every file in the folder,
kept as it is, named after the folder. A new models.source column
(hub | local) marks them; local models skip Hub checks, metadata,
downloads, updates and deep verify, and Verify checks existence and
size only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 00:14:56 +01:00

144 lines
5.1 KiB
TypeScript

"use client";
import { FolderInputIcon, RefreshCwIcon } from "lucide-react";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { Card, CardAction, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { formatBytes, shortSha } from "@/lib/format";
import { cn } from "@/lib/utils";
import { AddModelForm, type ImportFolder } from "./add-model-form";
/** 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 }) {
return (
<Button variant={open ? "outline" : "default"} onClick={onToggle}>
{open ? "Close" : "Add model"}
</Button>
);
}
/**
* The lookup/selection form, opened inline by the "Add model" toggle, and the folders in the models
* folder that aren't in the library yet, each with "Import".
*/
export function AddModelPanel({
freeBytes,
folders,
open,
setOpen,
}: {
freeBytes: number | null;
folders: Folder[];
open: boolean;
setOpen: (open: boolean) => void;
}) {
const router = useRouter();
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) {
setWasOpen(open);
if (!open) setImporting(null);
}
const [scanning, setScanning] = useState(false);
function close() {
setOpen(false);
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 {
const res = await fetch("/api/local-folders/scan", { method: "POST" });
if (!res.ok) {
toast.error(`Could not start a scan (${res.status})`);
return;
}
// The worker picks the request up with its next heartbeat (every 5 s).
await new Promise((r) => setTimeout(r, 7_000));
router.refresh();
} finally {
setScanning(false);
}
}
return (
<>
{open && (
<AddModelForm
key={importing?.path ?? "new"}
freeBytes={freeBytes}
importFolder={importing ?? undefined}
onAdded={() => {
close();
router.refresh();
}}
/>
)}
{!open && folders.length > 0 && (
<Card size="sm">
<CardHeader>
<CardTitle className="flex items-center gap-2">
<FolderInputIcon className="size-4 text-muted-foreground" />
Found in the models folder
</CardTitle>
<CardDescription>
{folders.length} folder{folders.length === 1 ? " isn't" : "s aren't"} in the library yet. Import matches
{folders.length === 1 ? " it" : " them"} with the Hub repo and verifies the files instead of downloading them again,
or adds {folders.length === 1 ? "it" : "them"} as a local model if there is no Hub repo.
</CardDescription>
<CardAction>
<Button variant="ghost" size="sm" onClick={rescan} disabled={scanning}>
<RefreshCwIcon className={cn(scanning && "animate-spin")} />
{scanning ? "Scanning…" : "Rescan"}
</Button>
</CardAction>
</CardHeader>
<CardContent>
<ul className="divide-y rounded-lg ring-1 ring-foreground/10">
{folders.map((f) => (
<li key={f.path} className="flex flex-wrap items-center gap-x-4 gap-y-1 px-3 py-2">
<span className="min-w-0 flex-1 truncate font-mono text-sm" title={f.path}>
{f.path}
</span>
<span className="text-xs text-muted-foreground tabular-nums">
{f.fileCount} file{f.fileCount === 1 ? "" : "s"} · {formatBytes(f.totalBytes)}
{f.commit && <> · hf commit {shortSha(f.commit)}</>}
</span>
<Button size="sm" variant="outline" disabled={fetching != null} onClick={() => void startImport(f)}>
{fetching === f.path ? "Loading…" : "Import"}
</Button>
</li>
))}
</ul>
</CardContent>
</Card>
)}
</>
);
}