import { useEffect, useMemo, useRef, useState } from "react" import { area, curveMonotoneX, line, stack } from "d3-shape" import { scaleLinear } from "d3-scale" import type { Release, Site } from "@/lib/types" import { CAT_ORDER, catColor, shortName } from "@/lib/category" import { DAY, full, md, mdy, num, toTime } from "@/lib/format" type Row = { t: number } & Record export type Series = { rows: Row[]; cats: string[]; start: number; end: number } export type Metric = "models" | "downloads" /** A milestone: releases within three weeks share one pole. Names sit right of the pole, or * left of it (`side`) when the right is taken; with no room either side only the pole shows. */ type Pin = { t: number; key: string; rels: Release[]; cat: string; side: "right" | "left" | "none" } const NAME_FONT = '500 15px "Outfit Variable", system-ui, sans-serif' let measurer: CanvasRenderingContext2D | null = null const textWidth = (s: string) => { measurer ??= document.createElement("canvas").getContext("2d")! measurer.font = NAME_FONT return measurer.measureText(s).width } const REVEAL = 2000 // ms for the curve to sweep in /** A shared pin opens the release that has its own page, if one does. */ const pick = (rs: Release[]) => rs.find((r) => r.page) ?? rs[0] const LINE_H = 24 // one release name in the milestone row const ROW_TOP = 14 // centre of the first name, where every pole starts /** Cumulative community models per modality, one point a week from our first release to today. */ export function useSeries(site: Site): Series { return useMemo(() => { const first = Math.min(...site.releases.map((r) => toTime(r.date))) const d = new Date(first - 20 * DAY) const start = Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), 1) const end = toTime(site.today) const ts: number[] = [] for (let t = start; t < end; t += 7 * DAY) ts.push(t) ts.push(end) const cats = CAT_ORDER.filter((c) => site.categories.some((x) => x.key === c)) const rows: Row[] = ts.map((t) => ({ t }) as Row) for (const c of cats) { const ev = site.releases.filter((r) => r.cat === c).flatMap((r) => r.growth.map(([s, n]) => [toTime(s), n] as const)) .sort((a, b) => a[0] - b[0]) let i = 0, run = 0 for (const row of rows) { while (i < ev.length && ev[i][0] <= row.t) run += ev[i++][1] row[c] = run } } return { rows, cats, start, end } }, [site]) } /** All-time downloads by modality, of MiniMax models and everything built on them, about * weekly, on the same time axis as the models. */ export function useDownloadSeries(site: Site, base: Series): Series | null { return useMemo(() => { const d = site.dl_total if (!d?.days.length) return null const rows = d.days.map((day, i) => ({ t: toTime(day), ...Object.fromEntries(base.cats.map((c) => [c, d.cats[c]?.[i] ?? 0])) }) as Row) rows.unshift({ t: base.start, ...Object.fromEntries(base.cats.map((c) => [c, 0])) } as Row) return { rows, cats: base.cats, start: base.start, end: base.end } }, [site, base]) } /** The box sets the size (its height comes from the page, so the hero can fit the first screen). */ export function CommunityChart({ site, series, metric, hidden, onPick, className }: { site: Site; series: Series; metric: Metric; hidden: Set; onPick: (r: Release) => void; className: string }) { const box = useRef(null) const [[W, H], setSize] = useState([0, 0]) const [hover, setHover] = useState(null) const pointer = useRef("mouse") // a tap previews; only a mouse click on the plot opens a release useEffect(() => { const el = box.current if (!el) return const ro = new ResizeObserver(() => setSize([el.clientWidth, el.clientHeight])) ro.observe(el) return () => ro.disconnect() }, []) const { rows, cats, start, end } = series const last = rows.at(-1)!.t const value = metric === "models" ? (v: number) => full(Math.round(v)) : (v: number) => num(v) const tick = (v: number) => (v >= 1e6 ? num(v) : v >= 1000 ? `${v / 1000}K` : String(v)) const keys = cats.filter((c) => !hidden.has(c)) const narrow = W < 640 // Phones get the dots on the curve only; the release cards below carry the names. const m = { l: 4, r: narrow ? 44 : 60, t: narrow ? 24 : ROW_TOP + LINE_H + 40, b: 40 } const g = useMemo(() => { if (!W || !H) return null const x = scaleLinear().domain([start, end]).range([m.l, W - m.r]) const stacked = stack().keys(keys)(rows) const top = rows.map((_, i) => (stacked.length ? stacked[stacked.length - 1][i][1] : 0)) const y = scaleLinear().domain([0, Math.max(10, ...top)]).nice(5).range([H - m.b, m.t]) const areaGen = area<[number, number] & { data: Row }>().x((d) => x(d.data.t)).y0((d) => y(d[0])).y1((d) => y(d[1])) .curve(curveMonotoneX) const edge = line<[number, number] & { data: Row }>().x((d) => x(d.data.t)).y((d) => y(d[1])).curve(curveMonotoneX) const topLine = line().x((_, i) => x(rows[i].t)).y((v) => y(v)).curve(curveMonotoneX)(top) ?? "" const valueAt = (t: number) => { const i = rows.findIndex((r) => r.t >= t) if (i <= 0) return top[Math.max(0, i)] ?? 0 const a = rows[i - 1], b = rows[i] return top[i - 1] + ((top[i] - top[i - 1]) * (t - a.t)) / (b.t - a.t) } // Releases within three weeks of each other share one pole, names stacked under each other. const sorted = [...site.releases].sort((a, b) => a.date.localeCompare(b.date)) const groups: Release[][] = [] for (const r of sorted) { const last = groups.at(-1) if (last && toTime(r.date) - toTime(last[0].date) <= 21 * DAY) last.push(r) else groups.push([r]) } const width = (rs: Release[]) => Math.max(...rs.map((r) => textWidth(shortName(r.name)))) + 22 const pins: Pin[] = [] let taken = -Infinity // right edge of the last name block placed groups.forEach((rs, i) => { const t = toTime(rs[0].date), px = x(t), w = width(rs) const next = i + 1 < groups.length ? x(toTime(groups[i + 1][0].date)) : Infinity const prev = i > 0 ? x(toTime(groups[i - 1][0].date)) : -Infinity let side: Pin["side"] = "none" if (px - 5 + w < Math.min(next - 8, W + 8) && px - 5 > taken + 8) side = "right" else if (px + 5 - w > Math.max(taken, prev) + 10) side = "left" if (side === "right") taken = px - 5 + w else if (side === "left") taken = Math.max(taken, px + 5) pins.push({ t, key: rs.map((r) => r.key).join("+"), rels: rs, cat: rs[0].cat, side }) }) const ticksY = y.ticks(5) const ticksX: { t: number; label: string }[] = [] for (let d = new Date(start); d.getTime() <= end; d.setUTCMonth(d.getUTCMonth() + 3)) { const mo = d.getUTCMonth() if (mo % 3) { d.setUTCMonth(mo - (mo % 3)); continue } ticksX.push({ t: d.getTime(), label: mo === 0 ? String(d.getUTCFullYear()) : ["Jan", "Apr", "Jul", "Oct"][mo / 3] }) } // Quarters when there is room, otherwise half-years; years always win a place. const minGap = narrow ? 44 : 60 const xt: typeof ticksX = [] for (const tk of ticksX) { const px = x(tk.t) if (px < m.l + 12 || px > W - m.r - 10) continue if (narrow && !["Jan", "Jul"].includes(new Date(tk.t).toUTCString().slice(8, 11))) continue const prev = xt.at(-1) if (prev && px - x(prev.t) < minGap) { if (tk.label.length === 4) xt[xt.length - 1] = tk continue } xt.push(tk) } return { x, y, stacked, top, areaGen, edge, topLine, valueAt, pins, ticksY, xt } // eslint-disable-next-line react-hooks/exhaustive-deps }, [W, H, rows, keys.join(), site, narrow]) useEffect(() => setHover(null), [metric]) const onMove = (e: React.PointerEvent) => { if (!g) return const rect = (e.currentTarget as SVGElement).getBoundingClientRect() const t = g.x.invert(e.clientX - rect.left) let best = 0 for (let i = 0; i < rows.length; i++) if (Math.abs(rows[i].t - t) < Math.abs(rows[best].t - t)) best = i setHover(best) } const hr = hover != null ? rows[hover] : null const near = hr && g ? g.pins.find((p) => p.rels.some((r) => Math.abs(toTime(r.date) - hr.t) <= 4 * DAY)) : null const [lit, setLit] = useState(null) const plotW = W - m.l - m.r return (
{g && H > 0 && ( {keys.map((c) => ( ))} {g.ticksY.map((v) => ( {tick(v)} ))} {g.xt.map((tk) => ( {tk.label} ))} {/* the milestones: every pole starts on the same line at the top and ends on the curve */} {!narrow && g.pins.map((p) => { const px = g.x(p.t), py = g.y(g.valueAt(p.t)) const on = lit === p.key || near === p return ( ) })} {g.stacked.map((s) => ( ))} {g.stacked.map((s) => ( ))} {g.pins.map((p) => { const px = g.x(p.t), py = g.y(g.valueAt(p.t)) const delay = (REVEAL * (px - m.l)) / plotW return ( ) })} {/* today */} {hr && ( {g.stacked.map((s) => ( ))} )} { pointer.current = e.pointerType; onMove(e) }} onPointerLeave={(e) => e.pointerType === "mouse" && setHover(null)} onClick={() => near && pointer.current === "mouse" && onPick(pick(near.rels))} style={{ cursor: near ? "pointer" : "crosshair" }} /> )} {/* names are HTML so they stay crisp, focusable and clickable; each dot sits on its pole */} {g && !narrow && g.pins.map((p) => p.side !== "none" && (
setLit(p.key)} onPointerLeave={() => setLit(null)}> {p.rels.map((r) => ( ))}
))} {g && hr && (
{mdy(new Date(hr.t).toISOString())}
{value(keys.reduce((a, c) => a + hr[c], 0))}
{[...keys].reverse().filter((c) => hr[c] > 0).map((c) => (
{site.categories.find((x) => x.key === c)?.name} {value(hr[c])}
))}
{near && (
{near.rels.map((r) => (
{r.name} {md(r.date)}
))}
)}
)}
) } function Tip({ x, W, top, children }: { x: number; W: number; top: number; children: React.ReactNode }) { const w = 248 const left = x + 18 + w < W ? x + 18 : Math.max(0, x - 18 - w) return (
{children}
) }