"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 (
);
}
/**
* 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(null);
// The folder whose file list is being fetched for Import.
const [fetching, setFetching] = useState(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 && (
{
close();
router.refresh();
}}
/>
)}
{!open && folders.length > 0 && (
Found in the models folder
{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.