Map: a Lines setting for curved or right-angled connections

Both map views can draw their lines as right-angled runs with rounded corners instead of curves; the choice is remembered per browser.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-14 01:54:22 +01:00
1 parent 939ebf8d83
commit 2986734196
7 files changed
+191 -5

No files matched your search

+2 -1
View File
@@ -431,7 +431,8 @@ linked server runs on, which DNS names point at it (an A or AAAA record holding
one of the server's own addresses — nothing is guessed from names), and which
Uptime Kuma monitors watch it. Hover or tab to a box to light up its
connections; each box links to its page. A monitor Uptime Kuma can't be read for
leaves a note rather than a silent gap.
leaves a note rather than a silent gap. Both views have a **Lines** switch above the boxes: *Curved* (the default) or *Right-angled* — out from a box, across, up or down
and into the next, with the corners softly rounded, like a wiring diagram. It changes only how the lines are drawn, is remembered in this browser, and is shared by both views.
The Map's second view, **Dependencies**, draws what needs what with the foundations on the left (the servers and services that take part in
a dependency or a "runs on" link, each with its status). Pick a box and what would go down with it turns red and what it relies on blue,
+79
View File
@@ -0,0 +1,79 @@
/**
* How the maps draw the lines between their boxes: smooth curves (the original look) or right-angled lines — the path each makes, that an unknown stored choice is the original look, and that both maps
* draw through the one function and offer the choice.
*
* Run with: npm test --workspace server
*/
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { CORNER_RADIUS, edgePath, MAP_LINE_STYLES, parseMapLineStyle } from "../../web/src/utils/mapLines.js";
const web = (...p: string[]) => readFileSync(new URL(`../../web/${p.join("/")}`, import.meta.url), "utf8");
describe("the path of a line", () => {
test("curved is the S it always was: out from the box, through the middle, into the next", () => {
assert.equal(edgePath(100, 20, 300, 120, "curved"), "M100,20 C200,20 200,120 300,120");
assert.equal(edgePath(100, 20, 300, 20, "curved"), "M100,20 C200,20 200,20 300,20", "level boxes: the same, flat");
});
test("right-angled goes out to the middle, up or down, and on in — with the corners rounded", () => {
const down = edgePath(100, 20, 300, 120, "right-angled");
assert.equal(down, `M100,20 H${200 - CORNER_RADIUS} Q200,20 200,${20 + CORNER_RADIUS} V${120 - CORNER_RADIUS} Q200,120 ${200 + CORNER_RADIUS},120 H300`);
const up = edgePath(100, 120, 300, 20, "right-angled");
assert.equal(up, `M100,120 H${200 - CORNER_RADIUS} Q200,120 200,${120 - CORNER_RADIUS} V${20 + CORNER_RADIUS} Q200,20 ${200 + CORNER_RADIUS},20 H300`);
});
test("every segment is horizontal or vertical: no diagonal, so the lines meet the boxes at right angles", () => {
for (const [x1, y1, x2, y2] of [[0, 0, 400, 300], [0, 300, 400, 0], [10, 50, 260, 70], [10, 50, 30, 52]] as const) {
const d = edgePath(x1, y1, x2, y2, "right-angled");
assert.doesNotMatch(d, /[LC]/, d);
assert.match(d, /^M[\d.]+,[\d.]+( [HVQ][\d.,\- ]+)+$/);
}
});
test("a line between boxes at the same height is one straight line", () => {
assert.equal(edgePath(100, 40, 300, 40, "right-angled"), "M100,40 H300");
assert.equal(edgePath(100, 40, 300, 40.3, "right-angled"), "M100,40 H300", "a third of a pixel is level");
});
test("the corners are no rounder than the room they have: small steps and short runs get hard corners, or none", () => {
// 6 px step: a radius of 3 at the most
assert.match(edgePath(100, 20, 300, 26, "right-angled"), /H197 Q200,20 200,23 V23 Q200,26 203,26 H300/);
// two boxes nearly touching: no room for a curve at all
assert.equal(edgePath(100, 20, 103, 90, "right-angled"), "M100,20 H101.5 V90 H103");
});
test("the line ends where it is told to start and finish", () => {
for (const style of ["curved", "right-angled"] as const) {
const d = edgePath(12.5, 33, 211.25, 90.75, style);
assert.ok(d.startsWith("M12.5,33"), d);
assert.ok(d.endsWith("211.3,90.8") || d.endsWith("H211.3") || d.endsWith(" 211.3,90.8"), d);
}
});
});
describe("the choice", () => {
test("two looks; the original is the one when nothing, or something unknown, is stored", () => {
assert.deepEqual(MAP_LINE_STYLES.map((s) => s.value), ["curved", "right-angled"]);
assert.equal(parseMapLineStyle("right-angled"), "right-angled");
for (const odd of [null, undefined, "", "curved", "straight", "RIGHT-ANGLED"]) assert.equal(parseMapLineStyle(odd), "curved", String(odd));
});
test("both maps draw their lines through the one function, and both offer the choice", () => {
for (const file of [["src", "pages", "Map.tsx"], ["src", "components", "DependencyMap.tsx"]]) {
const source = web(...file);
assert.match(source, /edgePath\(x1, y1, x2, y2, lineStyle\)/, file.join("/"));
assert.match(source, /<MapLinesToggle \/>/, file.join("/"));
assert.match(source, /useMapLineStyle\(\)/, file.join("/"));
assert.doesNotMatch(source, /C\$\{mid\}/, "no curve written out by hand any more");
}
});
test("it is remembered in this browser, under one key, and the original look removes it", () => {
const toggle = web("src", "components", "MapLinesToggle.tsx");
assert.match(toggle, /localStorage\.removeItem\(MAP_LINES_KEY\)/);
assert.match(toggle, /localStorage\.setItem\(MAP_LINES_KEY, next\)/);
assert.match(toggle, /aria-pressed=\{style === s\.value\}/);
});
});
+5 -2
View File
@@ -4,6 +4,8 @@ import { api, type DependencyGraph, type DependencyGraphNode } from "../api/clie
import { ErrorNotice } from "./Notice";
import { PageSkeleton } from "./Skeleton";
import { readableError } from "../utils/errors";
import MapLinesToggle, { useMapLineStyle } from "../components/MapLinesToggle";
import { edgePath } from "../utils/mapLines";
const NODE_W = 200;
const NODE_H = 46;
@@ -88,6 +90,7 @@ const names = (graph: DependencyGraph, keys: Iterable<string>) => [...keys].map(
* hovering does the same for a moment. This is the answer to "what breaks if I reboot this?" before doing it.
*/
export default function DependencyMap() {
const [lineStyle] = useMapLineStyle();
const [graph, setGraph] = useState<DependencyGraph | null>(null);
const [error, setError] = useState<string | null>(null);
// From the command palette ("What depends on …?"): start with that box picked.
@@ -127,6 +130,7 @@ export default function DependencyMap() {
return (
<div className="card">
<div className="card-body map-scroll">
<MapLinesToggle />
<div className="map-canvas" style={{ width: view.width, height: view.height }}>
<svg className="map-lines" width={view.width} height={view.height} aria-hidden="true">
{graph.edges.map((e) => {
@@ -137,14 +141,13 @@ export default function DependencyMap() {
const y1 = a.y + NODE_H / 2;
const x2 = b.x;
const y2 = b.y + NODE_H / 2;
const mid = (x1 + x2) / 2;
let cls = "map-edge";
if (sets && active) {
const downPath = (e.from === active || sets.down.has(e.from)) && sets.down.has(e.to);
const upPath = (e.to === active || sets.up.has(e.to)) && sets.up.has(e.from);
cls = downPath ? "map-edge map-edge-down" : upPath ? "map-edge map-edge-up" : "map-edge map-dim";
}
return <path key={`${e.from}>${e.to}`} d={`M${x1},${y1} C${mid},${y1} ${mid},${y2} ${x2},${y2}`} className={cls} fill="none" strokeDasharray={e.kind !== "written" ? "5 4" : undefined} />;
return <path key={`${e.from}>${e.to}`} d={edgePath(x1, y1, x2, y2, lineStyle)} className={cls} fill="none" strokeDasharray={e.kind !== "written" ? "5 4" : undefined} />;
})}
</svg>
{[...view.placed.values()].map((n) => (
+52
View File
@@ -0,0 +1,52 @@
import { useCallback, useEffect, useState } from "react";
import { t } from "../i18n";
import { MAP_LINE_STYLES, MAP_LINES_KEY, parseMapLineStyle, type MapLineStyle } from "../utils/mapLines";
function stored(): MapLineStyle {
try {
return parseMapLineStyle(localStorage.getItem(MAP_LINES_KEY));
} catch {
return "curved";
}
}
// The two maps share one choice, and a change in one is seen by the other without a reload.
const listeners = new Set<(s: MapLineStyle) => void>();
/** The line style of the maps: remembered in this browser (like the density), shared by both maps. */
export function useMapLineStyle(): [MapLineStyle, (style: MapLineStyle) => void] {
const [style, setStyle] = useState<MapLineStyle>(stored);
useEffect(() => {
listeners.add(setStyle);
return () => void listeners.delete(setStyle);
}, []);
const choose = useCallback((next: MapLineStyle) => {
try {
if (next === "curved") localStorage.removeItem(MAP_LINES_KEY);
else localStorage.setItem(MAP_LINES_KEY, next);
} catch {
// not remembered; it still applies for this visit
}
listeners.forEach((l) => l(next));
}, []);
return [style, choose];
}
/** "Lines: Curved | Right-angled" — how the connections between the boxes are drawn. */
export default function MapLinesToggle() {
const [style, choose] = useMapLineStyle();
return (
<div className="d-flex align-items-center justify-content-end gap-2 mb-2">
<span className="text-secondary small" id="map-lines-label">
{t("Lines")}
</span>
<div className="btn-group btn-group-sm" role="group" aria-labelledby="map-lines-label">
{MAP_LINE_STYLES.map((s) => (
<button key={s.value} type="button" className={`btn ${style === s.value ? "active" : ""}`} aria-pressed={style === s.value} onClick={() => choose(s.value)}>
{t(s.label)}
</button>
))}
</div>
</div>
);
}
+3
View File
@@ -206,6 +206,7 @@ export const sv: Record<string, string> = {
"CPU load": "Processorbelastning",
"Created": "Skapad",
"Critical": "Kritisk",
"Curved": "Böjda",
"Customise": "Anpassa",
"Dark mode": "Mörkt läge",
"Dashboard": "Översikt",
@@ -357,6 +358,7 @@ export const sv: Record<string, string> = {
"Left out now": "Utelämnas nu",
"Let someone else in (optional)": "Släpp in någon annan (valfritt)",
"Light mode": "Ljust läge",
"Lines": "Linjer",
"Link": "Koppla",
"Linked to": "Kopplad till",
"Linking…": "Kopplar…",
@@ -564,6 +566,7 @@ export const sv: Record<string, string> = {
"restart: {state}": "omstart: {state}",
"Restart…": "Starta om…",
"restarted": "omstartad",
"Right-angled": "Rätvinkliga",
"Roomy": "Luftig",
"Rotate every": "Rotera var",
"Rotate token": "Byt nyckel",
+5 -2
View File
@@ -9,6 +9,8 @@ import { PageSkeleton } from "../components/Skeleton";
import { readableError } from "../utils/errors";
import PageIntro from "../components/PageIntro";
import { t } from "../i18n";
import MapLinesToggle, { useMapLineStyle } from "../components/MapLinesToggle";
import { edgePath } from "../utils/mapLines";
const NODE_W = 210;
const NODE_H = 46;
@@ -130,6 +132,7 @@ export default function MapPage({ user }: { user: CurrentUser }) {
}
function ConnectionsMap() {
const [lineStyle] = useMapLineStyle();
const [data, setData] = useState<MapData | null>(null);
const [error, setError] = useState<string | null>(null);
const [focus, setFocus] = useState<string | null>(null);
@@ -169,6 +172,7 @@ function ConnectionsMap() {
{data && drawing && data.nodes.length > 0 && (
<div className="card">
<div className="card-body map-scroll">
<MapLinesToggle />
<div className="map-columns" style={{ width: drawing.width }}>
{drawing.columns.map((c) => (
<div key={c.title} className="map-column-title" style={{ left: c.x, width: NODE_W }}>
@@ -188,9 +192,8 @@ function ConnectionsMap() {
const y1 = l.y + NODE_H / 2;
const x2 = r.x;
const y2 = r.y + NODE_H / 2;
const mid = (x1 + x2) / 2;
const active = !lit || (lit.has(e.from) && lit.has(e.to) && (e.from === focus || e.to === focus));
return <path key={`${e.from}>${e.to}`} d={`M${x1},${y1} C${mid},${y1} ${mid},${y2} ${x2},${y2}`} className={active ? "map-edge" : "map-edge map-dim"} fill="none" />;
return <path key={`${e.from}>${e.to}`} d={edgePath(x1, y1, x2, y2, lineStyle)} className={active ? "map-edge" : "map-edge map-dim"} fill="none" />;
})}
</svg>
{[...drawing.placed.values()].map((n) => {
+45
View File
@@ -0,0 +1,45 @@
/**
* How the lines between the boxes of the maps are drawn: as smooth curves (the original look), or right-angled — out from a box, across, up or down, and into the next, the way a wiring diagram
* is drawn. Chosen per browser, like the density; it changes only the look of the lines, never what is connected to what. Pure, so it can be tested without a browser.
*/
export type MapLineStyle = "curved" | "right-angled";
export const MAP_LINE_STYLES: { value: MapLineStyle; label: string }[] = [
{ value: "curved", label: "Curved" },
{ value: "right-angled", label: "Right-angled" },
];
export const MAP_LINES_KEY = "hlm-map-lines";
/** What is stored, read back as a style; anything else is the original curved look. */
export function parseMapLineStyle(stored: string | null | undefined): MapLineStyle {
return stored === "right-angled" ? "right-angled" : "curved";
}
/** How far the corners of a right-angled line are rounded, in pixels (a hard corner reads as clutter where many lines meet). */
export const CORNER_RADIUS = 8;
const r1 = (n: number) => Math.round(n * 10) / 10;
/**
* The path of a line from (x1, y1) on the left to (x2, y2) on the right. Curved: an S through the middle. Right-angled: straight out to the middle, straight up or down, straight on in — with
* the two corners rounded where there is room for it. A line between boxes at the same height is straight either way.
*/
export function edgePath(x1: number, y1: number, x2: number, y2: number, style: MapLineStyle): string {
const mid = (x1 + x2) / 2;
if (style === "curved") return `M${r1(x1)},${r1(y1)} C${r1(mid)},${r1(y1)} ${r1(mid)},${r1(y2)} ${r1(x2)},${r1(y2)}`;
if (Math.abs(y2 - y1) < 0.5) return `M${r1(x1)},${r1(y1)} H${r1(x2)}`;
const dy = y2 - y1;
const down = dy > 0 ? 1 : -1;
// the corner can be no rounder than half the room it has, along and across
const r = Math.min(CORNER_RADIUS, Math.abs(dy) / 2, Math.abs(x2 - x1) / 4);
if (r < 1) return `M${r1(x1)},${r1(y1)} H${r1(mid)} V${r1(y2)} H${r1(x2)}`;
return [
`M${r1(x1)},${r1(y1)}`,
`H${r1(mid - r)}`,
`Q${r1(mid)},${r1(y1)} ${r1(mid)},${r1(y1 + down * r)}`,
`V${r1(y2 - down * r)}`,
`Q${r1(mid)},${r1(y2)} ${r1(mid + r)},${r1(y2)}`,
`H${r1(x2)}`,
].join(" ");
}