Skip to content

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

fix(rack): keep the saved pan/zoom when switching back to a rack canvas

Pouzor37f893abe69b
diff --git a/frontend/src/rack/__tests__/RackCanvas.test.tsx b/frontend/src/rack/__tests__/RackCanvas.test.tsx
index 059e469..80ac129 100644
--- a/frontend/src/rack/__tests__/RackCanvas.test.tsx
+++ b/frontend/src/rack/__tests__/RackCanvas.test.tsx
@@ -5,28 +5,33 @@
* buttons look enabled but do nothing.
*/
import { describe, it, expect, beforeEach, vi } from 'vitest'
-import { render, screen, fireEvent } from '@testing-library/react'
+import { render, screen, fireEvent, act, waitFor } from '@testing-library/react'
import { RackCanvas } from '../components/RackCanvas'
import { useRackStore } from '../store'
+const applyViewport = vi.hoisted(() => vi.fn())
+
vi.mock('@xyflow/react', async () => {
const { mockReactFlow } = await import('@/test/mocks')
const React = await import('react')
return mockReactFlow({
ReactFlow: ({ children }: { children?: React.ReactNode }) =>
React.createElement('div', { 'data-testid': 'flow' }, children),
+ ReactFlowProvider: ({ children }: { children?: React.ReactNode }) =>
+ React.createElement('div', { 'data-testid': 'rack-flow-provider' }, children),
Background: () => null,
BackgroundVariant: { Dots: 'dots' },
Controls: () => null,
ViewportPortal: () => null,
useReactFlow: () => ({
- setViewport: vi.fn(),
+ setViewport: applyViewport,
screenToFlowPosition: (p: { x: number; y: number }) => p,
}),
})
})
beforeEach(() => {
+ applyViewport.mockClear()
useRackStore.getState().reset()
})
@@ -164,3 +169,72 @@ describe('RackCanvas cable dragging', () => {
expect(useRackStore.getState().cableDrag).toBeNull()
})
})
+
+/**
+ * Switching to another canvas and back remounts `RackCanvas` before `App` calls
+ * `loadDesign`, so the restore must not be spent on the pre-load state — that is
+ * issue #408, where the racks came back at the wrong pan/zoom until a hard
+ * reload.
+ */
+describe('RackCanvas viewport restore', () => {
+ const view = { x: -120, y: -340, zoom: 0.75 }
+
+ it('carries its own React Flow provider, so the logical canvas cannot leak its pan/zoom in', () => {
+ render(<RackCanvas />)
+ expect(screen.getByTestId('rack-flow-provider')).toContainElement(screen.getByTestId('flow'))
+ })
+
+ it('applies the stored viewport once a design is loaded', async () => {
+ useRackStore.setState({ designId: 'd1', loading: false, viewport: view })
+ render(<RackCanvas />)
+ await waitFor(() => expect(applyViewport).toHaveBeenCalledWith(view))
+ })
+
+ it('waits for the load to finish rather than restoring the previous state', async () => {
+ useRackStore.setState({ designId: 'd1', loading: true, viewport: view })
+ render(<RackCanvas />)
+ // A frame passes with the fetch still in flight.
+ await act(async () => {
+ await new Promise((r) => requestAnimationFrame(r))
+ })
+ expect(applyViewport).not.toHaveBeenCalled()
+ })
+
+ it('restores the viewport the load brings back, not the one left over from the last mount', async () => {
+ // Mount as a switch-back does: same design id, stale viewport, no load yet.
+ useRackStore.setState({ designId: 'd1', loading: false, viewport: view })
+ render(<RackCanvas />)
+ await waitFor(() => expect(applyViewport).toHaveBeenCalledWith(view))
+
+ const loaded = { x: 40, y: 80, zoom: 1.4 }
+ act(() => {
+ useRackStore.setState({ loading: true })
+ })
+ act(() => {
+ useRackStore.setState({ loading: false, viewport: loaded })
+ })
+ await waitFor(() => expect(applyViewport).toHaveBeenLastCalledWith(loaded))
+ })
+
+ it('ignores a viewport with no zoom', async () => {
+ useRackStore.setState({ designId: 'd1', loading: false, viewport: { x: 0, y: 0, zoom: 0 } })
+ render(<RackCanvas />)
+ await act(async () => {
+ await new Promise((r) => requestAnimationFrame(r))
+ })
+ expect(applyViewport).not.toHaveBeenCalled()
+ })
+
+ it('does not re-apply the viewport when the user pans', async () => {
+ useRackStore.setState({ designId: 'd1', loading: false, viewport: view })
+ render(<RackCanvas />)
+ await waitFor(() => expect(applyViewport).toHaveBeenCalledTimes(1))
+ act(() => {
+ useRackStore.getState().setViewport({ x: 5, y: 5, zoom: 1 })
+ })
+ await act(async () => {
+ await new Promise((r) => requestAnimationFrame(r))
+ })
+ expect(applyViewport).toHaveBeenCalledTimes(1)
+ })
+})
diff --git a/frontend/src/rack/components/RackCanvas.tsx b/frontend/src/rack/components/RackCanvas.tsx
index 556cf8a..3c6cca5 100644
--- a/frontend/src/rack/components/RackCanvas.tsx
+++ b/frontend/src/rack/components/RackCanvas.tsx
@@ -2,7 +2,9 @@
* Rack canvas: one React Flow node per rack, plus the cabling overlay.
*
* Rendered by `App` in place of `CanvasContainer` when the active design is of
- * type `rack`. React Flow context comes from the App-level provider.
+ * type `rack`. It carries its OWN `ReactFlowProvider`: sharing the App-level one
+ * with the logical canvas leaks that canvas' pan/zoom and pane size into the
+ * rack flow when the user switches back and forth.
*/
import { useCallback, useEffect, useMemo, useRef } from 'react'
import {
@@ -10,6 +12,7 @@ import {
BackgroundVariant,
Controls,
ReactFlow,
+ ReactFlowProvider,
useReactFlow,
type Node,
type NodeChange,
@@ -27,7 +30,7 @@ import { RackSettingsModal } from './RackSettingsModal'
const nodeTypes = { rack: RackFlowNode }
-export function RackCanvas() {
+function RackCanvasInner() {
const racks = useRackStore((s) => s.racks)
const moveRack = useRackStore((s) => s.moveRack)
const selectDevice = useRackStore((s) => s.selectDevice)
@@ -55,12 +58,24 @@ export function RackCanvas() {
// `node_status`, which only a refetch moves.
useAutoStatusRefresh()
- // Restore the saved pan/zoom once per design, not on every viewport nudge.
+ // Restore the saved pan/zoom once per load, not on every viewport nudge.
+ //
+ // Coming back from another canvas remounts this component BEFORE `App`'s
+ // design effect calls `loadDesign`, so the first pass here sees the previous
+ // state. Clearing the marker whenever a load starts means the viewport the
+ // fetch brings back is the one that gets applied, not the pre-load leftover.
const restoredFor = useRef<string | null>(null)
useEffect(() => {
- if (!designId || loading || restoredFor.current === designId) return
+ if (loading) {
+ restoredFor.current = null
+ return
+ }
+ if (!designId || restoredFor.current === designId || storedViewport.zoom <= 0) return
restoredFor.current = designId
- if (storedViewport.zoom > 0) void applyViewport(storedViewport)
+ // A frame later: on a fresh mount React Flow has not measured its pane yet,
+ // and a viewport set against a stale size lands in the wrong place.
+ const frame = requestAnimationFrame(() => void applyViewport(storedViewport))
+ return () => cancelAnimationFrame(frame)
}, [designId, loading, storedViewport, applyViewport])
// A patch dragged out of a port follows the pointer until it is released.
@@ -185,3 +200,11 @@ export function RackCanvas() {
</>
)
}
+
+export function RackCanvas() {
+ return (
+ <ReactFlowProvider>
+ <RackCanvasInner />
+ </ReactFlowProvider>
+ )
+}