Skip to content

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

fix(rack): let a patch panel port carry its front and rear cable

Pouzor79a99142a414
diff --git a/frontend/src/rack/README.md b/frontend/src/rack/README.md
index 7698344..43779eb 100644
--- a/frontend/src/rack/README.md
+++ b/frontend/src/rack/README.md
@@ -26,7 +26,7 @@ Create one from **New Canvas → Kind → Rack**.
| Port ownership | The **Device Inventory row owns the rack modelisation** — faceplate, U height, column span, colour override and the port list, positions included — not the mount. A device therefore wears the same front panel in every rack. Only placement (`rackId`, `uStart`, `colStart`), pinned status and label stay per canvas. Columns `device_inventory.rack_*`; `POST /racks/save` writes them through and `GET /racks` overlays them on load. Size is applied only where it still fits the mount's rack **and lands on no neighbour** — geometry is global, a rack is not, and an overlaid plate must obey the same placement rule as a dropped one. A mount can therefore hold less than its row does, so the write-through sends size back only when that save actually changed it; an unchanged size is an echo of the clamp and would shrink the device in every other rack. `POST /racks/save` also refuses two mounts that overlap in the same rack. Standalone has no inventory row to hang this on, so ports stay per mount there. Editable from both ends: a mount in `RackDeviceModal`, or the row itself in the Device Inventory detail modal (`DeviceFaceplateEditor`, shown only for a device already modelled), which writes the same columns through `PATCH /scan/pending/{id}`. |
| Port visibility | Per mount, `portVisibility`: `auto` (the default) leaves the faceplate in charge — patch-facing gear (switches, patch panels, `alwaysShowPorts`) shows its ports permanently, everything else reveals them on hover, on selection or in patch mode; `always` and `hover` override that plate rule either way. On top of the mode, a port a **drawn** cable ends on is always rendered, on both plates — hovering a switch lights the far sockets its runs land on, so a cable never ends on invisible metal. `cableVisibility.ts` owns that rule and `CableLayer` draws from the same answer. The mode is a canvas display choice: it lives on the rack mount, never on the inventory row. |
| 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. |
+| Cables | Port-to-port, a relation of their own — not React Flow edges. One cable per port, except a patch panel's, which takes `PATCH_PANEL_PORT_CABLES` (2): the panel is a pass-through, so the wall run lands on the rear of the port and the patch to the switch on its front, and with only a front view to draw both hang off the same port. A refused release toasts rather than dropping the run in silence. 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, `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__/RackFlowNode.test.tsx b/frontend/src/rack/__tests__/RackFlowNode.test.tsx
index d4a1ce2..1bfbd81 100644
--- a/frontend/src/rack/__tests__/RackFlowNode.test.tsx
+++ b/frontend/src/rack/__tests__/RackFlowNode.test.tsx
@@ -10,12 +10,15 @@ import { rackPalette } from '../rackTheme'
import { useThemeStore } from '@/stores/themeStore'
import { getFaceplate } from '../faceplates'
import type { NodeProps } from '@xyflow/react'
+import { toast } from 'sonner'
vi.mock('@xyflow/react', async () => {
const { mockReactFlow } = await import('@/test/mocks')
return mockReactFlow({ useReactFlow: () => ({ getZoom: () => 1 }) })
})
+vi.mock('sonner', async () => (await import('@/test/mocks')).mockSonner())
+
const store = () => useRackStore.getState()
beforeEach(() => {
@@ -139,6 +142,22 @@ describe('RackFlowNode patching', () => {
expect(created.to.portId).toBe(b.port.id)
})
+ it('says so when the release lands on a port that is already full', () => {
+ store().toggleCableMode()
+ renderRack()
+ const { b } = freePorts()
+ const taken = store().cables.find((c) => c.from.deviceId !== 'dev-patch')!
+ const device = store().devices.find((d) => d.id === taken.from.deviceId)!
+ const port = device.ports.find((p) => p.id === taken.from.portId)!
+ const before = store().cables.length
+
+ fireEvent.pointerDown(portGroup(b.device.label, b.port.label, b.port.type))
+ fireEvent.pointerUp(portGroup(device.label, port.label, port.type))
+
+ expect(store().cables).toHaveLength(before)
+ expect(toast.error).toHaveBeenCalledWith(expect.stringContaining('already patched'))
+ })
+
it('still patches with two separate clicks', () => {
store().toggleCableMode()
renderRack()
diff --git a/frontend/src/rack/__tests__/store.test.ts b/frontend/src/rack/__tests__/store.test.ts
index e15d269..deb05bd 100644
--- a/frontend/src/rack/__tests__/store.test.ts
+++ b/frontend/src/rack/__tests__/store.test.ts
@@ -620,6 +620,35 @@ describe('cables', () => {
expect(store().addCable(existing.from, b)).toBeNull()
})
+ // A patch panel port is a pass-through: the wall run lands on its rear and
+ // the patch to the switch on its front. Refusing the second one made the
+ // panel decorative (issue #482).
+ it('takes a second cable on a patch panel port', () => {
+ const patched = store().cables.find((c) => c.from.deviceId === 'dev-patch')!
+ const { b } = freePorts()
+ const id = store().addCable(patched.from, b)
+ expect(id).not.toBeNull()
+ expect(store().cables.filter((c) => c.from.portId === patched.from.portId)).toHaveLength(2)
+ })
+
+ it('refuses a third cable on a patch panel port', () => {
+ const patched = store().cables.find((c) => c.from.deviceId === 'dev-patch')!
+ const { b } = freePorts()
+ expect(store().addCable(patched.from, b)).not.toBeNull()
+ const spare = store()
+ .devices.find((d) => d.id === 'dev-pve2')!
+ .ports[0]
+ expect(
+ store().addCable(patched.from, { deviceId: 'dev-pve2', portId: spare.id }),
+ ).toBeNull()
+ })
+
+ it('keeps the one-cable limit on gear that is not a patch panel', () => {
+ const existing = store().cables.find((c) => c.from.deviceId === 'dev-sw24')!
+ const { b } = freePorts()
+ expect(store().addCable(existing.from, b)).toBeNull()
+ })
+
it('refuses a port patched to itself', () => {
const { a } = freePorts()
expect(store().addCable(a, a)).toBeNull()
@@ -653,6 +682,23 @@ describe('cables', () => {
expect(store().cableDrag).toBeNull()
})
+ // The refusal used to be silent — the run simply vanished on release.
+ it('reports what a release did, so a refused patch can be explained', () => {
+ const { a, b } = freePorts()
+ store().startCableDrag(a.deviceId, a.portId)
+ expect(store().endCableDrag(b)).toBe('patched')
+
+ const full = store().cables.find((c) => c.from.deviceId === 'dev-sw24')!
+ const spare = store().devices.find((d) => d.id === 'dev-pve2')!.ports[0]
+ store().startCableDrag(full.from.deviceId, full.from.portId)
+ expect(store().endCableDrag({ deviceId: 'dev-pve2', portId: spare.id })).toBe('refused')
+
+ store().startCableDrag(a.deviceId, a.portId)
+ store().moveCableDrag({ x: 10, y: 10 })
+ expect(store().endCableDrag(null)).toBe('none')
+ expect(store().endCableDrag(b)).toBe('none')
+ })
+
it('drops the draft when a drag is released on nothing', () => {
const { a } = freePorts()
const before = store().cables.length
diff --git a/frontend/src/rack/components/RackFlowNode.tsx b/frontend/src/rack/components/RackFlowNode.tsx
index 0c77a48..d33b7a5 100644
--- a/frontend/src/rack/components/RackFlowNode.tsx
+++ b/frontend/src/rack/components/RackFlowNode.tsx
@@ -1,6 +1,7 @@
/** One rack = one React Flow node. The U grid inside is rendered by hand. */
import { useCallback, useState } from 'react'
import { useReactFlow, type NodeProps } from '@xyflow/react'
+import { toast } from 'sonner'
import { patchedPortIds } from '../cableVisibility'
import { getFaceplate } from '../faceplates'
import {
@@ -145,10 +146,13 @@ export function RackFlowNode({ id }: NodeProps) {
const height = rackHeight(rack)
const gutter = rack.style.showNumbers ? NUMBER_GUTTER_PX : 0
const col = columnWidth(rack)
+ // The socket takes the colour of the first cable on it: a patch panel port
+ // carries two, and letting the later one win made the LED flip colour when an
+ // unrelated patch was added at the other end of the panel.
const patchedPorts = new Map<string, string>()
for (const cable of cables) {
- patchedPorts.set(cable.from.portId, cable.color)
- patchedPorts.set(cable.to.portId, cable.color)
+ if (!patchedPorts.has(cable.from.portId)) patchedPorts.set(cable.from.portId, cable.color)
+ if (!patchedPorts.has(cable.to.portId)) patchedPorts.set(cable.to.portId, cable.color)
}
const cablesOn = cableVisibility === 'always' || cableMode
// Ports of non-patch gear stay hidden until the plate is focused, or until
@@ -316,7 +320,11 @@ export function RackFlowNode({ id }: NodeProps) {
patchedPorts={patchedPorts}
draftPortId={isDraftDevice ? cableDraft.portId : null}
onPortPointerDown={(portId) => startCableDrag(device.id, portId)}
- onPortPointerUp={(portId) => endCableDrag({ deviceId: device.id, portId })}
+ onPortPointerUp={(portId) => {
+ if (endCableDrag({ deviceId: device.id, portId }) === 'refused') {
+ toast.error('That port is already patched — unplug a cable first')
+ }
+ }}
/>
</div>
)
diff --git a/frontend/src/rack/rackDefaults.ts b/frontend/src/rack/rackDefaults.ts
index 642eb83..db8c088 100644
--- a/frontend/src/rack/rackDefaults.ts
+++ b/frontend/src/rack/rackDefaults.ts
@@ -17,6 +17,15 @@ export const DEFAULT_RACK_STYLE: RackStyle = {
export const MIN_RACK_U = 1
export const MAX_RACK_U = 48
+/**
+ * Cables a single patch panel port accepts, front and rear.
+ *
+ * Every other port takes one: a switch jack really is one cable. A panel port
+ * is a pass-through, and with only a front view to draw on both ends of it hang
+ * off the same port.
+ */
+export const PATCH_PANEL_PORT_CABLES = 2
+
export const CABLE_COLORS: Record<CableType, string> = {
ethernet: '#39d353',
fiber: '#f0a500',
diff --git a/frontend/src/rack/store.ts b/frontend/src/rack/store.ts
index 16f3052..e3617b7 100644
--- a/frontend/src/rack/store.ts
+++ b/frontend/src/rack/store.ts
@@ -20,7 +20,7 @@ import {
toRack,
toRackDevice,
} from '@/utils/rackSerializer'
-import { getFaceplate, suggestFaceplate } from './faceplates'
+import { deviceTypeForFaceplate, getFaceplate, suggestFaceplate } from './faceplates'
import { canPlace, clamp, findSlot, type Placement } from './layout'
import {
RACK_COLUMNS,
@@ -39,6 +39,7 @@ import {
DEFAULT_RACK_STYLE,
MAX_RACK_U,
MIN_RACK_U,
+ PATCH_PANEL_PORT_CABLES,
PORT_CABLE_TYPE,
} from './rackDefaults'
@@ -54,6 +55,13 @@ interface CableDraft {
portId: string
}
+/**
+ * Outcome of releasing a patch: it landed, it was refused (the port is full),
+ * or the release was not a patch at all — a cancel, or the first click of a
+ * click-then-click.
+ */
+export type PatchResult = 'patched' | 'refused' | 'none'
+
export interface Viewport {
x: number
y: number
@@ -238,8 +246,13 @@ interface RackState {
*/
startCableDrag: (deviceId: string, portId: string) => void
moveCableDrag: (pointer: { x: number; y: number }) => void
- /** Release: patch onto `target`, or drop the draft if the pointer travelled. */
- endCableDrag: (target: CableDraft | null) => void
+ /**
+ * Release: patch onto `target`, or drop the draft if the pointer travelled.
+ *
+ * Reports what the release did so the UI can say why nothing appeared — a
+ * refused patch used to vanish without a word.
+ */
+ endCableDrag: (target: CableDraft | null) => PatchResult
cancelCableDraft: () => void
openDeviceEditor: (deviceId?: string | null) => void
closeDeviceEditor: () => void
@@ -819,14 +832,23 @@ export const useRackStore = create<RackState>((set, get) => {
devices.some((d) => d.id === ref.deviceId && d.ports.some((p) => p.id === ref.portId))
if (!hasPort(from) || !hasPort(to)) return null
- // A physical port takes one cable.
- const taken = (ref: CableDraft) =>
- cables.some(
+ // A physical port takes one cable — except a patch panel's, which is a
+ // pass-through: the run from the wall lands on the rear of the port and
+ // the patch to the switch on its front. The canvas draws one face, so
+ // both sides hang off the same port and the limit is two.
+ const cablesOn = (ref: CableDraft) =>
+ cables.filter(
(c) =>
(c.from.deviceId === ref.deviceId && c.from.portId === ref.portId) ||
(c.to.deviceId === ref.deviceId && c.to.portId === ref.portId),
- )
- if (taken(from) || taken(to)) return null
+ ).length
+ const portLimit = (ref: CableDraft) => {
+ const device = devices.find((d) => d.id === ref.deviceId)!
+ return deviceTypeForFaceplate(device.faceplateId) === 'patch_panel'
+ ? PATCH_PANEL_PORT_CABLES
+ : 1
+ }
+ if (cablesOn(from) >= portLimit(from) || cablesOn(to) >= portLimit(to)) return null
// Fibre vs copper follows the port the patch starts from.
const fromPort = devices
@@ -964,19 +986,20 @@ export const useRackStore = create<RackState>((set, get) => {
endCableDrag: (target) => {
const { cableDrag, cableDraft } = get()
- if (!cableDrag) return
+ if (!cableDrag) return 'none'
const onAnotherPort =
target &&
cableDraft &&
(target.deviceId !== cableDraft.deviceId || target.portId !== cableDraft.portId)
if (onAnotherPort) {
- get().addCable(cableDraft, target)
+ const id = get().addCable(cableDraft, target)
set({ cableDraft: null, cableDrag: null })
- return
+ return id ? 'patched' : 'refused'
}
// A drag released on nothing is a cancel; a press that never moved is the
// first half of a click-then-click patch, so the source stays armed.
set(cableDrag.moved ? { cableDraft: null, cableDrag: null } : { cableDrag: null })
+ return 'none'
},
cancelCableDraft: () => set({ cableDraft: null, cableDrag: null }),