Skip to content
refs
History
135 lines · 7.0 KBRaw
1import { useEffect, useRef, useState } from "react";
2import { Link } from "../provider";
3import { cn } from "../ui/cn";
4
5export interface LineStation {
6 readonly key: string;
7 readonly href: string;
8 readonly title: string;
9 readonly decisions: number;
10 /** A person steered one of this station's decisions: a human act on the line. */
11 readonly steered: boolean;
12}
13
14export interface LineBranch {
15 /** The station the branch leaves from, or -1 when it leaves from the base. */
16 readonly at: number;
17 readonly label: string;
18 readonly href: string;
19}
20
21export interface LineDiagramProps {
22 readonly stations: readonly LineStation[];
23 readonly branches?: readonly LineBranch[];
24 readonly base: string;
25 readonly head: string;
26 readonly status: "open" | "closed" | "merged";
27 /** Something on this line waits for you, such as a tool approval. The only amber mark in the diagram. */
28 readonly needsYou?: boolean;
29 readonly current?: number;
30 readonly className?: string;
31}
32
33const MIN_SPACING = 30;
34const MAX_SPACING = 120;
35const PAD = 28;
36const LANE = 38;
37const shortLabel = (label: string) => (label.length > 20 ? `${label.slice(0, 19)}…` : label);
38const BRANCH_INKS = ["var(--ochre)", "var(--sage)"];
39
40/**
41 * A pull request drawn as a transit line: each commit is a station, the merge into the base is the interchange,
42 * 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.
43 */
44export function LineDiagram({ stations, branches = [], base, head, status, needsYou = false, current, className }: LineDiagramProps) {
45 const frame = useRef<HTMLDivElement>(null);
46 const [available, setAvailable] = useState(0);
47 useEffect(() => {
48 const element = frame.current;
49 if (!element) return;
50 const observer = new ResizeObserver(([entry]) => setAvailable(entry.contentRect.width));
51 observer.observe(element);
52 return () => observer.disconnect();
53 }, []);
54 const SPACING = Math.max(MIN_SPACING, Math.min(MAX_SPACING, (available - PAD - 12 - 110) / (stations.length + 1) || MIN_SPACING));
55 useEffect(() => {
56 const element = frame.current;
57 const station = element?.querySelector<SVGElement>('[aria-current="step"]');
58 if (!element || !station || element.scrollWidth <= element.clientWidth) return;
59 const box = station.getBoundingClientRect();
60 const left = element.getBoundingClientRect().left;
61 element.scrollTo({ left: element.scrollLeft + box.left - left - element.clientWidth / 2, behavior: "smooth" });
62 }, [current, SPACING]);
63 const lanes = branches.length;
64 const mainY = 42 + lanes * LANE;
65 const x = (index: number) => PAD + 12 + (index + 1) * SPACING;
66 const end = x(stations.length);
67 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);
68 const width = Math.max(end + 96, ...branchEnds.map((at) => at + 12));
69 const height = mainY + 40;
70 const interchangeFill = status === "merged" ? "var(--success)" : "var(--panel)";
71 const interchangeRing = status === "closed" ? "var(--faint)" : "var(--porcelain)";
72 return (
73 <div ref={frame} className={cn("scroll-fade overflow-x-auto overflow-y-hidden rounded-panel bg-panel shadow-card", className)}>
74 <svg width={width} height={height} viewBox={`0 0 ${width} ${height}`} role="img" aria-label={`${head} has ${stations.length} ${stations.length === 1 ? "step" : "steps"} into ${base}`} className="block font-sans">
75 {branches.map((branch, lane) => {
76 const from = branch.at < 0 ? PAD + 12 : x(branch.at);
77 const y = mainY - (lane + 1) * LANE;
78 const rise = mainY - y;
79 const runTo = from + rise + SPACING * 1.5;
80 const ink = BRANCH_INKS[lane % BRANCH_INKS.length];
81 return (
82 <Link key={branch.href} href={branch.href} aria-label={`Branch: ${branch.label}`} className="group/branch outline-none">
83 <path d={`M${from} ${mainY} L${from + rise} ${y} H${runTo}`} fill="none" stroke={ink} strokeWidth={3} strokeLinejoin="round" strokeLinecap="round" pathLength={1} className="line-draw" style={{ ["--line-length" as string]: 1, animationDelay: "250ms" }} />
84 <line x1={runTo} y1={y - 6} x2={runTo} y2={y + 6} stroke={ink} strokeWidth={3} strokeLinecap="round" />
85 <text x={runTo + 10} y={y + 4} fontSize={12} fill="var(--muted)" className="group-hover/branch:fill-[var(--ink)] group-focus-visible/branch:fill-[var(--ink)]">
86 {shortLabel(branch.label)}
87 </text>
88 </Link>
89 );
90 })}
91
92 <path d={`M${PAD} ${mainY} H${end}`} fill="none" stroke="var(--porcelain)" strokeWidth={3} strokeLinecap="round" pathLength={1} className="line-draw" style={{ ["--line-length" as string]: 1 }} />
93 <text x={PAD} y={mainY - 20} fontSize={11} fill="var(--faint)" className="font-mono">
94 {head}
95 </text>
96
97 {stations.map((station, index) => {
98 const cx = x(index);
99 const selected = index === current;
100 const last = index === stations.length - 1;
101 return (
102 <Link key={station.key} href={station.href} aria-label={`Step ${index + 1}: ${station.title}`} aria-current={selected ? "step" : undefined} className="group/station outline-none">
103 <title>{`Step ${index + 1}: ${station.title}`}</title>
104 <circle cx={cx} cy={mainY} r={16} fill="transparent" />
105 {selected && <circle cx={cx} cy={mainY} r={11} fill="none" stroke="var(--porcelain)" strokeWidth={1.5} strokeOpacity={0.55} />}
106 {needsYou && last && <circle cx={cx} cy={mainY} r={11} fill="none" stroke="var(--needs)" strokeWidth={2} />}
107 <circle
108 cx={cx}
109 cy={mainY}
110 r={6}
111 fill="var(--panel)"
112 stroke="var(--porcelain)"
113 strokeWidth={selected ? 3.5 : 2}
114 className="station-in transition-[stroke-width] duration-150 group-hover/station:[stroke-width:3] group-focus-visible/station:[stroke-width:3]"
115 style={{ animationDelay: `${Math.min(index * 30, 360)}ms` }}
116 />
117 {station.steered && <circle cx={cx} cy={mainY - 17} r={3.5} fill="var(--accent-hover)" />}
118 <text x={cx} y={mainY + 26} textAnchor="middle" fontSize={11} fill={selected ? "var(--ink)" : "var(--faint)"} fontWeight={selected ? 600 : 400} className="tabular-nums group-hover/station:fill-[var(--ink)]">
119 {index + 1}
120 </text>
121 </Link>
122 );
123 })}
124
125 <g aria-label={status === "merged" ? `Merged into ${base}` : `Into ${base}`}>
126 {status === "merged" && <circle cx={end} cy={mainY} r={9} fill="none" stroke="var(--success)" strokeWidth={2} className="merge-ping" />}
127 <circle cx={end} cy={mainY} r={9} fill={interchangeFill} stroke={interchangeRing} strokeWidth={2.5} />
128 <text x={end + 16} y={mainY + 4} fontSize={12} fill="var(--muted)" className="font-mono">
129 {base}
130 </text>
131 </g>
132 </svg>
133 </div>
134 );
135}