import { useMemo, useState } from "react" import { Bar, BarChart, CartesianGrid, Line, LineChart, XAxis, YAxis } from "recharts" import { ChartContainer, ChartTooltip, ChartTooltipContent, type ChartConfig } from "@/components/ui/chart" import { REL_NAME, type Lineage, type Rel } from "@/lib/types" import { relConfig, RelLegend, axisTick } from "@/components/rel" import { md } from "@/lib/format" const day = (d: Date) => d.toISOString().slice(0, 10) const addDays = (s: string, n: number) => { const d = new Date(s + "T00:00:00Z") d.setUTCDate(d.getUTCDate() + n) return day(d) } /** Running total of each kind of derivative, from the creation dates (full history). */ export function GrowthLines({ L, rel, today }: { L: Lineage; rel: Rel[]; today: string }) { const { rows, present } = useMemo(() => { const present = rel.filter((r) => L.by_relation[r].n > 0) if (!L.growth.length) return { rows: [], present } const byDate = new Map>>() for (const g of L.growth) { const m = byDate.get(g.date) ?? {} m[g.rel] = (m[g.rel] ?? 0) + g.n byDate.set(g.date, m) } const start = [...byDate.keys()].sort()[0] const run: Record = Object.fromEntries(present.map((r) => [r, 0])) const rows: Record[] = [] for (let d = start; d <= today; d = addDays(d, 1)) { for (const [r, n] of Object.entries(byDate.get(d) ?? {})) run[r] = (run[r] ?? 0) + n rows.push({ date: d, ...run }) } // Long histories are drawn from one point a week, which keeps the curves smooth. const step = rows.length > 90 ? 7 : 1 const thin = rows.filter((_, i) => i % step === 0 || i === rows.length - 1) return { rows: thin, present } }, [L, rel, today]) 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 n.add(r); return n }) return ( <>