timeline.ts

1.8 KB · raw

1import type { Save } from "../mirror/queries.ts";2import { type Html, html } from "./html.ts";3import { formatDate } from "./layout.ts";45/**6 * §21.4's dotted timeline as SVG, so it draws without JavaScript (kepr.js swaps it for Kepr 2's7 * drifting canvas, reading each save from its dot): one dot per save on a dashed line, x by8 * time (as a percentage, so it scales without squashing dots), brighter and fuller where9 * neighbours crowd within a fortieth of the span.10 */11export function timelineSvg(repo: string, saves: Save[]): Html {12	const times = saves.map((save) => save.created_at);13	// Replayed saves keep their own times, so the line is not in time order.14	const first = times.reduce((a, b) => Math.min(a, b), times[0] ?? 0);15	const span = times.reduce((a, b) => Math.max(a, b), first) - first;16	const reach = Math.max(span / 40, 3600);17	const dots = saves.map((save) => {18		const x = span === 0 ? 50 : 1 + (98 * (save.created_at - first)) / span;19		// ponytail: O(n²) neighbour count; a sliding window over sorted times if a repo20		// reaches ~5,000 saves.21		const near = times.filter((t) => Math.abs(t - save.created_at) <= reach).length;22		const level = near >= 8 ? 3 : near >= 4 ? 2 : near >= 2 ? 1 : 0;23		const meta = [formatDate(save.created_at), save.agent, save.session]24			.filter(Boolean)25			.join(" · ");26		const tip = `${save.id}\n${save.message.split("\n")[0]}\n${meta}`;27		return html`<a href="/${28			repo29		}/saves/${save.id}" class="dot d${level}" data-t="${save.created_at}"><title>${tip}</title><circle cx="${x.toFixed(3)}%" cy="28" r="${2.5 + level * 0.5}"/></a>`;30	});31	return html`<svg class="timeline" width="100%" height="56" role="img" aria-label="${32		saves.length33	} saves over time">34<line class="timeline-line" x1="0" x2="100%" y1="28" y2="28"/>35${dots}36</svg>`;37}