"use client"; import { ChevronLeftIcon, ChevronRightIcon, ChevronsLeftIcon, ChevronsRightIcon, 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 { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { NativeSelect } from "@/components/ui/native-select"; 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 { EMPTY_FILTERS, type Facet, FACETS, facetCounts, type Filters, matchesFilters, matchesQuery, queryTerms, type SearchableModel, searchText, } from "@/lib/search"; import { FacetFilter } from "./facet-filter"; const OPEN = ["queued", "downloading", "verifying"]; const PAGE_SIZES = [10, 25, 50, 100]; 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 { const m: SearchableModel = { 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, }; return { ...m, text: searchText(m) }; } /** 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 [page, setPage] = useState(0); const [pageSize, setPageSize] = useState(25); 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 }); } // A new search or filter starts on the first page. const updateQuery = (q: string) => { setQuery(q); setPage(0); sync(q, filters); }; const updateFilter = (facet: Facet, values: string[]) => { const next = { ...filters, [facet]: values }; setFilters(next); setPage(0); sync(query, next); }; const clearAll = () => { setQuery(""); setFilters(EMPTY_FILTERS); setPage(0); sync("", EMPTY_FILTERS); }; // Search text and counts are computed once per change of rows, query or filters, not on every // render (live job progress re-renders this table while downloads run). 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 terms = useMemo(() => queryTerms(query), [query]); const visible = useMemo( () => rows.filter((_, i) => matchesQuery(searchable[i], terms) && matchesFilters(searchable[i], filters)), [rows, searchable, terms, filters], ); const facetOptions = useMemo( () => Object.fromEntries(FACETS.map((facet) => [facet, facetCounts(searchable, facet, terms, filters)])) as Record, [searchable, terms, filters], ); const anyActive = query.trim() !== "" || FACETS.some((f) => filters[f].length > 0); const pages = Math.max(1, Math.ceil(visible.length / pageSize)); const current = Math.min(page, pages - 1); const shown = visible.slice(current * pageSize, (current + 1) * pageSize); 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 ( // Glass: the painting from the hero shows through, blurred. {/* Toolbar: title, search and filters, part of the table. */}

Models

{anyActive ? `${visible.length} of ${rows.length}` : rows.length}
{/* Same height and border as the filter pills. */}
updateQuery(e.target.value)} placeholder="Search models…" className="h-8 bg-transparent pl-8 text-sm" aria-label="Search models" />
{FACETS.map((facet) => ( updateFilter(facet, v)} format={facet === "status" ? statusLabel : undefined} marker={facet === "tag" ? (v) => : undefined} /> ))} {anyActive && ( )}
{visible.length === 0 ? (

No matching models

Try a different search or remove a filter.

) : ( <> Model Selection Size Pinned Downloaded Status {shown.map((r) => { const job = openJobs.get(r.id); const base = r.baseModels[0]; return ( {r.author}/ {r.repoId.slice(r.author.length + 1)}
{base && base !== r.repoId && base {base}} {r.architecture && {r.architecture}} {r.tags.map((t) => ( ))}
{r.quants.length > 0 ? ( {r.quants.map((q) => ( {q} ))} ) : ( {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 )} )}
); })}
{current * pageSize + 1}–{Math.min(visible.length, (current + 1) * pageSize)} of {visible.length}
Page {current + 1} of {pages}
)}
); }