Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring
TypeScript diagramhomelabhomelab-infrastructure-observabilitynetworkproxmox-veself-hostedzigbeezwavezwave-js
refactor(rack): lead with the faceplate, and make the port list a chip grid
Pouzorbffe1927ddcf
diff --git a/frontend/src/components/modals/__tests__/InventoryDeviceModalFaceplate.test.tsx b/frontend/src/components/modals/__tests__/InventoryDeviceModalFaceplate.test.tsxindex b924e3b..5fc3785 100644--- a/frontend/src/components/modals/__tests__/InventoryDeviceModalFaceplate.test.tsx+++ b/frontend/src/components/modals/__tests__/InventoryDeviceModalFaceplate.test.tsx@@ -91,8 +91,9 @@ describe('InventoryDeviceModal — rack faceplate', () => {fireEvent.click(screen.getByRole('button', { name: 'Position ports' }))const handle = screen.getByRole('button', { name: 'Move port eth0' })- expect(handle).toBeInTheDocument()- fireEvent.keyDown(handle, { key: 'ArrowRight' })+ fireEvent.pointerDown(handle)+ // Selected, so the arrows move it wherever focus happens to be.+ fireEvent.keyDown(document.body, { key: 'ArrowRight' })// The handle follows the port it moved, so the plate and the data agree.expect(Number.parseFloat(handle.style.left)).toBeCloseTo(21)})diff --git a/frontend/src/rack/README.md b/frontend/src/rack/README.mdindex b62efe3..dc16d72 100644--- a/frontend/src/rack/README.md+++ b/frontend/src/rack/README.md@@ -54,8 +54,8 @@ types in `@/types/rack`, narrowing every enum on the way in.- **+ Device** (left rail) → `RackDeviceModal`. Source is either an existing **Device Inventory** entry, a new device created here (which lands in the inventory too), or a rack-only accessory. Nothing is preselected: the entry is picked in the Device Inventory modal, which the field opens.- Drag from the sidebar tray onto a rack → snaps to a free U.- Drag a mounted device inside the rack → same snapping, own slot ignored.-- The **Faceplate** field names the current plate and opens `FaceplatePicker`; the plate itself is drawn full size at the bottom of the modal (`PortPositionEditor`, `interactive={false}`) — the old 160×18px thumbnail lost its label and stacked its ports. Switching the source between device and accessory swaps the plate to a sane default of the other kind.-- **Position** (port list header) turns that plate into the port editor: drag a handle to place a port, arrow keys nudge by 0.01, and each axis snaps to the nearest peer within 6px so banks stay in line. A new port continues the last row instead of landing on the middle of the plate. Editing a port's name highlights it on the plate. Positions are unit coordinates, so the blow-up never reaches the data.+- The **Faceplate** field names the current plate and opens `FaceplatePicker`; the plate itself is drawn full size at the *top* of the modal (`PortPositionEditor`, `interactive={false}`) — the old 160×18px thumbnail lost its label and stacked its ports. Switching the source between device and accessory swaps the plate to a sane default of the other kind.+- **Position** (port list header) turns that plate into the port editor: drag a handle to place a port, arrow keys nudge the *selected* port by 0.01 wherever focus sits (a text field keeps its own arrows), and each axis snaps to the nearest peer within 6px so banks stay in line. A new port continues the last row instead of landing on the middle of the plate. The port list itself is a two-column grid of 28px chips — a 48-port switch is a list, not a page — and clicking or typing in one highlights that port on the plate. Positions are unit coordinates, so the blow-up never reaches the data.- Double-click a plate → the same modal in edit mode: label, faceplate, U/height/column/width, status, colour, port list, the **Linked device** panel, Unmount. Single click only selects.- Double-click empty rack chrome → `RackSettingsModal` (name, location, U height, 19"/10", numbering direction, frame/rail/interior colours, U numbers, enclosed, delete).- Growing a device — by hand or by picking a taller plate — relocates it to the nearest slot that takes the new size. Only a rack with no such slot rejects the edit, and says so.diff --git a/frontend/src/rack/__tests__/PortPositionEditor.test.tsx b/frontend/src/rack/__tests__/PortPositionEditor.test.tsxindex e77b873..58866ae 100644--- a/frontend/src/rack/__tests__/PortPositionEditor.test.tsx+++ b/frontend/src/rack/__tests__/PortPositionEditor.test.tsx@@ -99,17 +99,34 @@ describe('PortPositionEditor', () => {expect(onSelect).toHaveBeenCalledWith('b')})- it('nudges the port with the arrow keys, for a placement no pointer can hit', () => {- const { onChange } = draw()- fireEvent.keyDown(screen.getByRole('button', { name: 'Move port eth0' }), { key: 'ArrowRight' })+ it('nudges the selected port with the arrow keys, wherever focus sits', () => {+ // Regression: the arrows only worked while the handle itself held focus, so+ // selecting a port in the list and reaching for them did nothing.+ const { onChange } = draw({ selectedPortId: 'a' })+ fireEvent.keyDown(document.body, { key: 'ArrowRight' })const moved = onChange.mock.calls.at(-1)![0] as Port[]expect(moved.find((p) => p.id === 'a')!.x).toBeCloseTo(0.21)})+ it('leaves the arrows to a text field being typed in', () => {+ const { onChange } = draw({ selectedPortId: 'a' })+ const field = document.createElement('input')+ document.body.appendChild(field)+ fireEvent.keyDown(field, { key: 'ArrowLeft' })+ expect(onChange).not.toHaveBeenCalled()+ field.remove()+ })++ it('does not move anything outside placement mode', () => {+ const { onChange } = draw({ interactive: false, selectedPortId: 'a' })+ fireEvent.keyDown(document.body, { key: 'ArrowRight' })+ expect(onChange).not.toHaveBeenCalled()+ })+it('ignores a key that means nothing here', () => {- const { onChange } = draw()- fireEvent.keyDown(screen.getByRole('button', { name: 'Move port eth0' }), { key: 'Enter' })+ const { onChange } = draw({ selectedPortId: 'a' })+ fireEvent.keyDown(document.body, { key: 'Enter' })expect(onChange).not.toHaveBeenCalled()})diff --git a/frontend/src/rack/__tests__/RackDeviceModal.test.tsx b/frontend/src/rack/__tests__/RackDeviceModal.test.tsxindex c320e51..b165128 100644--- a/frontend/src/rack/__tests__/RackDeviceModal.test.tsx+++ b/frontend/src/rack/__tests__/RackDeviceModal.test.tsx@@ -584,7 +584,9 @@ describe('RackDeviceModal — editing', () => {const name = screen.getByLabelText(`Port ${port.label} label`)expect(name).toHaveClass('flex-1', 'min-w-0')expect(name).not.toHaveClass('w-full')- expect(name).toHaveAttribute('placeholder', 'Port name')+ // The chip layout shortened the placeholder; the field must still be the+ // part of the row that stretches.+ expect(name).toHaveAttribute('placeholder', 'Name')})it('unmounts without dropping the inventory entry', async () => {diff --git a/frontend/src/rack/components/DeviceFaceplateEditor.tsx b/frontend/src/rack/components/DeviceFaceplateEditor.tsxindex 645b161..99d7ca9 100644--- a/frontend/src/rack/components/DeviceFaceplateEditor.tsx+++ b/frontend/src/rack/components/DeviceFaceplateEditor.tsx@@ -50,6 +50,28 @@ export function DeviceFaceplateEditor({ value, label, status, editable, onChangereturn (<div className="flex flex-col gap-3">+ {/* The plate leads, as it does in the rack's own device modal. */}+ <div className="flex flex-col items-center gap-2 rounded border border-[#30363d] bg-[#0d1117] p-3">+ <PortPositionEditor+ faceplateId={value.faceplateId}+ label={label}+ status={status}+ ports={value.ports}+ uHeight={value.uHeight}+ colSpan={value.colSpan}+ color={value.color ?? undefined}+ interactive={editable && positioning}+ selectedPortId={selectedPortId}+ onSelect={setSelectedPortId}+ onChange={(ports) => onChange({ ...value, ports })}+ />+ {!editable && (+ <p className="text-[11px] text-muted-foreground">+ {plate.label} · {value.uHeight}U · {value.ports.length} port+ {value.ports.length === 1 ? '' : 's'}+ </p>+ )}+ </div>{editable && (<><div className="flex flex-col gap-1.5">@@ -139,27 +161,6 @@ export function DeviceFaceplateEditor({ value, label, status, editable, onChange</>)}- <div className="flex flex-col items-center gap-2 rounded border border-[#30363d] bg-[#0d1117] p-3">- <PortPositionEditor- faceplateId={value.faceplateId}- label={label}- status={status}- ports={value.ports}- uHeight={value.uHeight}- colSpan={value.colSpan}- color={value.color ?? undefined}- interactive={editable && positioning}- selectedPortId={selectedPortId}- onSelect={setSelectedPortId}- onChange={(ports) => onChange({ ...value, ports })}- />- {!editable && (- <p className="text-[11px] text-muted-foreground">- {plate.label} · {value.uHeight}U · {value.ports.length} port- {value.ports.length === 1 ? '' : 's'}- </p>- )}- </div></div>)}diff --git a/frontend/src/rack/components/PortListEditor.tsx b/frontend/src/rack/components/PortListEditor.tsxindex e358766..5d46734 100644--- a/frontend/src/rack/components/PortListEditor.tsx+++ b/frontend/src/rack/components/PortListEditor.tsx@@ -12,9 +12,6 @@ import { nextPortSpot } from '../portLayout'import { generateUUID } from '@/utils/uuid'import type { Port, PortType } from '@/types'-const inputBase =- 'rounded border border-[#30363d] bg-[#21262d] px-2 py-1 text-sm text-foreground outline-none focus:border-[#00d4ff]'-const PORT_TYPES: PortType[] = ['rj45', 'sfp', 'sfp+']interface Props {@@ -83,28 +80,33 @@ export function PortListEditor({</button></div></div>- <ul className="max-h-[26rem] space-y-1 overflow-y-auto">+ {/* Two columns of one-line chips: a 24-port switch used to draw 24 full+ rows of full-height inputs, so the list ran a metre down the modal and+ buried everything under it. Name, type and remove all fit on one 28px+ line, and two columns halve the scroll again. */}+ <ul className="grid max-h-[19rem] grid-cols-2 gap-1 overflow-y-auto pr-0.5">{ports.map((port) => (<likey={port.id}- // Editing a port's name highlights it on the plate, so the list and- // the drawing never disagree about which port is which.+ // Editing or clicking a port highlights it on the plate — and, in+ // placement mode, the arrow keys then move that one.onFocusCapture={() => onSelect(port.id)}- className={`flex items-center gap-1 rounded border px-1 py-1 ${- selectedPortId === port.id ? 'border-[#00d4ff] bg-[#00d4ff0f]' : 'border-transparent'+ onClick={() => onSelect(port.id)}+ className={`flex h-7 items-center gap-0.5 rounded border bg-[#1c2128] pr-0.5 ${+ selectedPortId === port.id ? 'border-[#00d4ff]' : 'border-[#30363d]'}`}><input- // Without min-w-0 the flex row lets the type select win the space- // and the name field collapses into an unlabelled box.- className={`${inputBase} min-w-0 flex-1`}+ // Borderless inside the chip: a box drawn inside a box is what+ // made the old rows twice as tall as they needed to be.+ className="h-6 min-w-0 flex-1 bg-transparent px-1.5 text-xs text-foreground outline-none"aria-label={`Port ${port.label} label`}- placeholder="Port name"+ placeholder="Name"value={port.label}onChange={(e) => patch(port.id, { label: e.target.value })}/><select- className={`${inputBase} w-24 shrink-0`}+ className="h-6 shrink-0 cursor-pointer rounded bg-transparent text-[11px] text-muted-foreground outline-none hover:text-foreground"aria-label={`Port ${port.label} type`}value={port.type}onChange={(e) => patch(port.id, { type: e.target.value as PortType })}@@ -118,15 +120,15 @@ export function PortListEditor({<buttontype="button"aria-label={`Remove port ${port.label}`}- className="cursor-pointer px-1 text-xs text-muted-foreground hover:text-[#f85149]"+ className="cursor-pointer px-0.5 text-muted-foreground hover:text-[#f85149]"onClick={() => onChange(ports.filter((p) => p.id !== port.id))}>- <X size={12} />+ <X size={11} /></button></li>))}{ports.length === 0 && (- <li className="text-[11px] text-muted-foreground">No port on this plate.</li>+ <li className="col-span-2 text-[11px] text-muted-foreground">No port on this plate.</li>)}</ul></div>diff --git a/frontend/src/rack/components/PortPositionEditor.tsx b/frontend/src/rack/components/PortPositionEditor.tsxindex 0809dc8..fa3564b 100644--- a/frontend/src/rack/components/PortPositionEditor.tsx+++ b/frontend/src/rack/components/PortPositionEditor.tsx@@ -7,7 +7,7 @@* axes scale together for the same reason: stretching one would move the ports* relative to the artwork they sit on.*/-import { useCallback, useRef, useState } from 'react'+import { useCallback, useEffect, useRef, useState } from 'react'import { Faceplate } from './Faceplate'import { INNER_WIDTH_PX, U_PX } from '../layout'import { clampPort, snapThreshold, snapToPeers } from '../portLayout'@@ -31,6 +31,13 @@ const MIN_PLATE_PX = 90/** Nudge per arrow-key press, in unit coordinates. */const NUDGE_STEP = 0.01+const ARROW_STEPS: Record<string, [number, number]> = {+ ArrowLeft: [-1, 0],+ ArrowRight: [1, 0],+ ArrowUp: [0, -1],+ ArrowDown: [0, 1],+}+interface Props {faceplateId: stringlabel: string@@ -95,14 +102,43 @@ export function PortPositionEditor({}, [])/** A pointer cannot land a port on an exact fraction; the arrow keys can. */- const nudge = (port: Port, dx: number, dy: number) =>- onChange(- ports.map((p) =>- p.id === port.id- ? { ...p, ...clampPort({ x: p.x + dx * NUDGE_STEP, y: p.y + dy * NUDGE_STEP }) }- : p,+ const nudge = useCallback(+ (portId: string, dx: number, dy: number) =>+ onChange(+ ports.map((p) =>+ p.id === portId+ ? { ...p, ...clampPort({ x: p.x + dx * NUDGE_STEP, y: p.y + dy * NUDGE_STEP }) }+ : p,+ ),),- )+ [onChange, ports],+ )++ /**+ * Arrow keys move the selected port for as long as placement mode is on —+ * the handle does not have to hold focus.+ *+ * Selecting a port by clicking its row in the list, then reaching for the+ * arrows, was the obvious gesture and did nothing: focus was still in the+ * name field. Typing in a text field keeps its own arrow behaviour, so the+ * listener steps aside for one.+ */+ useEffect(() => {+ if (!interactive || !selectedPortId) return+ const onKeyDown = (e: KeyboardEvent) => {+ const step = ARROW_STEPS[e.key]+ if (!step) return+ const target = e.target as HTMLElement | null+ const tag = target?.tagName+ if (tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT' || target?.isContentEditable) {+ return+ }+ e.preventDefault()+ nudge(selectedPortId, step[0], step[1])+ }+ window.addEventListener('keydown', onKeyDown)+ return () => window.removeEventListener('keydown', onKeyDown)+ }, [interactive, selectedPortId, nudge])return (<div className="flex flex-col items-center gap-2">@@ -168,18 +204,6 @@ export function PortPositionEditor({onSelect(port.id)setDragging(port.id)}}- onKeyDown={(e) => {- const steps: Record<string, [number, number]> = {- ArrowLeft: [-1, 0],- ArrowRight: [1, 0],- ArrowUp: [0, -1],- ArrowDown: [0, 1],- }- const step = steps[e.key]- if (!step) return- e.preventDefault()- nudge(port, step[0], step[1])- }}><span className="pointer-events-none max-w-[22px] truncate text-white/80">{port.label}@@ -191,8 +215,8 @@ export function PortPositionEditor({{interactive && (<p className="text-[11px] text-muted-foreground">- Drag a port to place it. Arrow keys nudge the selected one, and ports snap to the row or- column of their neighbours.+ Drag a port to place it, or nudge the selected one with the arrow keys. Ports snap to the+ row or column of their neighbours.</p>)}</div>diff --git a/frontend/src/rack/components/RackDeviceModal.tsx b/frontend/src/rack/components/RackDeviceModal.tsxindex 111bc62..36e53f3 100644--- a/frontend/src/rack/components/RackDeviceModal.tsx+++ b/frontend/src/rack/components/RackDeviceModal.tsx@@ -388,6 +388,25 @@ function DeviceForm({ deviceId, onClose }: { deviceId: string | null; onClose: (</Field>)}+ {/* The plate leads: it is what the user is editing, and it is where+ ports are placed. Drawn at rack proportions — coordinates are unit+ fractions, so the blow-up never reaches the saved data. */}+ <div className="flex flex-col items-center gap-2 rounded border border-[#30363d] bg-[#0d1117] p-3">+ <PortPositionEditor+ faceplateId={faceplateId}+ label={label || getFaceplate(faceplateId).label}+ status={previewStatus}+ ports={ports}+ uHeight={uHeight}+ colSpan={colSpan}+ color={color}+ interactive={positioning && !isAccessory}+ selectedPortId={selectedPortId}+ onSelect={setSelectedPortId}+ onChange={setLocalPorts}+ />+ </div>+{/* Left column: what the device is and where it sits. Right column:its ports, which is the list that used to push everything elsebelow the fold. */}@@ -648,25 +667,6 @@ function DeviceForm({ deviceId, onClose }: { deviceId: string | null; onClose: (</div></div>- {/* The plate at rack proportions: the preview the thumbnail could- never be, and the surface ports are placed on. Coordinates are unit- fractions, so the blow-up never reaches the saved data. */}- <div className="flex flex-col items-center gap-2 rounded border border-[#30363d] bg-[#0d1117] p-3">- <PortPositionEditor- faceplateId={faceplateId}- label={label || getFaceplate(faceplateId).label}- status={previewStatus}- ports={ports}- uHeight={uHeight}- colSpan={colSpan}- color={color}- interactive={positioning && !isAccessory}- selectedPortId={selectedPortId}- onSelect={setSelectedPortId}- onChange={setLocalPorts}- />- </div>-<div className="mt-1 flex items-center gap-2">{isEdit && (<Button