"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";
type Folder = ImportFolder & { 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);
// 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 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.
{folders.map((f) => (
-
{f.path}
{f.fileCount} file{f.fileCount === 1 ? "" : "s"} · {formatBytes(f.totalBytes)}
{f.commit && <> · hf commit {shortSha(f.commit)}>}
))}
)}
>
);
}