Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring
TypeScript diagramhomelabhomelab-infrastructure-observabilitynetworkproxmox-veself-hostedzigbeezwavezwave-js
fix(canvas): keep a node inside its group when editing it
Pouzor26e18dafdd70
diff --git a/frontend/src/components/modals/NodeModal.tsx b/frontend/src/components/modals/NodeModal.tsxindex 8d6adc4..88ae47e 100644--- a/frontend/src/components/modals/NodeModal.tsx+++ b/frontend/src/components/modals/NodeModal.tsx@@ -13,7 +13,7 @@ import { resolveNodeColors } from '@/utils/nodeColors'import { ICON_REGISTRY, NODE_TYPE_DEFAULT_ICONS, isBrandIconKey, brandIconSlug, brandIconUrl } from '@/utils/nodeIcons'import { IconPickerPanel } from './IconPickerPanel'import { MAX_HANDLES, MIN_HANDLES, clampHandles, sideDefault, handleCountField, type Side } from '@/utils/handleUtils'-import { getValidParentTypes } from '@/utils/virtualEdgeParent'+import { isValidParentNode } from '@/utils/virtualEdgeParent'const NODE_TYPE_GROUPS: { label: string; types: NodeType[] }[] = [{ label: 'Hardware', types: ['isp', 'router', 'firewall', 'switch', 'server', 'nas', 'kvm', 'ap', 'printer'] },@@ -174,15 +174,10 @@ export function NodeModal({ open, onClose, onSubmit, initial, title = 'Add Node'setLabelError(false)const selectedType = (form.type ?? 'generic') as NodeTypeconst canUseContainerMode = CONTAINER_MODE_TYPES.includes(selectedType)- const validParentTypes = getValidParentTypes(selectedType)- // A parent is valid either by the type rules (lxc/vm/docker_container) or- // because the candidate is a container-mode node (any child can nest in it).- const isValidParent = (p: ParentCandidate) =>- validParentTypes.includes(p.type) || p.container_mode === truelet safeParentId = form.parent_idif (safeParentId) {const parent = parentCandidates.find((n) => n.id === safeParentId)- if (!parent || !isValidParent(parent)) safeParentId = undefined+ if (!parent || !isValidParentNode(selectedType, parent)) safeParentId = undefined}const isGroupType = selectedType === 'groupRect' || selectedType === 'group'onSubmit({@@ -225,7 +220,7 @@ export function NodeModal({ open, onClose, onSubmit, initial, title = 'Add Node'// Drop the parent only if it's no longer a valid target for the// new type — keep container-mode parents (any node can nest).const parent = parentCandidates.find((n) => n.id === f.parent_id)- if (f.parent_id && !(parent && (getValidParentTypes(t).includes(parent.type) || parent.container_mode === true))) {+ if (f.parent_id && !(parent && isValidParentNode(t, parent))) {next.parent_id = undefined}return next@@ -377,13 +372,13 @@ export function NodeModal({ open, onClose, onSubmit, initial, title = 'Add Node'{/* Parent Container */}{(() => {const childType = (form.type ?? 'generic') as NodeType- const validParentTypes = getValidParentTypes(childType)- // Candidates: type-based parents (lxc/vm/docker_container) plus any- // container-mode node. The current parent is always kept so an- // already-nested node can be re-targeted or detached here.+ // Candidates: type-based parents (lxc/vm/docker_container), any+ // container-mode node, and visual groups. The current parent is+ // always kept so an already-nested node can be re-targeted or+ // detached here.const validParents = parentCandidates.filter((n) => n.id !== currentNodeId &&- (validParentTypes.includes(n.type) || n.container_mode === true || n.id === form.parent_id),+ (isValidParentNode(childType, n) || n.id === form.parent_id),)if (validParents.length === 0) return nullreturn (diff --git a/frontend/src/components/modals/__tests__/NodeModal.test.tsx b/frontend/src/components/modals/__tests__/NodeModal.test.tsxindex e78be12..0d2a753 100644--- a/frontend/src/components/modals/__tests__/NodeModal.test.tsx+++ b/frontend/src/components/modals/__tests__/NodeModal.test.tsx@@ -424,6 +424,33 @@ describe('NodeModal', () => {expect((onSubmit.mock.calls[0][0] as Partial<NodeData>).parent_id).toBeUndefined()})+ it('keeps a group parent_id on submit for a plain node', () => {+ const { onSubmit } = renderModal({+ initial: { ...BASE, type: 'server', parent_id: 'g1' },+ parentCandidates: [{ id: 'g1', label: 'My Group', type: 'group' }],+ })+ fireEvent.click(screen.getByRole('button', { name: 'Add' }))+ expect((onSubmit.mock.calls[0][0] as Partial<NodeData>).parent_id).toBe('g1')+ })++ it('keeps a group parent_id when the type changes', () => {+ const { onSubmit } = renderModal({+ initial: { ...BASE, type: 'server', parent_id: 'g1' },+ parentCandidates: [{ id: 'g1', label: 'My Group', type: 'group' }],+ })+ fireEvent.change(selects()[0], { target: { value: 'nas' } })+ fireEvent.click(screen.getByRole('button', { name: 'Add' }))+ expect((onSubmit.mock.calls[0][0] as Partial<NodeData>).parent_id).toBe('g1')+ })++ it('offers a group as a parent for a plain node', () => {+ renderModal({+ initial: { ...BASE, type: 'server' },+ parentCandidates: [{ id: 'g1', label: 'My Group', type: 'group' }],+ })+ expect(screen.getByText('Parent Container')).toBeDefined()+ })+// ── Appearance ────────────────────────────────────────────────────────it('renders 3 color swatch labels (border, background, icon)', () => {diff --git a/frontend/src/stores/__tests__/canvasStore/containers.test.ts b/frontend/src/stores/__tests__/canvasStore/containers.test.tsindex 501f910..0e46b00 100644--- a/frontend/src/stores/__tests__/canvasStore/containers.test.ts+++ b/frontend/src/stores/__tests__/canvasStore/containers.test.ts@@ -83,6 +83,47 @@ describe('canvasStore — containers & nesting', () => {expect(node?.position.y).toBe(80)})+ it('updateNode setting parent_id on a group nests the node visually', () => {+ const group = { ...makeNode('g1', { type: 'group' }), position: { x: 100, y: 100 } }+ const child = { ...makeNode('s1', { type: 'server' }), position: { x: 160, y: 180 } }+ useCanvasStore.getState().addNode(group)+ useCanvasStore.getState().addNode(child)+ useCanvasStore.getState().updateNode('s1', { parent_id: 'g1' })+ const node = useCanvasStore.getState().nodes.find((n) => n.id === 's1')+ expect(node?.parentId).toBe('g1')+ expect(node?.extent).toBe('parent')+ expect(node?.position).toEqual({ x: 60, y: 80 })+ })++ it('updateNode re-sending an unchanged group parent_id keeps the node in place', () => {+ const group = { ...makeNode('g1', { type: 'group' }), position: { x: 100, y: 100 } }+ const child = {+ ...makeNode('s1', { type: 'server', parent_id: 'g1' }),+ parentId: 'g1',+ extent: 'parent' as const,+ position: { x: 20, y: 30 },+ }+ useCanvasStore.setState({ nodes: [group, child] })+ // What the node modal submits on an unrelated edit: label plus the parent it+ // already had. It must not detach or re-offset the node (#332 follow-up).+ useCanvasStore.getState().updateNode('s1', { label: 'renamed', parent_id: 'g1' })+ const node = useCanvasStore.getState().nodes.find((n) => n.id === 's1')+ expect(node?.data.parent_id).toBe('g1')+ expect(node?.parentId).toBe('g1')+ expect(node?.extent).toBe('parent')+ expect(node?.position).toEqual({ x: 20, y: 30 })+ })++ it('addNode keeps a node nested when its parent is a group', () => {+ const group = { ...makeNode('g1', { type: 'group' }), position: { x: 100, y: 100 } }+ useCanvasStore.setState({ nodes: [group] })+ useCanvasStore.getState().addNode({ ...makeNode('s1', { type: 'server', parent_id: 'g1' }), position: { x: 160, y: 180 } })+ const node = useCanvasStore.getState().nodes.find((n) => n.id === 's1')+ expect(node?.parentId).toBe('g1')+ expect(node?.extent).toBe('parent')+ expect(node?.position).toEqual({ x: 60, y: 80 })+ })+it('updateNode setting parent_id on non-container proxmox does NOT set React Flow parentId', () => {const proxmox = { ...makeNode('px1', { type: 'proxmox', container_mode: false }), position: { x: 100, y: 100 } }const lxc = { ...makeNode('lxc1', { type: 'lxc' }), position: { x: 160, y: 180 } }diff --git a/frontend/src/stores/canvasStore.ts b/frontend/src/stores/canvasStore.tsindex 0b6aa80..c15a9bd 100644--- a/frontend/src/stores/canvasStore.ts+++ b/frontend/src/stores/canvasStore.ts@@ -433,7 +433,8 @@ export const useCanvasStore = create<CanvasState>((rawSet) => {addNode: (node) =>set((state) => {const parent = node.data.parent_id ? state.nodes.find((n) => n.id === node.data.parent_id) : null- const shouldNestInParent = !!(parent?.data.container_mode)+ // A visual group nests its children just like a container-mode host.+ const shouldNestInParent = !!(parent?.data.container_mode) || parent?.data.type === 'group'const enriched = node.data.parent_id && shouldNestInParent? {...node,@@ -487,8 +488,8 @@ export const useCanvasStore = create<CanvasState>((rawSet) => {updated.extent = undefined} else if (newParentId && newParentId !== n.parentId) {const parent = state.nodes.find((p) => p.id === newParentId)- if (parent?.data.container_mode) {- // Attaching to a container-mode Proxmox: nest visually+ if (parent?.data.container_mode || parent?.data.type === 'group') {+ // Attaching to a container-mode host or a visual group: nest visuallyupdated.parentId = newParentIdupdated.extent = 'parent' as const// Convert absolute position to parent-relative (keep node visible inside)diff --git a/frontend/src/utils/__tests__/virtualEdgeParent.test.ts b/frontend/src/utils/__tests__/virtualEdgeParent.test.tsindex dae4707..36938a6 100644--- a/frontend/src/utils/__tests__/virtualEdgeParent.test.ts+++ b/frontend/src/utils/__tests__/virtualEdgeParent.test.ts@@ -1,5 +1,5 @@import { describe, it, expect } from 'vitest'-import { resolveVirtualEdgeParent, getValidParentTypes } from '../virtualEdgeParent'+import { resolveVirtualEdgeParent, getValidParentTypes, isValidParentNode } from '../virtualEdgeParent'describe('getValidParentTypes', () => {it('returns container-mode types for lxc', () => {@@ -22,6 +22,27 @@ describe('getValidParentTypes', () => {})})+describe('isValidParentNode', () => {+ it('accepts a group for any child type', () => {+ expect(isValidParentNode('server', { type: 'group' })).toBe(true)+ expect(isValidParentNode('router', { type: 'group' })).toBe(true)+ expect(isValidParentNode('lxc', { type: 'group' })).toBe(true)+ })++ it('accepts a container-mode node for any child type', () => {+ expect(isValidParentNode('server', { type: 'proxmox', container_mode: true })).toBe(true)+ })++ it('accepts a type-rule parent', () => {+ expect(isValidParentNode('lxc', { type: 'proxmox' })).toBe(true)+ })++ it('rejects a non-container node that no type rule allows', () => {+ expect(isValidParentNode('server', { type: 'proxmox', container_mode: false })).toBe(false)+ expect(isValidParentNode('server', { type: 'groupRect' })).toBe(false)+ })+})+describe('resolveVirtualEdgeParent', () => {it('nests lxc under proxmox (container-mode parent)', () => {const res = resolveVirtualEdgeParent(diff --git a/frontend/src/utils/virtualEdgeParent.ts b/frontend/src/utils/virtualEdgeParent.tsindex 838fef1..126ba0a 100644--- a/frontend/src/utils/virtualEdgeParent.ts+++ b/frontend/src/utils/virtualEdgeParent.ts@@ -25,6 +25,20 @@ export function getValidParentTypes(childType: NodeType): NodeType[] {return []}+/** Can `parent` hold a child of `childType`?+ *+ * A visual group holds any node type — the type rules above only govern+ * virtual (container) nesting, so they'd wrongly reject a grouped node and+ * drop it out of its group on the next edit.+ */+export function isValidParentNode(+ childType: NodeType,+ parent: { type: NodeType; container_mode?: boolean },+): boolean {+ if (parent.type === 'group') return true+ return getValidParentTypes(childType).includes(parent.type) || parent.container_mode === true+}+export function resolveVirtualEdgeParent(source: VirtualEdgeEndpoint,target: VirtualEdgeEndpoint,