File size: 4,471 Bytes
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
import { useMemo, useState } from "react"
import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from "recharts"
import { ChartContainer, ChartTooltip, ChartTooltipContent, type ChartConfig } from "@/components/ui/chart"
import { axisTick } from "@/components/rel"
import { isoDay, mdy, num, toTime } from "@/lib/format"
import { timeTicks } from "@/lib/ticks"
import { T } from "@/lib/i18n"

type Key = "comm" | "off"

/**
 * All-time downloads over time, like a star history: the community models built on a release,
 * and the release itself. Points are about a week apart (hub-stats history, then our snapshots).
 */
export function DownloadHistory({ history, name, color }: { history: [string, number, number][]; name: string; color: string }) {
  const rows = useMemo(() => history.map(([d, off, comm]) => ({ t: toTime(d), off, comm })), [history])
  const [hidden, setHidden] = useState<Set<Key>>(new Set())
  const config = {
    comm: { label: T.communityDownloads.replace(/^./, (c) => c.toUpperCase()), color },
    off: { label: T.releaseDownloads(name), color: "var(--foreground)" },
  } satisfies ChartConfig
  const keys: Key[] = ["comm", "off"]

  const { ticks, label } = useMemo(() => timeTicks(rows[0].t, rows.at(-1)!.t), [rows])

  const toggle = (k: Key) => setHidden((h) => {
    const n = new Set(h)
    if (n.has(k)) n.delete(k)
    else if (n.size < keys.length - 1) n.add(k)
    return n
  })

  return (
    <>
      <div className="mb-4 flex flex-wrap gap-x-5 gap-y-2 text-[15px] text-muted-foreground">
        {keys.map((k) => {
          const off = hidden.has(k)
          return (
            <button key={k} type="button" aria-pressed={!off} onClick={() => toggle(k)}
                    className="inline-flex items-center gap-2 rounded-md hover:text-foreground">
              <span className={`inline-block size-2.5 shrink-0 rounded-full ${off ? "border border-muted-foreground" : ""}`}
                    style={{ background: off ? "transparent" : config[k].color }} />
              <span className={off ? "line-through opacity-60" : ""}>{config[k].label}</span>
              <span className="font-medium text-foreground tabular-nums">{num(rows.at(-1)![k])}</span>
            </button>
          )
        })}
      </div>
      <ChartContainer config={config} className="aspect-auto h-[360px] w-full">
        <AreaChart data={rows} margin={{ left: 4, right: 12, top: 8 }}>
          <defs>
            {keys.map((k) => (
              <linearGradient key={k} id={`dlh-${k}`} x1="0" y1="0" x2="0" y2="1">
                <stop offset="0" stopColor={`var(--color-${k})`} stopOpacity={k === "comm" ? 0.35 : 0.12} />
                <stop offset="1" stopColor={`var(--color-${k})`} stopOpacity={0.02} />
              </linearGradient>
            ))}
          </defs>
          <CartesianGrid vertical={false} />
          <XAxis dataKey="t" type="number" scale="time" domain={["dataMin", "dataMax"]} ticks={ticks}
                 tickFormatter={label} tickLine={false} axisLine={false} tick={axisTick} />
          <YAxis tickLine={false} axisLine={false} tick={axisTick} width={56} tickFormatter={(v) => num(Number(v))} />
          <ChartTooltip content={<ChartTooltipContent labelFormatter={(_, p) => mdy(isoDay(Number(p?.[0]?.payload?.t)))}
                                                      formatter={(v, k) => (
                                                        <span className="flex w-full items-center justify-between gap-6">
                                                          <span className="flex items-center gap-2 text-muted-foreground">
                                                            <span className="size-2.5 rounded-full" style={{ background: config[k as Key].color }} />
                                                            {config[k as Key].label}
                                                          </span>
                                                          <span className="font-medium tabular-nums">{num(Number(v))}</span>
                                                        </span>)} />} />
          {keys.filter((k) => !hidden.has(k)).map((k) => (
            <Area key={k} dataKey={k} type="monotone" stroke={`var(--color-${k})`} strokeWidth={k === "comm" ? 2.5 : 2}
                  fill={`url(#dlh-${k})`} dot={false} animationDuration={1100} animationEasing="ease-out" />
          ))}
        </AreaChart>
      </ChartContainer>
    </>
  )
}