Skip to content

Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring

fix(layout): size zones and groups at their real box in Auto Layout

Pouzor7f09b0224394
diff --git a/frontend/src/utils/__tests__/layout.test.ts b/frontend/src/utils/__tests__/layout.test.ts
index 4df12e1..6dac87b 100644
--- a/frontend/src/utils/__tests__/layout.test.ts
+++ b/frontend/src/utils/__tests__/layout.test.ts
@@ -167,4 +167,59 @@ describe('applyDagreLayout', () => {
const sw2 = result.find((n) => n.id === 'sw2')!
expect(sw1.position.y).toBe(sw2.position.y)
})
+
+ describe('zones (#326)', () => {
+ function sized(id: string, type: string, width: number, height: number): Node<NodeData> {
+ return { ...makeNode(id, type), width, height }
+ }
+ function box(n: Node<NodeData>, w: number, h: number) {
+ return { l: n.position.x, r: n.position.x + w, t: n.position.y, b: n.position.y + h }
+ }
+ function overlaps(a: ReturnType<typeof box>, b: ReturnType<typeof box>) {
+ return a.l < b.r && b.l < a.r && a.t < b.b && b.t < a.b
+ }
+
+ it('keeps two large unconnected zones from overlapping', () => {
+ const nodes = [sized('z1', 'groupRect', 600, 400), sized('z2', 'groupRect', 600, 400)]
+ const result = applyDagreLayout(nodes, [])
+ const [a, b] = result.map((n) => box(n, 600, 400))
+ expect(overlaps(a, b)).toBe(false)
+ })
+
+ it('places a node linked below a zone under the zone bottom edge', () => {
+ const nodes = [sized('z', 'groupRect', 500, 300), makeNode('srv', 'server')]
+ const result = applyDagreLayout(nodes, [makeEdge('z', 'srv')])
+ const z = result.find((n) => n.id === 'z')!
+ const srv = result.find((n) => n.id === 'srv')!
+ expect(srv.position.y).toBeGreaterThanOrEqual(z.position.y + 300)
+ })
+
+ it('sizes a group box from its own dimensions too', () => {
+ const nodes = [sized('g1', 'group', 500, 350), sized('g2', 'group', 500, 350)]
+ const result = applyDagreLayout(nodes, [])
+ const [a, b] = result.map((n) => box(n, 500, 350))
+ expect(overlaps(a, b)).toBe(false)
+ })
+
+ it('falls back to the measured size when width/height are unset', () => {
+ const nodes = [
+ { ...makeNode('z1', 'groupRect'), measured: { width: 700, height: 300 } },
+ { ...makeNode('z2', 'groupRect'), measured: { width: 700, height: 300 } },
+ ]
+ const result = applyDagreLayout(nodes, [])
+ const [a, b] = result.map((n) => box(n, 700, 300))
+ expect(overlaps(a, b)).toBe(false)
+ })
+
+ it('still lays plain nodes out at the fixed node size, ignoring measured', () => {
+ const nodes = [
+ { ...makeNode('a', 'server'), measured: { width: 900, height: 900 } },
+ { ...makeNode('b', 'server'), measured: { width: 900, height: 900 } },
+ ]
+ const result = applyDagreLayout(nodes, [])
+ const xs = result.map((n) => n.position.x).sort((p, q) => p - q)
+ // 180 px node + 60 px nodesep: the pair sits 240 px apart.
+ expect(xs[1] - xs[0]).toBe(240)
+ })
+ })
})
diff --git a/frontend/src/utils/layout.ts b/frontend/src/utils/layout.ts
index d193420..499a03c 100644
--- a/frontend/src/utils/layout.ts
+++ b/frontend/src/utils/layout.ts
@@ -8,6 +8,24 @@ const NODE_HEIGHT = 52
const PEER_TYPES = new Set(['proxmox', 'switch'])
+// Boxes whose footprint is their own size, not the fixed node card: a proxmox
+// host and the two zone kinds. Dagre must see the real box or it packs them
+// on top of each other (#326). The fallbacks match the store's defaults.
+const BOX_DEFAULT_SIZE: Record<string, { w: number; h: number }> = {
+ proxmox: { w: 300, h: 200 },
+ groupRect: { w: 360, h: 240 },
+ group: { w: 360, h: 240 },
+}
+
+function layoutSize(node: Node<NodeData>): { w: number; h: number } {
+ const box = BOX_DEFAULT_SIZE[node.type ?? '']
+ if (!box) return { w: NODE_WIDTH, h: NODE_HEIGHT }
+ return {
+ w: node.width ?? node.measured?.width ?? box.w,
+ h: node.height ?? node.measured?.height ?? box.h,
+ }
+}
+
/**
* Port index encoded by a bottom source handle:
* 'bottom' → 0, 'bottom-2' → 1, 'bottom-3' → 2, ...
@@ -91,8 +109,7 @@ export function applyDagreLayout(
g.setGraph({ rankdir: 'TB', nodesep: 60, ranksep: 80 })
for (const node of topLevel) {
- const w = node.type === 'proxmox' ? (node.width ?? 300) : NODE_WIDTH
- const h = node.type === 'proxmox' ? (node.height ?? 200) : NODE_HEIGHT
+ const { w, h } = layoutSize(node)
g.setNode(node.id, { width: w, height: h })
}
for (const edge of edges) {
@@ -117,8 +134,7 @@ export function applyDagreLayout(
const positions = new Map<string, { x: number; y: number; w: number; h: number }>()
for (const node of topLevel) {
const pos = g.node(node.id)
- const w = node.type === 'proxmox' ? (node.width ?? 300) : NODE_WIDTH
- const h = node.type === 'proxmox' ? (node.height ?? 200) : NODE_HEIGHT
+ const { w, h } = layoutSize(node)
positions.set(node.id, { x: pos.x - w / 2, y: pos.y - h / 2, w, h })
}