Skip to content

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

refactor(rack): rebuild the device modal around a headed strip

Pouzorb26e9d313715
diff --git a/frontend/src/rack/__tests__/PortPositionEditor.test.tsx b/frontend/src/rack/__tests__/PortPositionEditor.test.tsx
index 58866ae..942eb0f 100644
--- a/frontend/src/rack/__tests__/PortPositionEditor.test.tsx
+++ b/frontend/src/rack/__tests__/PortPositionEditor.test.tsx
@@ -139,3 +139,29 @@ describe('PortPositionEditor', () => {
expect(onChange).not.toHaveBeenCalled()
})
})
+
+describe('PortPositionEditor stage width', () => {
+ /** The stage is sized in inline styles, in editor pixels. */
+ const stageWidth = () =>
+ Number.parseFloat(screen.getByTestId('faceplate-stage').style.width)
+
+ it('draws a full-width plate at the width it was given', () => {
+ draw({ fullWidth: 900 })
+ expect(stageWidth()).toBeCloseTo(900, 0)
+ })
+
+ it('scales a narrower plate to its share of that width', () => {
+ // Half-width gear takes half the stage — the blow-up for short plates is
+ // capped at the width a full-width plate already gets.
+ draw({ fullWidth: 900, colSpan: 6 })
+ expect(stageWidth()).toBeGreaterThan(450)
+ expect(stageWidth()).toBeLessThanOrEqual(900)
+ })
+
+ it('falls back to its own default width when nothing measures', () => {
+ // jsdom reports a zero client width and has no ResizeObserver; the plate
+ // must keep a usable size rather than collapse to nothing.
+ draw()
+ expect(stageWidth()).toBeGreaterThan(0)
+ })
+})
diff --git a/frontend/src/rack/__tests__/RackDeviceModal.test.tsx b/frontend/src/rack/__tests__/RackDeviceModal.test.tsx
index 49be135..9684239 100644
--- a/frontend/src/rack/__tests__/RackDeviceModal.test.tsx
+++ b/frontend/src/rack/__tests__/RackDeviceModal.test.tsx
@@ -268,6 +268,34 @@ describe('RackDeviceModal — adding', () => {
expect(columns).toContainElement(screen.getByRole('button', { name: 'Add port' }))
})
+ /** The titles of the rules the form is split by, in document order. */
+ function sectionTitles() {
+ return screen
+ .getAllByTestId('section-header')
+ .map((el) => el.firstElementChild?.textContent)
+ }
+
+ it('heads every block of the form with a section rule', () => {
+ store().openDeviceEditor('dev-pve1')
+ render(<RackDeviceModal />)
+
+ expect(sectionTitles()).toEqual([
+ 'Linked device',
+ 'Faceplate',
+ 'Information',
+ 'Placement',
+ 'Ports',
+ ])
+ })
+
+ it('leaves the ports heading off an accessory, which has none', () => {
+ // No inventory row behind it either, so the strip is the plate alone.
+ store().openDeviceEditor('dev-shelf')
+ render(<RackDeviceModal />)
+
+ expect(sectionTitles()).toEqual(['Faceplate', 'Information', 'Placement'])
+ })
+
it('offers a Width option for every plate width in the catalog', async () => {
const { FACEPLATES } = await import('../faceplates')
store().openDeviceEditor()
diff --git a/frontend/src/rack/components/LinkedDevicePanel.tsx b/frontend/src/rack/components/LinkedDevicePanel.tsx
index 0ab37f6..26924eb 100644
--- a/frontend/src/rack/components/LinkedDevicePanel.tsx
+++ b/frontend/src/rack/components/LinkedDevicePanel.tsx
@@ -12,6 +12,7 @@
* prints everything discovery found; only the canvas-side rows go missing.
*/
import { useRackPalette } from '../rackTheme'
+import { SectionHeader } from './SectionHeader'
import type { DeviceStatus, InventoryDevice } from '@/types'
interface Row {
@@ -78,27 +79,30 @@ export function LinkedDevicePanel({
const services = entry.services ?? []
+ // The title sits on a section rule, level with the faceplate's own, and takes
+ // the status with it: the two halves of the strip must read as one row.
const header = (
- <header className="mb-2 flex items-center justify-between gap-2">
- <span className="text-xs font-medium">Linked device</span>
- <span className="flex items-center gap-1.5 text-[11px] text-muted-foreground">
- <span
- aria-hidden
- className="inline-block h-2 w-2 rounded-full"
- style={{ backgroundColor: palette.status[status] }}
- />
- {status}
- </span>
- </header>
+ <SectionHeader
+ aside={
+ <span className="flex items-center gap-1.5 text-[11px] text-muted-foreground">
+ <span
+ aria-hidden
+ className="inline-block h-2 w-2 rounded-full"
+ style={{ backgroundColor: palette.status[status] }}
+ />
+ {status}
+ </span>
+ }
+ >
+ Linked device
+ </SectionHeader>
)
return (
- <section
- aria-label="Linked device"
- className="rounded border border-[#30363d] bg-[#0d1117] p-2.5"
- >
+ <div aria-label="Linked device" className="flex flex-col gap-2">
{header}
+ <section className="flex flex-1 flex-col rounded border border-[#30363d] bg-[#0d1117] p-2.5">
{rows.length === 0 && (
<p className="text-[11px] text-muted-foreground">
Nothing known about this device yet.
@@ -151,6 +155,7 @@ export function LinkedDevicePanel({
Link to another device…
</button>
)}
- </section>
+ </section>
+ </div>
)
}
diff --git a/frontend/src/rack/components/PortListEditor.tsx b/frontend/src/rack/components/PortListEditor.tsx
index 5d46734..c06f3b7 100644
--- a/frontend/src/rack/components/PortListEditor.tsx
+++ b/frontend/src/rack/components/PortListEditor.tsx
@@ -84,7 +84,7 @@ export function PortListEditor({
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">
+ <ul className="grid max-h-[19rem] grid-cols-3 gap-1 overflow-y-auto pr-0.5">
{ports.map((port) => (
<li
key={port.id}
diff --git a/frontend/src/rack/components/PortPositionEditor.tsx b/frontend/src/rack/components/PortPositionEditor.tsx
index fa3564b..86569c4 100644
--- a/frontend/src/rack/components/PortPositionEditor.tsx
+++ b/frontend/src/rack/components/PortPositionEditor.tsx
@@ -14,14 +14,11 @@ import { clampPort, snapThreshold, snapToPeers } from '../portLayout'
import { useRackPalette } from '../rackTheme'
import { RACK_COLUMNS, type DeviceStatus, type Port } from '@/types'
-/** Width of a full-width (12-column) plate in the editor. */
+/** Default width of a full-width (12-column) plate in the editor. */
const EDITOR_FULL_WIDTH = 660
-/** Editor pixels per canvas pixel, before the small-plate blow-up below. */
-const SCALE = EDITOR_FULL_WIDTH / INNER_WIDTH_PX['19']
-
/**
- * A 1U plate is ~35px tall at `SCALE` — a thin strip to aim a port at. A narrow
+ * A 1U plate is ~35px tall at that scale — a thin strip to aim a port at. A narrow
* plate (a third-width mini node, the worst case for port placement) is scaled
* up further until it reaches this height, never past the width a full-width
* plate already gets.
@@ -46,6 +43,13 @@ interface Props {
uHeight: number
colSpan: number
color?: string
+ /**
+ * Width a full-width (12-column) plate is drawn at; a narrower plate takes
+ * its fraction of it. Left out, the editor measures the space it was given
+ * and fills it — a plate that renders at a third of the panel is one nobody
+ * can aim a port at.
+ */
+ fullWidth?: number
/**
* Drag mode. Off, the plate is a plain preview — no handles, no guides,
* nothing to grab.
@@ -65,6 +69,7 @@ export function PortPositionEditor({
uHeight,
colSpan,
color,
+ fullWidth,
interactive,
selectedPortId,
onSelect,
@@ -72,12 +77,19 @@ export function PortPositionEditor({
}: Props) {
const palette = useRackPalette()
const plateRef = useRef<HTMLDivElement>(null)
+ const stageRef = useRef<HTMLDivElement>(null)
+ const [measured, setMeasured] = useState<number | null>(null)
const [dragging, setDragging] = useState<string | null>(null)
const [guides, setGuides] = useState<{ x: number | null; y: number | null }>({ x: null, y: null })
- const width = (EDITOR_FULL_WIDTH * colSpan) / RACK_COLUMNS
- const height = uHeight * U_PX * SCALE
- const zoom = Math.min(EDITOR_FULL_WIDTH / width, Math.max(1, MIN_PLATE_PX / height))
+ // The width a full-width plate gets: whatever the caller pinned, else the
+ // room this editor was actually given, else the pre-measurement default.
+ const stageWidth = fullWidth ?? measured ?? EDITOR_FULL_WIDTH
+ // Editor pixels per canvas pixel, before the small-plate blow-up below.
+ const scale = stageWidth / INNER_WIDTH_PX['19']
+ const width = (stageWidth * colSpan) / RACK_COLUMNS
+ const height = uHeight * U_PX * scale
+ const zoom = Math.min(stageWidth / width, Math.max(1, MIN_PLATE_PX / height))
const plateW = width * zoom
const plateH = height * zoom
@@ -123,6 +135,27 @@ export function PortPositionEditor({
* name field. Typing in a text field keeps its own arrow behaviour, so the
* listener steps aside for one.
*/
+ /**
+ * Fill the space the parent gave us. jsdom reports 0 and has no
+ * `ResizeObserver`, so both paths fall back to the default width rather than
+ * collapsing the plate to nothing.
+ */
+ useEffect(() => {
+ if (fullWidth !== undefined) return
+ const measure = () => {
+ const w = stageRef.current?.clientWidth ?? 0
+ if (w > 0) setMeasured(w)
+ }
+ measure()
+ if (typeof ResizeObserver === 'undefined') {
+ window.addEventListener('resize', measure)
+ return () => window.removeEventListener('resize', measure)
+ }
+ const observer = new ResizeObserver(measure)
+ if (stageRef.current) observer.observe(stageRef.current)
+ return () => observer.disconnect()
+ }, [fullWidth])
+
useEffect(() => {
if (!interactive || !selectedPortId) return
const onKeyDown = (e: KeyboardEvent) => {
@@ -141,7 +174,7 @@ export function PortPositionEditor({
}, [interactive, selectedPortId, nudge])
return (
- <div className="flex flex-col items-center gap-2">
+ <div ref={stageRef} className="flex w-full flex-col items-center gap-2">
<div
ref={plateRef}
data-testid="faceplate-stage"
@@ -161,7 +194,7 @@ export function PortPositionEditor({
width={plateW}
height={plateH}
colorOverride={color}
- portScale={zoom * SCALE}
+ portScale={zoom * scale}
revealed
/>
diff --git a/frontend/src/rack/components/RackDeviceModal.tsx b/frontend/src/rack/components/RackDeviceModal.tsx
index c533da6..a12009a 100644
--- a/frontend/src/rack/components/RackDeviceModal.tsx
+++ b/frontend/src/rack/components/RackDeviceModal.tsx
@@ -22,6 +22,7 @@ import { FaceplatePicker } from './FaceplatePicker'
import { PortListEditor } from './PortListEditor'
import { PortPositionEditor } from './PortPositionEditor'
import { LinkedDevicePanel } from './LinkedDevicePanel'
+import { SectionHeader } from './SectionHeader'
import { DevicePickerModal } from './DevicePickerModal'
import { deviceTypeForFaceplate, getFaceplate } from '../faceplates'
import { findSlot } from '../layout'
@@ -392,23 +393,36 @@ 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}
+ {/* The strip leads with the two things the user checks first: what
+ this mount actually stands for, and the plate it wears. The plate
+ is drawn at rack proportions — coordinates are unit fractions, so
+ the blow-up never reaches the saved data. The two halves are
+ spelled out rather than `sm:grid-cols-2` so the strip stays
+ distinguishable from the form grid below it. */}
+ <div className="grid gap-3 sm:grid-cols-[1fr_1fr]">
+ <LinkedDevicePanel
+ entry={mountEntry}
status={previewStatus}
- ports={ports}
- uHeight={uHeight}
- colSpan={colSpan}
- color={color}
- interactive={positioning && !isAccessory}
- selectedPortId={selectedPortId}
- onSelect={setSelectedPortId}
- onChange={setLocalPorts}
+ onLink={canRelink ? () => setDevicePickerOpen(true) : undefined}
/>
+ <div className="flex flex-col gap-2">
+ <SectionHeader>Faceplate</SectionHeader>
+ <div className="flex flex-1 flex-col items-center justify-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>
</div>
{/* Left column: what the device is and where it sits. Right column:
@@ -416,6 +430,7 @@ function DeviceForm({ deviceId, onClose }: { deviceId: string | null; onClose: (
below the fold. */}
<div className="grid gap-x-4 gap-y-3 sm:grid-cols-2">
<div className="flex flex-col gap-3">
+ <SectionHeader>Information</SectionHeader>
{showInventoryPicker && STANDALONE && (
/* No backend, so no Device Inventory modal to open (it reads
`pending_devices` over REST) — standalone keeps the flat list. */
@@ -547,6 +562,8 @@ function DeviceForm({ deviceId, onClose }: { deviceId: string | null; onClose: (
)}
</Field>
+ <SectionHeader>Placement</SectionHeader>
+
<div className="grid grid-cols-2 gap-3">
<Field label="U position">
<input
@@ -638,8 +655,11 @@ function DeviceForm({ deviceId, onClose }: { deviceId: string | null; onClose: (
</div>
<div className="flex flex-col gap-3">
+ {/* An accessory has no ports, so it gets no heading either — a
+ rule over an empty column reads as a missing section. */}
{!isAccessory && (
<>
+ <SectionHeader>Ports</SectionHeader>
<PortListEditor
ports={ports}
onChange={setLocalPorts}
@@ -667,14 +687,6 @@ function DeviceForm({ deviceId, onClose }: { deviceId: string | null; onClose: (
</>
)}
- {/* The column below the port list used to be dead space. A mount
- that stands for an inventory entry fills it with what the
- logical canvas knows about the same box. */}
- <LinkedDevicePanel
- entry={mountEntry}
- status={previewStatus}
- onLink={canRelink ? () => setDevicePickerOpen(true) : undefined}
- />
{canRelink && devicePickerOpen && (
/* Inside this DialogContent for the same reason as the inventory
picker: an open Base UI dialog makes outside content inert.
diff --git a/frontend/src/rack/components/SectionHeader.tsx b/frontend/src/rack/components/SectionHeader.tsx
new file mode 100644
index 0000000..bfd07ce
--- /dev/null
+++ b/frontend/src/rack/components/SectionHeader.tsx
@@ -0,0 +1,26 @@
+/**
+ * The rule the logical canvas' `NodeModal` draws over each block of fields,
+ * reused by the rack modal so both editors read the same way.
+ *
+ * `aside` rides the right end of the rule — a status, a count, anything that
+ * belongs to the section rather than to one field inside it.
+ */
+export function SectionHeader({
+ children,
+ aside,
+}: {
+ children: React.ReactNode
+ aside?: React.ReactNode
+}) {
+ return (
+ <div
+ data-testid="section-header"
+ className="flex items-center justify-between gap-2 border-b border-[#30363d] pb-1"
+ >
+ <span className="text-[11px] font-semibold uppercase tracking-wider text-muted-foreground/70">
+ {children}
+ </span>
+ {aside}
+ </div>
+ )
+}