From 29867341969484dd44e78faf8e7a37f12ee69fa7 Mon Sep 17 00:00:00 2001 From: Bobban Rydh Date: Sat, 14 Nov 2026 01:54:22 +0100 Subject: [PATCH] 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 --- README.md | 3 +- server/test/mapLines.test.ts | 79 +++++++++++++++++++++++++++ web/src/components/DependencyMap.tsx | 7 ++- web/src/components/MapLinesToggle.tsx | 52 ++++++++++++++++++ web/src/i18n/sv.ts | 3 + web/src/pages/Map.tsx | 7 ++- web/src/utils/mapLines.ts | 45 +++++++++++++++ 7 files changed, 191 insertions(+), 5 deletions(-) create mode 100644 server/test/mapLines.test.ts create mode 100644 web/src/components/MapLinesToggle.tsx create mode 100644 web/src/utils/mapLines.ts diff --git a/README.md b/README.md index f818296..c7475e9 100644 --- a/README.md +++ b/README.md @@ -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, diff --git a/server/test/mapLines.test.ts b/server/test/mapLines.test.ts new file mode 100644 index 0000000..2973bf2 --- /dev/null +++ b/server/test/mapLines.test.ts @@ -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, //, 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\}/); + }); +}); diff --git a/web/src/components/DependencyMap.tsx b/web/src/components/DependencyMap.tsx index 0702d1a..6faf120 100644 --- a/web/src/components/DependencyMap.tsx +++ b/web/src/components/DependencyMap.tsx @@ -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) => [...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(null); const [error, setError] = useState(null); // From the command palette ("What depends on …?"): start with that box picked. @@ -127,6 +130,7 @@ export default function DependencyMap() { return (
+
{[...view.placed.values()].map((n) => ( diff --git a/web/src/components/MapLinesToggle.tsx b/web/src/components/MapLinesToggle.tsx new file mode 100644 index 0000000..4bfd1ac --- /dev/null +++ b/web/src/components/MapLinesToggle.tsx @@ -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(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 ( +
+ + {t("Lines")} + +
+ {MAP_LINE_STYLES.map((s) => ( + + ))} +
+
+ ); +} diff --git a/web/src/i18n/sv.ts b/web/src/i18n/sv.ts index 656a304..d44b087 100644 --- a/web/src/i18n/sv.ts +++ b/web/src/i18n/sv.ts @@ -206,6 +206,7 @@ export const sv: Record = { "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 = { "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 = { "restart: {state}": "omstart: {state}", "Restart…": "Starta om…", "restarted": "omstartad", + "Right-angled": "Rätvinkliga", "Roomy": "Luftig", "Rotate every": "Rotera var", "Rotate token": "Byt nyckel", diff --git a/web/src/pages/Map.tsx b/web/src/pages/Map.tsx index 9107d76..fd60485 100644 --- a/web/src/pages/Map.tsx +++ b/web/src/pages/Map.tsx @@ -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(null); const [error, setError] = useState(null); const [focus, setFocus] = useState(null); @@ -169,6 +172,7 @@ function ConnectionsMap() { {data && drawing && data.nodes.length > 0 && (
+
{drawing.columns.map((c) => (
@@ -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 ${e.to}`} d={`M${x1},${y1} C${mid},${y1} ${mid},${y2} ${x2},${y2}`} className={active ? "map-edge" : "map-edge map-dim"} fill="none" />; + return ${e.to}`} d={edgePath(x1, y1, x2, y2, lineStyle)} className={active ? "map-edge" : "map-edge map-dim"} fill="none" />; })} {[...drawing.placed.values()].map((n) => { diff --git a/web/src/utils/mapLines.ts b/web/src/utils/mapLines.ts new file mode 100644 index 0000000..304b1ca --- /dev/null +++ b/web/src/utils/mapLines.ts @@ -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(" "); +}