import { useEffect, useRef, useState } from "react"; import { Link } from "../provider"; import { cn } from "../ui/cn"; export interface LineStation { readonly key: string; readonly href: string; readonly title: string; readonly decisions: number; /** A person steered one of this station's decisions: a human act on the line. */ readonly steered: boolean; } export interface LineBranch { /** The station the branch leaves from, or -1 when it leaves from the base. */ readonly at: number; readonly label: string; readonly href: string; } export interface LineDiagramProps { readonly stations: readonly LineStation[]; readonly branches?: readonly LineBranch[]; readonly base: string; readonly head: string; readonly status: "open" | "closed" | "merged"; /** Something on this line waits for you, such as a tool approval. The only amber mark in the diagram. */ readonly needsYou?: boolean; readonly current?: number; readonly className?: string; } const MIN_SPACING = 30; const MAX_SPACING = 120; const PAD = 28; const LANE = 38; const shortLabel = (label: string) => (label.length > 20 ? `${label.slice(0, 19)}…` : label); const BRANCH_INKS = ["var(--ochre)", "var(--sage)"]; /** * A pull request drawn as a transit line: each commit is a station, the merge into the base is the interchange, * and every branch taken from a step leaves at 45°. Agent work is a hollow ring; a person's act is a filled blue dot. */ export function LineDiagram({ stations, branches = [], base, head, status, needsYou = false, current, className }: LineDiagramProps) { const frame = useRef(null); const [available, setAvailable] = useState(0); useEffect(() => { const element = frame.current; if (!element) return; const observer = new ResizeObserver(([entry]) => setAvailable(entry.contentRect.width)); observer.observe(element); return () => observer.disconnect(); }, []); const SPACING = Math.max(MIN_SPACING, Math.min(MAX_SPACING, (available - PAD - 12 - 110) / (stations.length + 1) || MIN_SPACING)); useEffect(() => { const element = frame.current; const station = element?.querySelector('[aria-current="step"]'); if (!element || !station || element.scrollWidth <= element.clientWidth) return; const box = station.getBoundingClientRect(); const left = element.getBoundingClientRect().left; element.scrollTo({ left: element.scrollLeft + box.left - left - element.clientWidth / 2, behavior: "smooth" }); }, [current, SPACING]); const lanes = branches.length; const mainY = 42 + lanes * LANE; const x = (index: number) => PAD + 12 + (index + 1) * SPACING; const end = x(stations.length); const branchEnds = branches.map((branch, lane) => (branch.at < 0 ? PAD + 12 : x(branch.at)) + (lane + 1) * LANE + SPACING * 1.5 + 10 + shortLabel(branch.label).length * 7); const width = Math.max(end + 96, ...branchEnds.map((at) => at + 12)); const height = mainY + 40; const interchangeFill = status === "merged" ? "var(--success)" : "var(--panel)"; const interchangeRing = status === "closed" ? "var(--faint)" : "var(--porcelain)"; return (
{branches.map((branch, lane) => { const from = branch.at < 0 ? PAD + 12 : x(branch.at); const y = mainY - (lane + 1) * LANE; const rise = mainY - y; const runTo = from + rise + SPACING * 1.5; const ink = BRANCH_INKS[lane % BRANCH_INKS.length]; return ( {shortLabel(branch.label)} ); })} {head} {stations.map((station, index) => { const cx = x(index); const selected = index === current; const last = index === stations.length - 1; return ( {`Step ${index + 1}: ${station.title}`} {selected && } {needsYou && last && } {station.steered && } {index + 1} ); })} {status === "merged" && } {base}
); }