File size: 4,220 Bytes
2471af3
 
 
 
 
0953253
 
2471af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0953253
 
2471af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0953253
 
2471af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0953253
2471af3
 
 
 
 
 
 
 
 
 
 
 
0953253
2471af3
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { useMemo, useState } from "react"
import { ArrowDown, ArrowUp, Search } from "lucide-react"
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import { Input } from "@/components/ui/input"
import { Button } from "@/components/ui/button"
import type { Author, Repo } from "@/lib/types"
import { relName, T } from "@/lib/i18n"
import { num as fmt, hfUrl } from "@/lib/format"
import { RelDot } from "@/components/rel"
import { Who } from "@/components/who"

type Key = "id" | "rel" | "dl30" | "dlall" | "d1"

export function ModelTable({ repos, authors, hasDaily }: { repos: Repo[]; authors: Record<string, Author>; hasDaily: boolean }) {
  const [q, setQ] = useState("")
  const [sort, setSort] = useState<{ k: Key; dir: 1 | -1 }>({ k: "dl30", dir: -1 })
  const [shown, setShown] = useState(20)

  const rows = useMemo(() => {
    const s = q.trim().toLowerCase()
    const list = s ? repos.filter((r) => r.id.toLowerCase().includes(s)) : repos
    return [...list].sort((a, b) => {
      const x = a[sort.k], y = b[sort.k]
      if (x == null || y == null) return (x == null ? 1 : 0) - (y == null ? 1 : 0)
      return (x < y ? -1 : x > y ? 1 : 0) * sort.dir
    })
  }, [repos, q, sort])

  const cols: [Key, string, boolean][] = [
    ["id", T.model, false], ["rel", T.type, false], ["dl30", T.days30, true], ["dlall", T.allTime, true],
    ...(hasDaily ? ([["d1", T.lastDay, true]] as [Key, string, boolean][]) : []),
  ]
  const head = (k: Key, label: string, num: boolean) => (
    <TableHead key={k} className={`h-11 text-[15px] ${num ? "text-right" : ""}`}>
      <button className={`inline-flex items-center gap-1 text-muted-foreground hover:text-foreground ${sort.k === k ? "text-foreground" : ""}`}
              onClick={() => setSort((s) => ({ k, dir: s.k === k ? (-s.dir as 1 | -1) : -1 }))}>
        {label}
        {sort.k === k && (sort.dir < 0 ? <ArrowDown className="size-4" /> : <ArrowUp className="size-4" />)}
      </button>
    </TableHead>
  )

  return (
    <div>
      <div className="relative mb-4 max-w-sm">
        <Search className="pointer-events-none absolute top-1/2 left-3.5 size-4 -translate-y-1/2 text-muted-foreground" />
        <Input value={q} onChange={(e) => { setQ(e.target.value); setShown(20) }} placeholder={T.search}
               className="h-11 rounded-full pl-10 text-[15px]" aria-label={T.search} />
      </div>
      <Table className="text-[15px]">
        <TableHeader>
          <TableRow className="border-border/60 hover:bg-transparent">{cols.map(([k, l, n]) => head(k, l, n))}</TableRow>
        </TableHeader>
        <TableBody>
          {rows.slice(0, shown).map((r) => {
            const [owner, name] = r.id.split("/")
            return (
              <TableRow key={r.id} className="border-border/60">
                <TableCell className="py-3">
                  <a href={hfUrl(r.id)} target="_blank" rel="noopener" className="flex min-w-0 items-center gap-3 hover:underline">
                    <Who name={owner} authors={authors} size="size-7" />
                    <span className="max-w-[min(52vw,520px)] truncate" title={r.id}>
                      <span className="text-muted-foreground">{owner}/</span>{name}
                    </span>
                  </a>
                </TableCell>
                <TableCell className="py-3 whitespace-nowrap">
                  <span className="inline-flex items-center gap-2"><RelDot rel={r.rel} />{relName(r.rel)}</span>
                </TableCell>
                <TableCell className="py-3 text-right tabular-nums">{fmt(r.dl30)}</TableCell>
                <TableCell className="py-3 text-right tabular-nums">{fmt(r.dlall)}</TableCell>
                {hasDaily && <TableCell className="py-3 text-right tabular-nums">{fmt(r.d1)}</TableCell>}
              </TableRow>
            )
          })}
        </TableBody>
      </Table>
      {rows.length > shown && (
        <div className="mt-5 flex justify-center">
          <Button variant="secondary" className="h-10 rounded-full px-6 text-[15px]" onClick={() => setShown((n) => n + 50)}>
            {T.showMore(50)}
          </Button>
        </div>
      )}
    </div>
  )
}