Skip to content

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

fix(rack): let the release reach a port a cable already ends on

Pouzor23400b084184
diff --git a/frontend/src/rack/__tests__/CableLayer.test.tsx b/frontend/src/rack/__tests__/CableLayer.test.tsx
index 87ebf44..32c7e51 100644
--- a/frontend/src/rack/__tests__/CableLayer.test.tsx
+++ b/frontend/src/rack/__tests__/CableLayer.test.tsx
@@ -3,7 +3,7 @@
* it outright, so a misclick costs nothing and Delete is the destructive step.
*/
import { describe, it, expect, beforeEach, vi } from 'vitest'
-import { render, fireEvent } from '@testing-library/react'
+import { render, fireEvent, act } from '@testing-library/react'
import { CableLayer } from '../components/CableLayer'
import { useRackStore } from '../store'
@@ -38,6 +38,26 @@ describe('CableLayer selection', () => {
expect(useRackStore.getState().selectedCableId).toBe(useRackStore.getState().cables[0].id)
})
+ // The overlay paints above the plates and a hit stroke is 14 wide around the
+ // run's own end point, so it covered the socket that run is plugged into. A
+ // second patch released on a patch panel port landed on the cable and the
+ // canvas read it as a drop on nothing.
+ it('stops taking the pointer while a patch is in flight', () => {
+ const store = useRackStore.getState()
+ store.setCableVisibility('always')
+ const { container, rerender } = render(<CableLayer />)
+ expect(hitPaths(container)[0].style.pointerEvents).toBe('stroke')
+
+ const { from } = useRackStore.getState().cables[0]
+ act(() => useRackStore.getState().startCableDrag(from.deviceId, from.portId))
+ rerender(<CableLayer />)
+ expect(hitPaths(container)[0].style.pointerEvents).toBe('none')
+
+ act(() => useRackStore.getState().cancelCableDraft())
+ rerender(<CableLayer />)
+ expect(hitPaths(container)[0].style.pointerEvents).toBe('stroke')
+ })
+
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.
diff --git a/frontend/src/rack/__tests__/RackFlowNode.test.tsx b/frontend/src/rack/__tests__/RackFlowNode.test.tsx
index 1bfbd81..5b03751 100644
--- a/frontend/src/rack/__tests__/RackFlowNode.test.tsx
+++ b/frontend/src/rack/__tests__/RackFlowNode.test.tsx
@@ -158,6 +158,24 @@ describe('RackFlowNode patching', () => {
expect(toast.error).toHaveBeenCalledWith(expect.stringContaining('already patched'))
})
+ it('says so when a click-then-click closes onto a full port', () => {
+ 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
+
+ const portB = portGroup(b.device.label, b.port.label, b.port.type)
+ fireEvent.pointerDown(portB)
+ fireEvent.pointerUp(portB)
+ fireEvent.pointerDown(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 deb05bd..a17c51e 100644
--- a/frontend/src/rack/__tests__/store.test.ts
+++ b/frontend/src/rack/__tests__/store.test.ts
@@ -682,6 +682,21 @@ describe('cables', () => {
expect(store().cableDrag).toBeNull()
})
+ // The second click is what makes the cable, so it is what has to report.
+ it('reports what a click-then-click press did', () => {
+ const { a, b } = freePorts()
+ expect(store().startCableDrag(a.deviceId, a.portId)).toBe('none')
+ expect(store().startCableDrag(a.deviceId, a.portId)).toBe('none')
+
+ store().startCableDrag(a.deviceId, a.portId)
+ expect(store().startCableDrag(b.deviceId, b.portId)).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().startCableDrag('dev-pve2', spare.id)).toBe('refused')
+ })
+
// 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()
diff --git a/frontend/src/rack/components/CableLayer.tsx b/frontend/src/rack/components/CableLayer.tsx
index a464863..7daee9e 100644
--- a/frontend/src/rack/components/CableLayer.tsx
+++ b/frontend/src/rack/components/CableLayer.tsx
@@ -101,6 +101,10 @@ export function CableLayer() {
visible.push({ cable, from, to })
}
+ // A port is armed: the next release belongs to the plates, not to the runs
+ // drawn over them.
+ const patching = cableDraft !== null
+
// Rubber band: the patch being dragged out of its source port.
const dragFrom = cableDraft && resolve(cableDraft.deviceId, cableDraft.portId)
const dragTo = cableDrag?.pointer ?? null
@@ -158,7 +162,14 @@ export function CableLayer() {
/>
{/* Fat invisible hit area — a 1.8px path is hard to hit at low
zoom. Clickable outside patch mode too: a cable is selected to
- read and edit it, not only to unplug it. */}
+ read and edit it, not only to unplug it.
+
+ It steps aside while a patch is in flight: the overlay paints
+ above the plates and this stroke is 14 wide around a run's own
+ end point, so it covered the very socket the cable is plugged
+ into. Releasing a second patch on a patch panel port landed on
+ the cable instead of the port, and the canvas read it as a
+ drop on nothing. */}
<path
data-testid={`cable-hit-${cable.id}`}
d={d}
@@ -166,7 +177,7 @@ export function CableLayer() {
stroke="transparent"
strokeWidth={14}
strokeLinecap="round"
- style={{ pointerEvents: 'stroke', cursor: 'pointer' }}
+ style={{ pointerEvents: patching ? 'none' : 'stroke', cursor: 'pointer' }}
onClick={(e) => {
// The pane click behind would clear the selection again.
e.stopPropagation()
diff --git a/frontend/src/rack/components/RackFlowNode.tsx b/frontend/src/rack/components/RackFlowNode.tsx
index d33b7a5..33a5ca8 100644
--- a/frontend/src/rack/components/RackFlowNode.tsx
+++ b/frontend/src/rack/components/RackFlowNode.tsx
@@ -21,7 +21,7 @@ import {
xToCol,
yToU,
} from '../layout'
-import { useRackStore } from '../store'
+import { useRackStore, type PatchResult } from '../store'
import { useRackPalette } from '../rackTheme'
import { resolveDeviceStatus } from '../deviceStatus'
import { RACK_COLUMNS } from '@/types'
@@ -36,6 +36,11 @@ interface DropPreview {
valid: boolean
}
+/** A patch the store turned down used to vanish without a word. */
+function reportPatch(result: PatchResult) {
+ if (result === 'refused') toast.error('That port is already patched — unplug a cable first')
+}
+
export function RackFlowNode({ id }: NodeProps) {
const palette = useRackPalette()
const rack = useRackStore((s) => s.racks.find((r) => r.id === id))
@@ -319,12 +324,12 @@ export function RackFlowNode({ id }: NodeProps) {
interactivePorts={cableMode}
patchedPorts={patchedPorts}
draftPortId={isDraftDevice ? cableDraft.portId : null}
- onPortPointerDown={(portId) => startCableDrag(device.id, portId)}
- onPortPointerUp={(portId) => {
- if (endCableDrag({ deviceId: device.id, portId }) === 'refused') {
- toast.error('That port is already patched — unplug a cable first')
- }
+ onPortPointerDown={(portId) => {
+ // A click-then-click patch is closed by the press, a dragged one
+ // by the release — both can be refused, so both report.
+ reportPatch(startCableDrag(device.id, portId))
}}
+ onPortPointerUp={(portId) => reportPatch(endCableDrag({ deviceId: device.id, portId }))}
/>
</div>
)
diff --git a/frontend/src/rack/store.ts b/frontend/src/rack/store.ts
index e3617b7..59d054f 100644
--- a/frontend/src/rack/store.ts
+++ b/frontend/src/rack/store.ts
@@ -244,7 +244,7 @@ interface RackState {
* Press on a port: arms it as the cable source, or — when another port is
* already armed — closes the patch, so click-then-click still works.
*/
- startCableDrag: (deviceId: string, portId: string) => void
+ startCableDrag: (deviceId: string, portId: string) => PatchResult
moveCableDrag: (pointer: { x: number; y: number }) => void
/**
* Release: patch onto `target`, or drop the draft if the pointer travelled.
@@ -972,11 +972,15 @@ export const useRackStore = create<RackState>((set, get) => {
const samePort = cableDraft.deviceId === deviceId && cableDraft.portId === portId
// Pressing the armed port again disarms it; pressing another closes the
// patch — that is the click-then-click flow, kept alongside dragging.
- if (!samePort) get().addCable(cableDraft, { deviceId, portId })
+ // The press is where that patch is made, so it is also where a refusal
+ // has to be reported; the release that follows has nothing left to do.
+ const id = samePort ? null : get().addCable(cableDraft, { deviceId, portId })
set({ cableDraft: null, cableDrag: null })
- return
+ if (samePort) return 'none'
+ return id ? 'patched' : 'refused'
}
set({ cableDraft: { deviceId, portId }, cableDrag: { pointer: null, moved: false } })
+ return 'none'
},
moveCableDrag: (pointer) => {