"use client"; import { DownloadIcon, ShieldCheckIcon, WrenchIcon } from "lucide-react"; import Link from "next/link"; import { useState } from "react"; import { cancelJob, startDownload, startRepair, startVerify } from "@/components/job-actions"; import { JobProgress, JobStateBadge } from "@/components/job-progress"; import { Button } from "@/components/ui/button"; import { Progress } from "@/components/ui/progress"; import { VerifyResult } from "@/components/verify-result"; import { useLive } from "@/hooks/use-live"; import type { ModelStatus } from "@/lib/db/schema"; import { groupJobFiles } from "@/lib/file-groups"; import { formatBytes, percent } from "@/lib/format"; import { jobLabel } from "@/lib/job-labels"; const OPEN = ["queued", "downloading", "verifying"]; const VERIFY_KINDS = ["verify", "deep_verify"]; /** Sidebar card: current job with progress, the last verify result, and the model's actions. */ export function DownloadCard({ modelId, status, hubMissing, }: { modelId: string; status: ModelStatus; /** The repo or pinned commit is gone from the Hub; no new downloads. */ hubMissing: boolean; }) { const { snapshot } = useLive({ model: modelId }); const [busy, setBusy] = useState(false); const jobs = snapshot?.jobs ?? []; const latest = jobs[0]; const open = latest && OPEN.includes(latest.state) ? latest : undefined; const files = open ? groupJobFiles(snapshot?.files ?? []) : []; // The newest verify, unless a download ran after it (its result would be stale). const lastCheck = jobs.find((j) => VERIFY_KINDS.includes(j.kind) || j.kind === "download"); const verify = lastCheck && VERIFY_KINDS.includes(lastCheck.kind) && lastCheck.state === "completed" ? lastCheck : undefined; const verifyProblems = verify?.result ? (verify.result.missing?.length ?? 0) + (verify.result.sizeMismatch?.length ?? 0) + (verify.result.hashMismatch?.length ?? 0) : 0; async function run(fn: () => Promise) { setBusy(true); try { await fn(); } finally { setBusy(false); } } const neverRan = jobs.length === 0; const downloadLabel = neverRan ? "Download" : latest?.state === "interrupted" || latest?.state === "cancelled" ? "Resume" : latest?.state === "failed" && latest.kind === "download" ? "Retry" : "Download missing"; const canDownload = !open && !hubMissing && status !== "ready" && status !== "deleting" && !!snapshot; const canVerify = !open && !neverRan && status !== "deleting" && !!snapshot; const needsRepair = !open && !hubMissing && status !== "deleting" && (verifyProblems > 0 || status === "incomplete"); let hint = "Downloads the selected files at the pinned commit."; if (open?.state === "queued") hint = snapshot?.worker.online ? "Waiting for a free slot." : "Queued, but the worker is offline."; else if (open) hint = "Runs in the background; you can close this page."; else if (status === "deleting") hint = "Deleting…"; else if (hubMissing && status !== "ready") hint = "Can't download: no longer available on Hugging Face. Local files are kept."; else if (status === "ready") hint = "All selected files are present."; const showProgress = latest && latest.kind !== "delete" && !(latest.state === "completed" && (status === "ready" || VERIFY_KINDS.includes(latest.kind))); return (

{open ? jobLabel(open) : "Download"}

{latest && }

{hint}

{showProgress && } {latest?.state === "failed" && latest.errorMessage && (
          {latest.errorMessage}
        
)} {files.length > 0 && (
    {files.map((f) => (
  • {f.path} {f.parts > 1 && · {f.parts} parts} {formatBytes(f.expectedBytes)}
  • ))}
)} {verify?.result && !open && (
)}
{open ? ( ) : ( <> {needsRepair ? ( ) : ( canDownload && ( ) )} {canVerify && ( <> )} )} {latest && latest.kind !== "delete" && ( View log )}
); }