Files
Homelab-manager/web/src/pages/Map.tsx
T
bobbanandClaude Sonnet 5.5 1432b148e9 Fold four pages into the pages they belong to; add a Backups menu group
Dependencies become a view of the Map (graph with the editor under it), Sessions the
'Signed in now' tab of Users, the Audit Log the 'Raw audit log' tab of the Change log, and the
Diagnostic Log the 'Call log' tab of Integrations (admins). Backup coverage and Proxmox Backup
move to a new Backups group. Old addresses redirect; a shared ViewTabs component does the tabs.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-19 22:21:21 +02:00

233 lines
10 KiB
TypeScript

import { useEffect, useMemo, useState } from "react";
import { Link, useSearchParams } from "react-router-dom";
import DependencyMap from "../components/DependencyMap";
import DependencyEditor from "../views/DependencyEditor";
import type { CurrentUser } from "../api/client";
import { api, type MapData, type MapNode } from "../api/client";
import { ErrorNotice } from "../components/Notice";
import { PageSkeleton } from "../components/Skeleton";
import { readableError } from "../utils/errors";
const NODE_W = 210;
const NODE_H = 46;
const GAP_Y = 12;
const COL_GAP = 130;
const STATUS_SYMBOL: Record<MapNode["status"], string> = { ok: "✓", bad: "✕", warn: "▲", unknown: "·" };
const STATUS_WORD: Record<MapNode["status"], string> = { ok: "working", bad: "problem", warn: "check", unknown: "no status" };
const KIND_WORD: Record<MapNode["kind"], string> = { host: "Proxmox host", server: "Server", dns: "DNS name", monitor: "Uptime Kuma monitor" };
interface Placed extends MapNode {
x: number;
y: number;
}
/** Places each node: hosts, then servers, then names and monitors, each column ordered so the lines cross as little as is easy. */
function layout(data: MapData): { placed: Map<string, Placed>; width: number; height: number; columns: { x: number; title: string }[] } {
const neighbours = new Map<string, string[]>();
for (const e of data.edges) {
neighbours.set(e.from, [...(neighbours.get(e.from) ?? []), e.to]);
neighbours.set(e.to, [...(neighbours.get(e.to) ?? []), e.from]);
}
const byKind = (kinds: MapNode["kind"][]) => data.nodes.filter((n) => kinds.includes(n.kind));
const hosts = byKind(["host"]).sort((a, b) => a.label.localeCompare(b.label));
const serverNodes = byKind(["server"]);
const rightNodes = byKind(["dns", "monitor"]);
// Servers grouped under their host, then those with none.
const hostOf = (s: MapNode) => (neighbours.get(s.id) ?? []).find((id) => id.startsWith("host:"));
const servers: MapNode[] = [];
for (const h of hosts) servers.push(...serverNodes.filter((s) => hostOf(s) === h.id).sort((a, b) => a.label.localeCompare(b.label)));
servers.push(...serverNodes.filter((s) => !hostOf(s)).sort((a, b) => a.label.localeCompare(b.label)));
const step = NODE_H + GAP_Y;
const placed = new Map<string, Placed>();
const hasHosts = hosts.length > 0;
const hasRight = rightNodes.length > 0;
const colX = [0, hasHosts ? NODE_W + COL_GAP : 0, 0];
colX[2] = colX[1] + (servers.length > 0 ? NODE_W + COL_GAP : 0);
servers.forEach((s, i) => placed.set(s.id, { ...s, x: colX[1], y: i * step }));
// A column of nodes sits at the average height of the servers it connects to, pushed down as needed so none overlap.
const stack = (nodes: MapNode[], x: number) => {
const wanted = nodes.map((n) => {
const ys = (neighbours.get(n.id) ?? []).map((id) => placed.get(id)?.y).filter((y): y is number => y !== undefined);
return { n, y: ys.length ? ys.reduce((a, b) => a + b, 0) / ys.length : Number.MAX_SAFE_INTEGER / 2 };
});
wanted.sort((a, b) => a.y - b.y || a.n.label.localeCompare(b.n.label));
let next = 0;
for (const { n, y } of wanted) {
const at = Math.max(next, y >= Number.MAX_SAFE_INTEGER / 4 ? next : y);
placed.set(n.id, { ...n, x, y: at });
next = at + step;
}
};
stack(hosts, colX[0]);
stack(rightNodes, colX[2]);
const all = [...placed.values()];
const height = all.length ? Math.max(...all.map((p) => p.y)) + NODE_H : 0;
const width = Math.max(...all.map((p) => p.x), 0) + NODE_W;
const columns = [
...(hasHosts ? [{ x: colX[0], title: "Proxmox hosts" }] : []),
...(servers.length > 0 ? [{ x: colX[1], title: "Servers" }] : []),
...(hasRight ? [{ x: colX[2], title: "DNS names & monitors" }] : []),
];
return { placed, width, height, columns };
}
/** The picture: how the servers, their Proxmox hosts, DNS names and monitors connect. Hover or tab to a box to light up its connections. */
export default function MapPage({ user }: { user: CurrentUser }) {
const [params, setParams] = useSearchParams();
const view = params.get("view") === "dependencies" ? "dependencies" : "connections";
const choose = (v: "connections" | "dependencies") =>
setParams(
(prev) => {
const next = new URLSearchParams(prev);
if (v === "connections") next.delete("view");
else next.set("view", v);
return next;
},
{ replace: true },
);
return (
<>
<div className="d-flex flex-wrap align-items-center gap-2 mb-1">
<h2 className="page-title mb-0">Map</h2>
<div className="btn-group ms-auto" role="group" aria-label="What to draw">
<button type="button" className={`btn btn-sm ${view === "connections" ? "active" : ""}`} aria-pressed={view === "connections"} onClick={() => choose("connections")}>
Connections
</button>
<button type="button" className={`btn btn-sm ${view === "dependencies" ? "active" : ""}`} aria-pressed={view === "dependencies"} onClick={() => choose("dependencies")}>
Dependencies
</button>
</div>
</div>
{view === "connections" ? (
<ConnectionsMap />
) : (
<>
<p className="text-secondary">
A server that runs on another, a service that needs the server it is installed on. Pick a box to see what would go down with it before you reboot it.
</p>
<DependencyMap />
<h3 className="mt-4 mb-2">Say what needs what</h3>
<DependencyEditor user={user} />
</>
)}
</>
);
}
function ConnectionsMap() {
const [data, setData] = useState<MapData | null>(null);
const [error, setError] = useState<string | null>(null);
const [focus, setFocus] = useState<string | null>(null);
useEffect(() => {
api.map
.get()
.then(setData)
.catch((err) => setError(readableError(err)));
}, []);
const drawing = useMemo(() => (data ? layout(data) : null), [data]);
const lit = useMemo(() => {
if (!data || !focus) return null;
const set = new Set<string>([focus]);
for (const e of data.edges) {
if (e.from === focus) set.add(e.to);
if (e.to === focus) set.add(e.from);
}
return set;
}, [data, focus]);
return (
<>
<p className="text-secondary">
How things connect: which Proxmox host a server runs on, which DNS names point at it, and which Uptime Kuma monitors watch it. Hover or tab to a box to light up what it is
connected to.
</p>
<ErrorNotice error={error} />
{data?.notes.map((n) => (
<div key={n} className="alert alert-warning">
{n}
</div>
))}
{!data && !error && <PageSkeleton />}
{data && drawing && data.nodes.length === 0 && <div className="card"><div className="card-body text-secondary">Nothing to draw yet — add a server first.</div></div>}
{data && drawing && data.nodes.length > 0 && (
<div className="card">
<div className="card-body map-scroll">
<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 }}>
{c.title}
</div>
))}
</div>
<div className="map-canvas" style={{ width: drawing.width, height: drawing.height }}>
<svg className="map-lines" width={drawing.width} height={drawing.height} aria-hidden="true">
{data.edges.map((e) => {
const a = drawing.placed.get(e.from);
const b = drawing.placed.get(e.to);
if (!a || !b) return null;
// Always drawn from the node on the left to the one on the right.
const [l, r] = a.x <= b.x ? [a, b] : [b, a];
const x1 = l.x + NODE_W;
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" />;
})}
</svg>
{[...drawing.placed.values()].map((n) => {
const dim = lit && !lit.has(n.id);
const body = (
<>
<span className={`map-status map-status-${n.status}`} aria-hidden="true">
{STATUS_SYMBOL[n.status]}
</span>
<span className="map-label">
<span className="map-title">{n.label}</span>
{n.sub && <span className="map-sub">{n.sub}</span>}
</span>
</>
);
const props = {
className: `map-node map-node-${n.kind} ${dim ? "map-dim" : ""}`,
style: { left: n.x, top: n.y, width: NODE_W, height: NODE_H },
onMouseEnter: () => setFocus(n.id),
onMouseLeave: () => setFocus((f) => (f === n.id ? null : f)),
onFocus: () => setFocus(n.id),
onBlur: () => setFocus((f) => (f === n.id ? null : f)),
"aria-label": `${KIND_WORD[n.kind]}: ${n.label}${n.sub ? `, ${n.sub}` : ""}, ${STATUS_WORD[n.status]}`,
};
return n.href ? (
<Link key={n.id} to={n.href} {...props}>
{body}
</Link>
) : (
<div key={n.id} {...props}>
{body}
</div>
);
})}
</div>
</div>
<div className="card-footer text-secondary small d-flex flex-wrap gap-3">
<span>✓ working</span>
<span>✕ problem or not reporting</span>
<span>▲ needs a look</span>
<span>· no status</span>
<span className="ms-auto">DNS names appear where a record holds a server's own address; monitors where their target is the server's.</span>
</div>
</div>
)}
</>
);
}