Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring
TypeScript diagramhomelabhomelab-infrastructure-observabilitynetworkproxmox-veself-hostedzigbeezwavezwave-js
fix(liveview): keep zone children parented in the read-only view
Pouzor12a6d99c33b6
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsxindex b4d930d..5ef0d30 100644--- a/frontend/src/App.tsx+++ b/frontend/src/App.tsx@@ -3,7 +3,7 @@ import { ReactFlowProvider, type Connection, type Edge } from '@xyflow/react'import { type Node } from '@xyflow/react'import { applyDagreLayout } from '@/utils/layout'import { groupIntoZones, liftEdgesToTopLevel, type AutoLayoutMode } from '@/utils/zoneGrouping'-import { serializeNode, serializeEdge, deserializeApiNode, deserializeApiEdge, migrateClusterHandles, type ApiNode, type ApiEdge } from '@/utils/canvasSerializer'+import { serializeNode, serializeEdge, deserializeApiCanvas, migrateClusterHandles, type ApiNode, type ApiEdge } from '@/utils/canvasSerializer'import { generateUUID } from '@/utils/uuid'import { getCenteredPosition } from '@/utils/viewportCenter'import { resolveVirtualEdgeParent } from '@/utils/virtualEdgeParent'@@ -242,17 +242,9 @@ export default function App() {const { nodes: apiNodes, edges: apiEdges } = res.dataconst mode = decideCanvasLoad(apiNodes.length > 0, res.data.initialized === true)if (mode === 'real') {- const proxmoxContainerMap = new Map<string, boolean>(- (apiNodes as ApiNode[])- .filter((n) => n.type === 'group' || n.container_mode === true)- .map((n) => [n.id, true])- )- const zoneIds = new Set(- (apiNodes as ApiNode[]).filter((n) => n.type === 'groupRect').map((n) => n.id)- )- const { nodes: rfNodes, edges: rfEdges } = migrateClusterHandles(- (apiNodes as ApiNode[]).map((n) => deserializeApiNode(n, proxmoxContainerMap, zoneIds)),- (apiEdges as ApiEdge[]).map(deserializeApiEdge),+ const { nodes: rfNodes, edges: rfEdges } = deserializeApiCanvas(+ apiNodes as ApiNode[],+ apiEdges as ApiEdge[],)const savedTheme = res.data.viewport?.theme_idif (savedTheme) setTheme(savedTheme)diff --git a/frontend/src/components/LiveView.tsx b/frontend/src/components/LiveView.tsxindex ab35b23..fe40b69 100644--- a/frontend/src/components/LiveView.tsx+++ b/frontend/src/components/LiveView.tsx@@ -27,7 +27,7 @@ import { useThemeStore } from '@/stores/themeStore'import { THEMES } from '@/utils/themes'import { nodeTypes } from '@/components/canvas/nodes/nodeTypes'import { edgeTypes } from '@/components/canvas/edges/edgeTypes'-import { deserializeApiNode, deserializeApiEdge, migrateClusterHandles, type ApiNode, type ApiEdge } from '@/utils/canvasSerializer'+import { deserializeApiCanvas, type ApiNode, type ApiEdge } from '@/utils/canvasSerializer'import { computeCollapseInfo, rewireEdgesForCollapse } from '@/utils/collapseFilter'import { liveviewApi } from '@/api/client'import * as standaloneStorage from '@/utils/standaloneStorage'@@ -80,18 +80,10 @@ function LiveViewCanvas() {liveviewApi.load(key, design).then((res) => {const { nodes: apiNodes, edges: apiEdges } = res.data- const proxmoxMap = new Map<string, boolean>(- (apiNodes as ApiNode[])- .filter((n: ApiNode) => n.type === 'group' || n.container_mode === true)- .map((n: ApiNode) => [n.id, true])- )const savedTheme = res.data.viewport?.theme_idif (savedTheme) setTheme(savedTheme)if (res.data.custom_style) setCustomStyle(res.data.custom_style as CustomStyleDef)- const migrated = migrateClusterHandles(- (apiNodes as ApiNode[]).map((n) => deserializeApiNode(n, proxmoxMap)),- (apiEdges as ApiEdge[]).map(deserializeApiEdge),- )+ const migrated = deserializeApiCanvas(apiNodes as ApiNode[], apiEdges as ApiEdge[])loadCanvas(migrated.nodes, migrated.edges)setViewState('ready')})diff --git a/frontend/src/components/__tests__/LiveView.test.tsx b/frontend/src/components/__tests__/LiveView.test.tsxindex f230501..73a1494 100644--- a/frontend/src/components/__tests__/LiveView.test.tsx+++ b/frontend/src/components/__tests__/LiveView.test.tsx@@ -182,6 +182,38 @@ describe('LiveView (non-standalone)', () => {expect(ctr?.extent).toBe('parent')})+ // ── Zone children (#524) ─────────────────────────────────────────────────++ it('keeps a node parented to its zone, unclamped, like the editor does', async () => {+ setSearch('?key=valid')+ const zonePayload = {+ data: {+ nodes: [+ {+ id: 'zone', type: 'groupRect', label: 'Heimkino', status: 'unknown',+ services: [], pos_x: 600, pos_y: 400, width: 200, height: 300,+ created_at: '2024-01-01T00:00:00Z', updated_at: '2024-01-01T00:00:00Z',+ },+ {+ id: 'avr', type: 'server', label: 'Denon AVR', status: 'online',+ services: [], pos_x: 10, pos_y: 30, parent_id: 'zone',+ created_at: '2024-01-01T00:00:00Z', updated_at: '2024-01-01T00:00:00Z',+ },+ ],+ edges: [],+ viewport: { x: 0, y: 0, zoom: 1 },+ },+ }+ vi.mocked(liveviewApi.load).mockResolvedValue(zonePayload as never)+ render(<LiveView />)+ await waitFor(() => expect(screen.getByTestId('react-flow')).toBeDefined())+ const avr = useCanvasStore.getState().nodes.find((n) => n.id === 'avr')+ // Without parentId the zone-relative position renders as absolute.+ expect(avr?.parentId).toBe('zone')+ expect(avr?.extent).toBeUndefined()+ expect(avr?.position).toEqual({ x: 10, y: 30 })+ })+// ── Theme + custom_style applied from payload ────────────────────────────it('applies viewport.theme_id and custom_style from the payload', async () => {diff --git a/frontend/src/utils/__tests__/canvasSerializer.test.ts b/frontend/src/utils/__tests__/canvasSerializer.test.tsindex 88d8885..d9ad18c 100644--- a/frontend/src/utils/__tests__/canvasSerializer.test.ts+++ b/frontend/src/utils/__tests__/canvasSerializer.test.ts@@ -7,6 +7,7 @@ import {deserializeApiNode,deserializeApiEdge,migrateClusterHandles,+ deserializeApiCanvas,type ApiNode,type ApiEdge,} from '@/utils/canvasSerializer'@@ -607,6 +608,52 @@ describe('migrateClusterHandles', () => {})})+describe('deserializeApiCanvas', () => {+ it('parents a zone child without clamping it (#524)', () => {+ const { nodes } = deserializeApiCanvas([+ makeApiNode({ id: 'zone', type: 'groupRect', width: 200, height: 300 }),+ makeApiNode({ id: 'child', parent_id: 'zone', pos_x: 10, pos_y: 30 }),+ ], [])+ const child = nodes.find((n) => n.id === 'child')!+ expect(child.parentId).toBe('zone')+ expect(child.extent).toBeUndefined()+ expect(child.position).toEqual({ x: 10, y: 30 })+ })++ it('parents and clamps a container child (group or container_mode)', () => {+ const { nodes } = deserializeApiCanvas([+ makeApiNode({ id: 'grp', type: 'group' }),+ makeApiNode({ id: 'host', type: 'proxmox', container_mode: true }),+ makeApiNode({ id: 'a', parent_id: 'grp' }),+ makeApiNode({ id: 'b', type: 'vm', parent_id: 'host' }),+ ], [])+ for (const id of ['a', 'b']) {+ const n = nodes.find((x) => x.id === id)!+ expect(n.extent).toBe('parent')+ }+ expect(nodes.find((n) => n.id === 'a')!.parentId).toBe('grp')+ expect(nodes.find((n) => n.id === 'b')!.parentId).toBe('host')+ })++ it('drops a parent_id that points at neither a container nor a zone', () => {+ const { nodes } = deserializeApiCanvas([+ makeApiNode({ id: 'srv' }),+ makeApiNode({ id: 'child', parent_id: 'srv' }),+ ], [])+ expect(nodes.find((n) => n.id === 'child')!.parentId).toBeUndefined()+ })++ it('deserializes edges and migrates legacy cluster handles', () => {+ const { edges, nodes } = deserializeApiCanvas(+ [makeApiNode({ id: 'p1', type: 'proxmox' }), makeApiNode({ id: 'p2', type: 'proxmox' })],+ [makeApiEdge({ id: 'c1', source: 'p1', target: 'p2', type: 'cluster', source_handle: 'cluster-right', target_handle: 'cluster-left' })],+ )+ expect(edges[0].sourceHandle).toBe('right')+ expect(edges[0].targetHandle).toBe('left')+ expect(nodes.find((n) => n.id === 'p1')!.data.right_handles).toBe(1)+ })+})+// ── Furniture descriptions ───────────────────────────────────────────────────describe('furniture descriptions', () => {diff --git a/frontend/src/utils/canvasSerializer.ts b/frontend/src/utils/canvasSerializer.tsindex 7e1877a..290e908 100644--- a/frontend/src/utils/canvasSerializer.ts+++ b/frontend/src/utils/canvasSerializer.ts@@ -326,3 +326,25 @@ export function migrateClusterHandles(return { nodes: migratedNodes, edges: migratedEdges }}++/**+ * Turn a canvas API payload into React Flow nodes/edges. The single entry point+ * for every view that renders a saved canvas (editor, live view), so the+ * parent maps a node needs to keep its `parentId` — containers and zones —+ * cannot drift between them.+ */+export function deserializeApiCanvas(+ apiNodes: ApiNode[],+ apiEdges: ApiEdge[],+): { nodes: Node<NodeData>[]; edges: Edge<EdgeData>[] } {+ const proxmoxContainerMap = new Map<string, boolean>(+ apiNodes+ .filter((n) => n.type === 'group' || n.container_mode === true)+ .map((n) => [n.id, true])+ )+ const zoneIds = new Set(apiNodes.filter((n) => n.type === 'groupRect').map((n) => n.id))+ return migrateClusterHandles(+ apiNodes.map((n) => deserializeApiNode(n, proxmoxContainerMap, zoneIds)),+ apiEdges.map(deserializeApiEdge),+ )+}