Skip to content

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

fix(rack): stop a blanked height field from shrinking the rack

Pouzor57df3b9198d6
diff --git a/frontend/src/components/common/PropertyList.tsx b/frontend/src/components/common/PropertyList.tsx
index 8703a78..a464e23 100644
--- a/frontend/src/components/common/PropertyList.tsx
+++ b/frontend/src/components/common/PropertyList.tsx
@@ -62,7 +62,11 @@ export function PropertyList({
const handleRemove = (index: number) => {
onChange(properties.filter((_, i) => i !== index))
- if (editingIndex === index) setEditingIndex(null)
+ // `editingIndex` is a position into an array the caller owns, so any
+ // mutation from outside the form invalidates it: removing an earlier row
+ // shifted the open form onto its neighbour, and Save then overwrote the
+ // wrong property. Close the form rather than try to track the move.
+ setEditingIndex(null)
}
const handleToggleVisible = (index: number) => {
@@ -95,6 +99,8 @@ export function PropertyList({
const [moved] = reordered.splice(from, 1)
reordered.splice(to, 0, moved)
onChange(reordered)
+ // Same positional hazard as a remove — see handleRemove.
+ setEditingIndex(null)
}
const startAddWith = (key: string) => {
diff --git a/frontend/src/components/common/__tests__/PropertyList.test.tsx b/frontend/src/components/common/__tests__/PropertyList.test.tsx
new file mode 100644
index 0000000..f4503f4
--- /dev/null
+++ b/frontend/src/components/common/__tests__/PropertyList.test.tsx
@@ -0,0 +1,123 @@
+/**
+ * Shared property editor — used by the node DetailPanel and the rack cable
+ * panel, so a positional bug here lands on both canvases.
+ */
+import { describe, it, expect, vi } from 'vitest'
+import { render, screen, fireEvent } from '@testing-library/react'
+import { PropertyList } from '../PropertyList'
+import type { NodeProperty } from '@/types'
+
+const props = (): NodeProperty[] => [
+ { key: 'A', value: '1', icon: null, visible: true },
+ { key: 'B', value: '2', icon: null, visible: true },
+ { key: 'C', value: '3', icon: null, visible: true },
+]
+
+/** The form's confirm button; the section header carries an "Add" too. */
+const confirm = (name: string) => {
+ const buttons = screen.getAllByRole('button', { name })
+ return buttons[buttons.length - 1]
+}
+
+describe('PropertyList', () => {
+ it('renders each property with its key and value', () => {
+ render(<PropertyList properties={props()} onChange={vi.fn()} />)
+ expect(screen.getByTitle('A')).toBeInTheDocument()
+ expect(screen.getByTitle('3')).toBeInTheDocument()
+ })
+
+ it('adds a property', () => {
+ const onChange = vi.fn()
+ render(<PropertyList properties={[]} onChange={onChange} />)
+
+ fireEvent.click(screen.getByRole('button', { name: 'Add' }))
+ fireEvent.change(screen.getByPlaceholderText(/^Label/), { target: { value: 'GPU' } })
+ fireEvent.change(screen.getByPlaceholderText(/^Value/), { target: { value: 'RTX' } })
+ fireEvent.click(confirm('Add'))
+
+ expect(onChange).toHaveBeenCalledWith([
+ { key: 'GPU', value: 'RTX', icon: null, visible: true },
+ ])
+ })
+
+ it('refuses a property with no key or no value', () => {
+ const onChange = vi.fn()
+ render(<PropertyList properties={[]} onChange={onChange} />)
+
+ fireEvent.click(screen.getByRole('button', { name: 'Add' }))
+ fireEvent.change(screen.getByPlaceholderText(/^Label/), { target: { value: 'GPU' } })
+ fireEvent.click(confirm('Add'))
+
+ expect(onChange).not.toHaveBeenCalled()
+ })
+
+ it('toggles a property between shown and hidden', () => {
+ const onChange = vi.fn()
+ render(<PropertyList properties={props()} onChange={onChange} />)
+
+ fireEvent.click(screen.getAllByTitle('Hide on node')[1])
+ expect(onChange.mock.calls[0][0][1].visible).toBe(false)
+ })
+
+ it('closes the open edit form when another property is removed', () => {
+ // The form index is a position into an array the caller owns. Removing an
+ // earlier row shifted the open form onto its neighbour, and Save then
+ // overwrote the wrong property.
+ const onChange = vi.fn()
+ const { rerender } = render(<PropertyList properties={props()} onChange={onChange} />)
+
+ fireEvent.click(screen.getAllByTitle('Edit property')[1]) // editing B
+ expect(screen.getByDisplayValue('B')).toBeInTheDocument()
+
+ fireEvent.click(screen.getAllByTitle('Remove property')[0]) // remove A
+ const next = onChange.mock.calls[0][0]
+ rerender(<PropertyList properties={next} onChange={onChange} />)
+
+ // No form at all — index 1 now points at C, so a surviving form would sit
+ // on the wrong property and Save would overwrite it.
+ expect(screen.queryByRole('button', { name: 'Save' })).toBeNull()
+ expect(next.map((p: NodeProperty) => p.key)).toEqual(['B', 'C'])
+ })
+
+ it('closes the open edit form when the list is reordered', () => {
+ const onChange = vi.fn()
+ const { rerender } = render(<PropertyList properties={props()} onChange={onChange} />)
+
+ fireEvent.click(screen.getAllByTitle('Edit property')[2]) // editing C
+ expect(screen.getByDisplayValue('C')).toBeInTheDocument()
+
+ const rows = screen.getAllByTitle('Drag to reorder')
+ fireEvent.dragStart(rows[0])
+ fireEvent.dragEnter(rows[1])
+ fireEvent.drop(rows[1])
+
+ rerender(<PropertyList properties={onChange.mock.calls[0][0]} onChange={onChange} />)
+ expect(screen.queryByRole('button', { name: 'Save' })).toBeNull()
+ })
+
+ it('saves an edit onto the property it was opened on', () => {
+ const onChange = vi.fn()
+ render(<PropertyList properties={props()} onChange={onChange} />)
+
+ fireEvent.click(screen.getAllByTitle('Edit property')[1])
+ fireEvent.change(screen.getByDisplayValue('2'), { target: { value: '22' } })
+ fireEvent.click(confirm('Save'))
+
+ expect(onChange.mock.calls[0][0]).toEqual([
+ { key: 'A', value: '1', icon: null, visible: true },
+ { key: 'B', value: '22', icon: null, visible: true },
+ { key: 'C', value: '3', icon: null, visible: true },
+ ])
+ })
+
+ it('words the visibility toggle for the surface it edits', () => {
+ render(<PropertyList properties={props()} onChange={vi.fn()} visibleLabel="Show on canvas" />)
+ expect(screen.getAllByTitle('Hide on canvas')).toHaveLength(3)
+ })
+
+ it('pre-fills the add form from a suggestion', () => {
+ render(<PropertyList properties={[]} onChange={vi.fn()} suggestions={['Length']} />)
+ fireEvent.click(screen.getByText('+ Length'))
+ expect(screen.getByDisplayValue('Length')).toBeInTheDocument()
+ })
+})
diff --git a/frontend/src/rack/README.md b/frontend/src/rack/README.md
index f299233..773db17 100644
--- a/frontend/src/rack/README.md
+++ b/frontend/src/rack/README.md
@@ -24,7 +24,7 @@ Create one from **New Canvas → Kind → Rack**.
| Port visibility | Patch-facing gear (switches, patch panels) shows its ports permanently. Everything else reveals them on hover, on selection, or when cables are on — a cable never ends on an invisible port. |
| Plate zones | Each template reserves three non-overlapping bands: status LED (fixed left), `labelBox` (name, clipped), then artwork and ports. A test asserts no port lands on the name band. `labelBox.y` (default 0.5) moves the name band *and* the LED off mid-height — desktop NAS boxes are drive doors over a badge strip at the bottom. |
| Cables | Port-to-port, a relation of their own — not React Flow edges. One cable per port. Drawn in a `ViewportPortal` so they pan/zoom with the canvas and can cross racks. |
-| Cable visibility | Hidden by default; shown on hover/selection, or all-on via patch mode / the header. Plates stay opaque in every mode — fading them to 40 % showed the rails and the U grid through the gear. Copper vs fibre follows the port the patch starts from. A selected cable is drawn whatever the mode — its panel is open on the right, so hiding the run it describes would read as a bug. |
+| Cable visibility | Hidden by default; shown on hover/selection, or all-on via patch mode / the header. Plates stay opaque in every mode — fading them to 40 % showed the rails and the U grid through the gear. Copper vs fibre follows the port the patch starts from. A selected cable is drawn whatever the mode, `hidden` included — its panel is open on the right, so hiding the run it describes would read as a bug — but `hidden` reveals nothing else, not even on hover. |
| Cable annotations | A cable carries a `label` plus `properties` — the same `NodeProperty` records the logical canvas uses (`key`, `value`, `icon`, `visible`), edited with the shared `components/common/PropertyList`. `labelVisible` and each property's `visible` decide what is printed on the canvas: `CableLayer` draws the lines on a small plate at the midpoint of the run (the cubic solved at `t = 0.5`, not `getPointAtLength`, which needs a mounted path). |
| Colours | `rackTheme.ts` derives the whole rack palette from the active app theme rather than declaring one per theme, so a new theme works here for free. Per-rack chrome (frame, rails, interior) stays user-editable; only its default comes from the theme. |
diff --git a/frontend/src/rack/__tests__/CableLayer.test.tsx b/frontend/src/rack/__tests__/CableLayer.test.tsx
index e207d50..246f500 100644
--- a/frontend/src/rack/__tests__/CableLayer.test.tsx
+++ b/frontend/src/rack/__tests__/CableLayer.test.tsx
@@ -38,6 +38,33 @@ describe('CableLayer selection', () => {
expect(useRackStore.getState().selectedCableId).toBe(useRackStore.getState().cables[0].id)
})
+ it('keeps the selected cable drawn when cables are hidden', () => {
+ // Its panel is open on the right; hiding the run being edited reads as a
+ // bug. The header select used to make the selection vanish under the panel.
+ const store = useRackStore.getState()
+ store.selectCable(store.cables[0].id)
+ store.setCableVisibility('hidden')
+
+ const { container } = render(<CableLayer />)
+ expect(hitPaths(container)).toHaveLength(1)
+ })
+
+ it('draws nothing else when cables are hidden, even on hover', () => {
+ const store = useRackStore.getState()
+ store.setCableVisibility('hidden')
+ store.hoverDevice(store.cables[0].from.deviceId)
+ store.selectCable(store.cables[0].id)
+
+ const { container } = render(<CableLayer />)
+ expect(hitPaths(container)).toHaveLength(1)
+ })
+
+ it('draws nothing when cables are hidden and none is selected', () => {
+ useRackStore.getState().setCableVisibility('hidden')
+ const { container } = render(<CableLayer />)
+ expect(container).toBeEmptyDOMElement()
+ })
+
it('keeps the selected cable drawn when visibility is hover and nothing is focused', () => {
const store = useRackStore.getState()
store.setCableVisibility('hover')
diff --git a/frontend/src/rack/__tests__/RackSettingsModal.test.tsx b/frontend/src/rack/__tests__/RackSettingsModal.test.tsx
index c7f3762..fef544c 100644
--- a/frontend/src/rack/__tests__/RackSettingsModal.test.tsx
+++ b/frontend/src/rack/__tests__/RackSettingsModal.test.tsx
@@ -83,6 +83,46 @@ describe('RackSettingsModal', () => {
expect((height as HTMLInputElement).value).toBe('18')
})
+ it('treats a blanked height as no edit', () => {
+ // Number('') is 0, which used to fall through to 1 U: select-all, Delete,
+ // click elsewhere, and an empty rack silently became 1 U with no undo.
+ store().openRackEditor('rack-main')
+ render(<RackSettingsModal />)
+
+ const height = screen.getByLabelText('Rack height')
+ fireEvent.change(height, { target: { value: '' } })
+ fireEvent.blur(height)
+
+ expect(store().racks[0].uHeight).toBe(18)
+ expect((height as HTMLInputElement).value).toBe('18')
+ })
+
+ it('treats a blanked height as no edit on an empty rack too', () => {
+ // The dangerous case: with no mounts, updateRack has nothing to refuse.
+ const id = store().addRack({ name: 'Empty' })
+ const height = () => store().racks.find((r) => r.id === id)!.uHeight
+ const before = height()
+ store().openRackEditor(id)
+ render(<RackSettingsModal />)
+
+ const field = screen.getByLabelText('Rack height')
+ fireEvent.change(field, { target: { value: '' } })
+ fireEvent.blur(field)
+
+ expect(height()).toBe(before)
+ })
+
+ it('ignores a height that does not parse', () => {
+ store().openRackEditor('rack-main')
+ render(<RackSettingsModal />)
+
+ const height = screen.getByLabelText('Rack height')
+ fireEvent.change(height, { target: { value: 'abc' } })
+ fireEvent.blur(height)
+
+ expect(store().racks[0].uHeight).toBe(18)
+ })
+
it('commits nothing when the dialog closes on a typed height', () => {
// Escape closes the dialog; the uncommitted draft dies with the field.
store().openRackEditor('rack-main')
diff --git a/frontend/src/rack/components/CableLayer.tsx b/frontend/src/rack/components/CableLayer.tsx
index 8af8eee..8fc26f7 100644
--- a/frontend/src/rack/components/CableLayer.tsx
+++ b/frontend/src/rack/components/CableLayer.tsx
@@ -66,7 +66,10 @@ export function CableLayer() {
const cableDrag = useRackStore((s) => s.cableDrag)
const palette = useRackPalette()
- if (visibility === 'hidden' && !cableMode) return null
+ // `hidden` still draws the selected cable: its panel is open on the right and
+ // editing a run nothing shows reads as a bug. The per-cable filter below is
+ // what decides; this only skips the work when there is nothing to draw.
+ if (visibility === 'hidden' && !cableMode && !selectedCableId) return null
const rackById = new Map<string, Rack>(racks.map((r) => [r.id, r]))
const deviceById = new Map<string, RackDevice>(devices.map((d) => [d.id, d]))
@@ -87,6 +90,9 @@ export function CableLayer() {
// A selected cable stays on screen whatever the visibility mode — its panel
// is open on the right, so hiding the run it describes reads as a bug.
if (!showAll && cable.id !== selectedCableId) {
+ // `hidden` means hidden: hovering a plate reveals nothing extra, or the
+ // selection would drag its neighbours back on screen with it.
+ if (visibility === 'hidden') continue
if (!focus) continue
if (cable.from.deviceId !== focus && cable.to.deviceId !== focus) continue
}
diff --git a/frontend/src/rack/components/RackSettingsModal.tsx b/frontend/src/rack/components/RackSettingsModal.tsx
index 2797d09..16540f3 100644
--- a/frontend/src/rack/components/RackSettingsModal.tsx
+++ b/frontend/src/rack/components/RackSettingsModal.tsx
@@ -176,8 +176,11 @@ function HeightField({ rackId, uHeight }: { rackId: string; uHeight: number }) {
}
const commit = () => {
- const next = Number(draft) || MIN_RACK_U
- if (next === uHeight) {
+ // An empty or unparseable field is "no edit", not "1 U". `Number('')` is 0,
+ // so a select-all + Delete followed by a click elsewhere used to shrink an
+ // empty rack to a single U — silently, with no undo behind it.
+ const next = Number(draft.trim())
+ if (!draft.trim() || !Number.isFinite(next) || next === uHeight) {
setDraft(String(uHeight))
return
}