Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring
TypeScript diagramhomelabhomelab-infrastructure-observabilitynetworkproxmox-veself-hostedzigbeezwavezwave-js
feat(rack): pick faceplates from a visual catalog
Pouzorc6c762408416
diff --git a/frontend/src/rack/README.md b/frontend/src/rack/README.mdindex e3d3200..86049b4 100644--- a/frontend/src/rack/README.md+++ b/frontend/src/rack/README.md@@ -17,7 +17,7 @@ Create one from **New Canvas → Kind → Rack**.| Inventory | A mount references a **Device Inventory** entry (`pending_devices`) via `deviceId`. Those rows survive approval *and* node deletion, so unracking never removes the device. Accessories (blank, shelf, cable manager) have `deviceId: null`. || Rack-created gear | A device created from this canvas is a normal inventory row with `discovery_source: "rack"`, so it inherits the inventory's search, filters, hide and delete. It shows under the **Rack devices** source filter, and both `POST /scan/pending/{id}/approve` and bulk-approve refuse it — a mount is not a host to document on a logical canvas. || Canvas node link | `nodeId` is a second, optional link to a logical-canvas node, resolved server-side by IEEE then IP. It supplies live status and lets the network-link import match endpoints. Never required. |-| Faceplates | Declarative templates in `faceplates.ts`, drawn as SVG in unit coordinates so a plate scales with U height and rack width. Applying a template seeds ports; the user edits them afterwards. `suggestFaceplate()` picks one from the device's discovery type. |+| Faceplates | Declarative templates in `faceplates.ts`, drawn as SVG in unit coordinates so a plate scales with U height and rack width. Applying a template seeds ports; the user edits them afterwards. `suggestFaceplate()` picks one from the device's discovery type. Chosen from `FaceplatePicker` — a visual catalog, not a `<select>`: every template is drawn with the canvas renderer at its real relative width and U height, grouped, searchable, and filterable by `kind` (accessories are offered only to accessories). || Ports | RJ45 and SFP/SFP+ only, drawn as real jack artwork at a fixed pixel size so plates of different U heights line up. Manual list per device. Power outlets are artwork, never a cable endpoint. || 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. |@@ -48,6 +48,7 @@ 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.- 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 previews the current plate; clicking it opens `FaceplatePicker`. Switching the source between device and accessory swaps the plate to a sane default of the other kind.- Double-click a plate → the same modal in edit mode: label, faceplate, U/height/column/width, status, colour, port list, 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__/FaceplatePicker.test.tsx b/frontend/src/rack/__tests__/FaceplatePicker.test.tsxnew file mode 100644index 0000000..c30a4e0--- /dev/null+++ b/frontend/src/rack/__tests__/FaceplatePicker.test.tsx@@ -0,0 +1,84 @@+/**+ * The picker is the only place the faceplate catalog is browsable, so it has to+ * show every template, filter honestly and report the pick.+ */+import { describe, it, expect, vi } from 'vitest'+import { render, screen, fireEvent, within } from '@testing-library/react'+import { FaceplatePicker } from '../components/FaceplatePicker'+import { FACEPLATES, faceplateGroups } from '../faceplates'++function setup(props: Partial<React.ComponentProps<typeof FaceplatePicker>> = {}) {+ const onPick = vi.fn()+ const onClose = vi.fn()+ render(+ <FaceplatePicker open value="server-1u" onPick={onPick} onClose={onClose} {...props} />,+ )+ return { onPick, onClose }+}++describe('FaceplatePicker', () => {+ it('renders a tile for every template, grouped', () => {+ setup()+ for (const plate of FACEPLATES) {+ expect(screen.getByRole('button', { name: plate.label })).toBeInTheDocument()+ }+ for (const { group } of faceplateGroups()) {+ expect(screen.getByText(group)).toBeInTheDocument()+ }+ })++ it('draws each plate rather than listing its name only', () => {+ setup()+ const tile = screen.getByRole('button', { name: 'Switch 24 ports + 2 SFP' })+ expect(tile.querySelector('svg')).toBeInTheDocument()+ // Height and width read from the template, so sizes compare across tiles.+ expect(within(tile).getByText(/^1U · Full width · 26 ports$/)).toBeInTheDocument()+ })++ it('marks the applied plate as selected', () => {+ setup({ value: 'nas-2u' })+ expect(screen.getByRole('button', { name: 'NAS 2U — 8 bays' })).toHaveAttribute(+ 'aria-pressed',+ 'true',+ )+ expect(screen.getByRole('button', { name: 'Server 1U' })).toHaveAttribute(+ 'aria-pressed',+ 'false',+ )+ })++ it('filters on name and on category', () => {+ setup()+ fireEvent.change(screen.getByLabelText('Search faceplates'), { target: { value: 'switch' } })+ expect(screen.getByRole('button', { name: 'Switch 8 ports' })).toBeInTheDocument()+ expect(screen.queryByRole('button', { name: 'NAS 2U — 8 bays' })).not.toBeInTheDocument()++ fireEvent.change(screen.getByLabelText('Search faceplates'), { target: { value: 'power' } })+ expect(screen.getByRole('button', { name: 'PDU 1U — 8 outlets' })).toBeInTheDocument()+ expect(screen.queryByRole('button', { name: 'Switch 8 ports' })).not.toBeInTheDocument()+ })++ it('says so when nothing matches', () => {+ setup()+ fireEvent.change(screen.getByLabelText('Search faceplates'), { target: { value: 'zzz' } })+ expect(screen.getByText(/No faceplate matches/)).toBeInTheDocument()+ })++ it('restricts the catalog to a kind', () => {+ setup({ kind: 'accessory' })+ expect(screen.getByRole('button', { name: 'Blank panel 1U' })).toBeInTheDocument()+ expect(screen.queryByRole('button', { name: 'Server 1U' })).not.toBeInTheDocument()+ })++ it('reports the pick and closes', () => {+ const { onPick, onClose } = setup()+ fireEvent.click(screen.getByRole('button', { name: 'Router / firewall 1U' }))+ expect(onPick).toHaveBeenCalledWith('router-1u')+ expect(onClose).toHaveBeenCalled()+ })++ it('renders nothing while closed', () => {+ setup({ open: false })+ expect(screen.queryByRole('button', { name: 'Server 1U' })).not.toBeInTheDocument()+ })+})diff --git a/frontend/src/rack/__tests__/RackDeviceModal.test.tsx b/frontend/src/rack/__tests__/RackDeviceModal.test.tsxindex 64647c7..d0f21eb 100644--- a/frontend/src/rack/__tests__/RackDeviceModal.test.tsx+++ b/frontend/src/rack/__tests__/RackDeviceModal.test.tsx@@ -26,6 +26,12 @@ function submit() {fireEvent.click(screen.getByRole('button', { name: /^(Add|Save)$/ }))}+/** The plate is picked from the visual catalog, not a native select. */+function pickFaceplate(label: string) {+ fireEvent.click(screen.getByLabelText('Faceplate'))+ fireEvent.click(screen.getByRole('button', { name: label }))+}+describe('RackDeviceModal — nothing to edit', () => {it('renders nothing while no editor is open', () => {const { container } = render(<RackDeviceModal />)@@ -78,7 +84,7 @@ describe('RackDeviceModal — adding', () => {render(<RackDeviceModal />)fireEvent.click(screen.getByRole('button', { name: 'Accessory' }))- fireEvent.change(screen.getByLabelText('Faceplate'), { target: { value: 'blank-1u' } })+ pickFaceplate('Blank panel 1U')fireEvent.change(screen.getByLabelText('Label'), { target: { value: 'filler' } })submit()@@ -87,6 +93,22 @@ describe('RackDeviceModal — adding', () => {expect(mounted.deviceId).toBeNull()expect(store().inventory).toHaveLength(before)})++ it('swaps to an accessory plate — and back — with the source', () => {+ store().openDeviceEditor()+ render(<RackDeviceModal />)++ fireEvent.click(screen.getByRole('button', { name: 'Accessory' }))+ expect(screen.getByLabelText('Faceplate')).toHaveAttribute('data-faceplate', 'blank-1u')+ // The picker then only offers rack furniture.+ fireEvent.click(screen.getByLabelText('Faceplate'))+ expect(screen.queryByRole('button', { name: 'Server 1U' })).not.toBeInTheDocument()+ fireEvent.click(screen.getByRole('button', { name: 'Shelf 1U' }))+ expect(screen.getByLabelText('Faceplate')).toHaveAttribute('data-faceplate', 'shelf-1u')++ fireEvent.click(screen.getByRole('button', { name: 'New device' }))+ expect(screen.getByLabelText('Faceplate')).toHaveAttribute('data-faceplate', 'server-1u')+ })})describe('RackDeviceModal — editing', () => {@@ -98,7 +120,10 @@ describe('RackDeviceModal — editing', () => {expect(screen.getByLabelText('Label')).toHaveValue(device.label)expect(screen.getByLabelText('U position')).toHaveValue(device.uStart)expect(screen.getByLabelText('Height (U)')).toHaveValue(device.uHeight)- expect(screen.getByLabelText('Faceplate')).toHaveValue(device.faceplateId)+ expect(screen.getByLabelText('Faceplate')).toHaveAttribute(+ 'data-faceplate',+ device.faceplateId,+ )// The old right-panel fields all moved here.expect(screen.getByLabelText('Status')).toBeInTheDocument()expect(screen.getByLabelText('Colour override')).toBeInTheDocument()@@ -127,7 +152,7 @@ describe('RackDeviceModal — editing', () => {// Regression: a 2U plate used to leave the device 1U, so 1U and 2U plates// rendered at the same height and the field looked locked.- fireEvent.change(screen.getByLabelText('Faceplate'), { target: { value: 'ups-2u' } })+ pickFaceplate('UPS 2U')expect(screen.getByLabelText('Height (U)')).toHaveValue(2)submit()diff --git a/frontend/src/rack/components/FaceplatePicker.tsx b/frontend/src/rack/components/FaceplatePicker.tsxnew file mode 100644index 0000000..a038f1a--- /dev/null+++ b/frontend/src/rack/components/FaceplatePicker.tsx@@ -0,0 +1,179 @@+/**+ * Visual faceplate catalog.+ *+ * A plate is a drawing, not a name: "Switch 24 ports + 2 SFP" tells you far less+ * than seeing the plate. The picker renders every template with the same+ * component the canvas uses, at its real relative width (a half-width plate+ * draws half as wide) and a height proportional to its U count, so sizes compare+ * honestly against each other.+ */+import { useMemo, useState } from 'react'+import { Search } from 'lucide-react'+import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'+import { Faceplate } from './Faceplate'+import { faceplateGroups } from '../faceplates'+import { useRackPalette } from '../rackTheme'+import { RACK_COLUMNS, type FaceplateKind, type FaceplateTemplate, type Port } from '@/types'++/** Pixel width of a full-width plate in the preview strip. */+const PREVIEW_WIDTH = 400+/** Pixel height of one U in the preview strip. */+const PREVIEW_U = 34+/**+ * The strip hugs the plate — a 1U plate in a 4U-tall box reads as a speck. Tall+ * plates simply make their tile taller, which is the honest cue anyway.+ */+const PREVIEW_MIN = 2 * PREVIEW_U++const WIDTH_LABEL: Record<number, string> = {+ [RACK_COLUMNS]: 'Full width',+ [RACK_COLUMNS / 2]: 'Half width',+ [RACK_COLUMNS / 3]: 'Third width',+ [RACK_COLUMNS / 4]: 'Quarter width',+}++/** Ports carry no id in a template — seed stable ones for the preview only. */+function previewPorts(plate: FaceplateTemplate): Port[] {+ return plate.ports.map((p, i) => ({ ...p, id: `${plate.id}-${i}` }))+}++function matches(plate: FaceplateTemplate, query: string): boolean {+ if (!query) return true+ const q = query.toLowerCase()+ return plate.label.toLowerCase().includes(q) || plate.group.toLowerCase().includes(q)+}++interface TileProps {+ plate: FaceplateTemplate+ selected: boolean+ onPick: (id: string) => void+}++function FaceplateTile({ plate, selected, onPick }: TileProps) {+ const palette = useRackPalette()+ const ports = useMemo(() => previewPorts(plate), [plate])++ return (+ <button+ type="button"+ aria-label={plate.label}+ aria-pressed={selected}+ onClick={() => onPick(plate.id)}+ className={`flex cursor-pointer flex-col gap-2 rounded border p-2 text-left transition-colors ${+ selected+ ? 'border-[#00d4ff] bg-[#00d4ff11]'+ : 'border-[#30363d] hover:border-[#00d4ff66] hover:bg-[#21262d]'+ }`}+ >+ <div+ className="flex items-center justify-center rounded p-2"+ style={{ background: palette.canvas, minHeight: PREVIEW_MIN }}+ >+ {/* The plate is drawn at a nominal size, then scaled to the tile by CSS:+ an SVG is a flex item and would otherwise be shrunk, never grown. A+ sub-width plate keeps its fraction of the strip, so widths compare. */}+ <div+ className="[&>svg]:h-auto [&>svg]:w-full"+ style={{ width: `${(100 * plate.colSpan) / RACK_COLUMNS}%` }}+ >+ <Faceplate+ faceplateId={plate.id}+ label={plate.label}+ status="unknown"+ ports={ports}+ width={(PREVIEW_WIDTH * plate.colSpan) / RACK_COLUMNS}+ height={plate.uHeight * PREVIEW_U}+ revealed+ />+ </div>+ </div>+ <div className="flex flex-col gap-0.5">+ <span className="text-xs text-foreground">{plate.label}</span>+ <span className="font-mono text-[10px] text-muted-foreground">+ {plate.uHeight}U · {WIDTH_LABEL[plate.colSpan] ?? `${plate.colSpan}/${RACK_COLUMNS}`} ·{' '}+ {plate.ports.length} port{plate.ports.length === 1 ? '' : 's'}+ </span>+ </div>+ </button>+ )+}++interface Props {+ open: boolean+ /** Currently applied plate, highlighted in the grid. */+ value: string+ /** Restrict the catalog — accessories are rack furniture, not devices. */+ kind?: FaceplateKind+ onPick: (id: string) => void+ onClose: () => void+}++export function FaceplatePicker({ open, value, kind, onPick, onClose }: Props) {+ const [query, setQuery] = useState('')++ const groups = useMemo(+ () =>+ faceplateGroups()+ .map(({ group, items }) => ({+ group,+ items: items.filter((p) => (!kind || p.kind === kind) && matches(p, query)),+ }))+ .filter((g) => g.items.length > 0),+ [kind, query],+ )++ return (+ <Dialog open={open} onOpenChange={(o) => !o && onClose()}>+ {/* `sm:max-w-sm` on the shared DialogContent is media-scoped, so it beats a+ plain `max-w-*` here — the override has to carry the same prefix. */}+ <DialogContent className="w-[96vw] max-w-[96vw] sm:max-w-[1440px] border-[#30363d] bg-[#161b22] text-foreground max-h-[90vh] overflow-y-auto">+ <DialogHeader>+ <DialogTitle className="text-sm font-semibold">Choose a faceplate</DialogTitle>+ </DialogHeader>++ <div className="relative">+ <Search+ size={13}+ className="pointer-events-none absolute left-2 top-1/2 -translate-y-1/2 text-muted-foreground"+ />+ <input+ autoFocus+ aria-label="Search faceplates"+ value={query}+ onChange={(e) => setQuery(e.target.value)}+ placeholder="Search by name or category…"+ className="w-full rounded border border-[#30363d] bg-[#21262d] py-1 pl-7 pr-2 text-sm text-foreground outline-none focus:border-[#00d4ff]"+ />+ </div>++ <div className="flex flex-col gap-4">+ {groups.map(({ group, items }) => (+ <section key={group} className="flex flex-col gap-2">+ <h3 className="text-[11px] font-semibold uppercase tracking-wide text-muted-foreground">+ {group}+ </h3>+ <div className="grid grid-cols-1 gap-2 sm:grid-cols-2 lg:grid-cols-3">+ {items.map((plate) => (+ <FaceplateTile+ key={plate.id}+ plate={plate}+ selected={plate.id === value}+ onPick={(id) => {+ onPick(id)+ onClose()+ }}+ />+ ))}+ </div>+ </section>+ ))}+ {groups.length === 0 && (+ <p className="py-6 text-center text-xs text-muted-foreground">+ No faceplate matches “{query}”.+ </p>+ )}+ </div>+ </DialogContent>+ </Dialog>+ )+}diff --git a/frontend/src/rack/components/RackDeviceModal.tsx b/frontend/src/rack/components/RackDeviceModal.tsxindex 2c5b0b7..72b8827 100644--- a/frontend/src/rack/components/RackDeviceModal.tsx+++ b/frontend/src/rack/components/RackDeviceModal.tsx@@ -17,7 +17,9 @@ import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/uimport { Button } from '@/components/ui/button'import { Label } from '@/components/ui/label'import { useRackStore } from '../store'-import { faceplateGroups, getFaceplate } from '../faceplates'+import { FaceplatePicker } from './FaceplatePicker'+import { Faceplate } from './Faceplate'+import { getFaceplate } from '../faceplates'import { generateUUID } from '@/utils/uuid'import { RACK_COLUMNS, type DeviceStatus, type Port, type PortType } from '@/types'@@ -60,7 +62,6 @@ function DeviceForm({ deviceId, onClose }: { deviceId: string | null; onClose: (const isEdit = !!deviceconst unracked = useMemo(() => inventory.filter((i) => !i.racked), [inventory])- const groups = useMemo(() => faceplateGroups(), [])const [source, setSource] = useState<Source>(unracked.length > 0 ? 'inventory' : 'new')const [inventoryId, setInventoryId] = useState(unracked[0]?.id ?? '')@@ -80,6 +81,7 @@ function DeviceForm({ deviceId, onClose }: { deviceId: string | null; onClose: (device?.ports ?? getFaceplate(faceplateId).ports.map((p) => ({ ...p, id: generateUUID() })),)const [plateChanged, setPlateChanged] = useState(false)+ const [pickerOpen, setPickerOpen] = useState(false)const [busy, setBusy] = useState(false)// The inventory lives in the Device Inventory, which the user can edit from@@ -100,6 +102,15 @@ function DeviceForm({ deviceId, onClose }: { deviceId: string | null; onClose: (setPlateChanged(true)}+ /** Accessories and devices draw from disjoint halves of the catalog. */+ function pickSource(next: Source) {+ setSource(next)+ const wantAccessory = next === 'accessory'+ if ((getFaceplate(faceplateId).kind === 'accessory') !== wantAccessory) {+ pickFaceplate(wantAccessory ? 'blank-1u' : 'server-1u')+ }+ }+function pickInventory(id: string) {setInventoryId(id)const item = inventory.find((i) => i.id === id)@@ -211,7 +222,7 @@ function DeviceForm({ deviceId, onClose }: { deviceId: string | null; onClose: (<buttonkey={value}type="button"- onClick={() => setSource(value)}+ onClick={() => pickSource(value)}className={`flex-1 cursor-pointer rounded border px-2 py-1 text-xs ${source === value? 'border-[#00d4ff] text-[#00d4ff]'@@ -289,22 +300,37 @@ function DeviceForm({ deviceId, onClose }: { deviceId: string | null; onClose: (</Field><Field label="Faceplate">- <select- className={inputClass}+ {/* The catalog is visual — the button previews the current plate and+ opens the picker rather than hiding drawings behind a name. */}+ <button+ type="button"aria-label="Faceplate"- value={faceplateId}- onChange={(e) => pickFaceplate(e.target.value)}+ data-faceplate={faceplateId}+ onClick={() => setPickerOpen(true)}+ className="flex cursor-pointer items-center gap-3 rounded border border-[#30363d] bg-[#21262d] p-2 text-left hover:border-[#00d4ff]">- {groups.map(({ group, items }) => (- <optgroup key={group} label={group}>- {items.map((plate) => (- <option key={plate.id} value={plate.id}>- {plate.label}- </option>- ))}- </optgroup>- ))}- </select>+ <Faceplate+ faceplateId={faceplateId}+ label={label || getFaceplate(faceplateId).label}+ status={status}+ ports={ports}+ width={(160 * colSpan) / RACK_COLUMNS}+ height={Math.min(uHeight, 4) * 18}+ colorOverride={color}+ revealed+ />+ <span className="flex flex-col">+ <span className="text-xs">{getFaceplate(faceplateId).label}</span>+ <span className="text-[11px] text-[#00d4ff]">Browse faceplates…</span>+ </span>+ </button>+ <FaceplatePicker+ open={pickerOpen}+ value={faceplateId}+ kind={!isEdit && source === 'accessory' ? 'accessory' : undefined}+ onPick={pickFaceplate}+ onClose={() => setPickerOpen(false)}+ />{plateChanged && isEdit && (<p className="text-[11px] text-[#e3b341]">Changing the plate replaces its ports and drops their patches.