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>(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 ( <>
{keys.map((k) => { const off = hidden.has(k) return ( ) })}
{keys.map((k) => ( ))} num(Number(v))} /> mdy(isoDay(Number(p?.[0]?.payload?.t)))} formatter={(v, k) => ( {config[k as Key].label} {num(Number(v))} )} />} /> {keys.filter((k) => !hidden.has(k)).map((k) => ( ))} ) }