"use client"; import { RocketIcon, SearchIcon, XIcon } from "lucide-react"; import Link from "next/link"; import { usePathname, useRouter, useSearchParams } from "next/navigation"; import { useMemo, useState } from "react"; import { JobProgress, JobStateBadge } from "@/components/job-progress"; import { LocalTime } from "@/components/local-time"; import { HUB_STATE_LABEL, HubStateBadge } from "@/components/hub-state-badge"; import { MODEL_STATUS, ModelStatusBadge } from "@/components/model-status-badge"; import { TagBadge, TagDot } from "@/components/tag-badge"; import { Badge } from "@/components/ui/badge"; import { Button, buttonVariants } from "@/components/ui/button"; import { Card, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { useLive } from "@/hooks/use-live"; import type { ModelStatus } from "@/lib/db/schema"; import { formatBytes, shortSha } from "@/lib/format"; import type { JobSnapshot } from "@/lib/jobs"; import type { LibraryRow } from "@/lib/library"; import { cn } from "@/lib/utils"; import { EMPTY_FILTERS, type Facet, FACETS, facetCounts, type Filters, matchesFilters, matchesQuery, type SearchableModel, } from "@/lib/search"; import { FacetFilter } from "./facet-filter"; const OPEN = ["queued", "downloading", "verifying"]; const FACET_LABEL: Record = { author: "Author", family: "Family", quant: "Quant", tag: "Tag", status: "Status", }; const statusLabel = (v: string) => MODEL_STATUS[v as ModelStatus]?.label ?? v; function toSearchable(r: LibraryRow): SearchableModel { return { repoId: r.repoId, author: r.author, families: r.families, baseModels: r.baseModels, architecture: r.architecture, quants: r.quants, tags: r.tags.map((t) => t.name), hubTags: r.hubTags, pipelineTag: r.pipelineTag, license: r.license, status: r.status, hubStatus: r.hubState && r.hubState !== "available" ? HUB_STATE_LABEL[r.hubState].label : r.updateAvailable ? "update available" : null, }; } /** Search + facet filters (kept in the URL) over the library table. */ export function LibraryView({ rows }: { rows: LibraryRow[] }) { const router = useRouter(); const pathname = usePathname(); const params = useSearchParams(); const [query, setQuery] = useState(() => params.get("q") ?? ""); const [filters, setFilters] = useState(() => { const f = { ...EMPTY_FILTERS }; for (const facet of FACETS) f[facet] = params.getAll(facet); return f; }); function sync(nextQuery: string, nextFilters: Filters) { const p = new URLSearchParams(); if (nextQuery) p.set("q", nextQuery); for (const facet of FACETS) for (const v of nextFilters[facet]) p.append(facet, v); const qs = p.toString(); router.replace(qs ? `${pathname}?${qs}` : pathname, { scroll: false }); } const updateQuery = (q: string) => { setQuery(q); sync(q, filters); }; const updateFilter = (facet: Facet, values: string[]) => { const next = { ...filters, [facet]: values }; setFilters(next); sync(query, next); }; const clearAll = () => { setQuery(""); setFilters(EMPTY_FILTERS); sync("", EMPTY_FILTERS); }; const searchable = useMemo(() => rows.map(toSearchable), [rows]); const tagColors = useMemo(() => new Map(rows.flatMap((r) => r.tags.map((t) => [t.name, t.color] as const))), [rows]); const visible = rows.filter((_, i) => matchesQuery(searchable[i], query) && matchesFilters(searchable[i], filters)); const anyActive = query.trim() !== "" || FACETS.some((f) => filters[f].length > 0); const { snapshot } = useLive(); const openJobs = new Map(); for (const j of snapshot?.jobs ?? []) { if (OPEN.includes(j.state) && !openJobs.has(j.modelId)) openJobs.set(j.modelId, j); } return (
updateQuery(e.target.value)} placeholder="Search repo, author, family, quant, tag…" className="pl-8" aria-label="Search models" />
{FACETS.map((facet) => ( updateFilter(facet, v)} format={facet === "status" ? statusLabel : undefined} marker={facet === "tag" ? (v) => : undefined} /> ))}
{anyActive && (
{visible.length} of {rows.length} models {FACETS.flatMap((facet) => filters[facet].map((v) => ( {facet === "tag" && } {FACET_LABEL[facet]}: {facet === "status" ? statusLabel(v) : v} )), )}
)} {visible.length === 0 ? ( No matching models Try a different search or remove a filter. ) : ( Model Selection Size Pinned Downloaded Status Deploy {visible.map((r) => { const job = openJobs.get(r.id); const base = r.baseModels[0]; return ( {r.repoId}
{base && base !== r.repoId && base {base}} {r.architecture && {r.architecture}} {r.deployments.map((d) => ( {d.name} ))} {r.tags.map((t) => ( ))}
{r.quants.length > 0 ? r.quants.join(", ") : `${r.fileCount} file${r.fileCount === 1 ? "" : "s"}`} {formatBytes(r.totalBytes)} {r.trackedRevision}@{shortSha(r.pinnedSha)} {job ? ( {job.state !== "queued" && } ) : ( {r.updateAvailable && (r.hubState === "available" || !r.hubState) && ( update available )} )} {r.deployable && ( Deploy )}
); })}
)}
); }