import { useMemo, useState } from "react" import { Area, AreaChart, CartesianGrid, XAxis, YAxis } from "recharts" import { ChartContainer, ChartTooltip } from "@/components/ui/chart" import { REL_NAME, type Lineage, type Rel } from "@/lib/types" import { isoDay, mdy, num, toTime } from "@/lib/format" import { timeTicks } from "@/lib/ticks" import { Panel, Section } from "@/components/layout" import { axisTick, relColor, relConfig, RelLegend } from "@/components/rel" /** * Downloads per day of the community models, stacked by kind, about a week per point (the * hub-stats history, then our snapshots); drawn like the other time charts on the page. */ export function DailyLines({ L, rel, download }: { L: Lineage; rel: Rel[]; download?: string }) { const { rows, present, thin } = useMemo(() => { const days = [...new Set(L.daily.map((d) => d.day))].sort() // biggest kinds at the bottom of the stack const total = (r: Rel) => L.daily.reduce((a, d) => a + (d.rel === r ? d.n : 0), 0) const present = rel.filter((r) => total(r) > 0).sort((a, b) => total(b) - total(a)) const at = new Map(L.daily.map((d) => [`${d.day}|${d.rel}`, d.n])) const rows = days.map((day) => ({ t: toTime(day), ...Object.fromEntries(present.map((r) => [r, at.get(`${day}|${r}`) ?? 0])) })) // a sliver of the total gets no outline, or it would trace the top of the stack in its colour const all = present.reduce((a, r) => a + total(r), 0) || 1 const thin = new Set(present.filter((r) => total(r) / all < 0.02)) return { rows, present, thin } }, [L, rel]) const [hidden, setHidden] = useState>(new Set()) const toggle = (r: Rel) => setHidden((h) => { const n = new Set(h) if (n.has(r)) n.delete(r) else if (n.size < present.length - 1) n.add(r) return n }) const { ticks, label } = useMemo(() => (rows.length ? timeTicks(rows[0].t, rows.at(-1)!.t) : { ticks: [], label: String }), [rows]) if (rows.length < 2) return null const shown = present.filter((r) => !hidden.has(r)) return (
rel.indexOf(a) - rel.indexOf(b))} hidden={hidden} onToggle={toggle} /> {present.map((r) => ( ))} num(Number(v))} /> { if (!active || !payload?.length) return null const row = payload[0].payload as Record const items = [...shown].reverse().filter((r) => row[r] > 0) return (
{mdy(isoDay(row.t))}
{num(shown.reduce((a, r) => a + (row[r] ?? 0), 0))}
{items.map((r) => (
{REL_NAME[r]} {num(row[r])}
))}
) }} /> {shown.map((r) => ( ))}
) }