---
/**
* `` — copies made from a parent over time, like git branches.
* One lane per branch, time running right, a curve where a branch forks from
* its parent, and labelled points for what happens on each. Use it for
* database branches (Neon, PlanetScale, Prisma), point-in-time copies (Turso)
* and preview stages that come and go.
*
*
*
* `until` ends a branch (deleted with its stage). `dashed` draws the stretch of
* a branch that still shares its parent's storage, up to `sharedUntil`.
*/
import "./diagram.css";
interface Branch {
id: string;
label: string;
type?: string;
from?: { branch: string; at: number };
until?: number;
sharedUntil?: number;
tone?: "default" | "accent";
}
interface Props {
branches: Branch[];
points?: { branch: string; at: number; label: string; below?: boolean }[];
end: number;
ticks?: { at: number; label: string }[];
caption?: string;
}
const { branches, points = [], end, ticks = [], caption } = Astro.props as Props;
// Two layouts: branch names on the left (desktop), or above each lane
// (phones), so the text keeps its size. CSS shows one of them.
const layout = (narrow: boolean) => {
const LABEL_W = narrow ? 8 : Math.max(...branches.map((b) => Math.max(b.label.length * 7.6, (b.type ?? "").length * 6.4))) + 24;
const PLOT_W = narrow ? 304 : 540;
const LANE = narrow ? 78 : 64;
const TOP = narrow ? 46 : 30;
const x = (t: number) => LABEL_W + (t / end) * PLOT_W;
const lane = new Map(branches.map((b, i) => [b.id, TOP + i * LANE]));
const H = TOP + (branches.length - 1) * LANE + 34 + (ticks.length ? 20 : 0);
const W = LABEL_W + PLOT_W + 16;
// A point label that would run into the one before it on its lane goes below the line.
const lastEnd = new Map();
const placed = [...points]
.sort((a, b) => a.at - b.at)
.map((p) => {
const w = p.label.length * 6.4;
const left = Math.max(2, Math.min(x(p.at) - w / 2, W - w - 2));
const below = p.below || (narrow && left < (lastEnd.get(p.branch) ?? -Infinity) + 6);
if (!below) lastEnd.set(p.branch, left + w);
return { p, below, left };
});
return { narrow, x, lane, H, W, placed };
};
const layouts = [layout(false), layout(true)];
---