Skip to content

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

feat(ui): spin the Scan History icon while a scan runs

Pouzor32f130ca7fe1
diff --git a/frontend/src/components/panels/Sidebar.tsx b/frontend/src/components/panels/Sidebar.tsx
index 712ef9a..c4e5983 100644
--- a/frontend/src/components/panels/Sidebar.tsx
+++ b/frontend/src/components/panels/Sidebar.tsx
@@ -1,5 +1,5 @@
import { useState, useCallback, useEffect } from 'react'
-import { Plus, Save, ScanLine, ChevronLeft, ChevronRight, LayoutDashboard, Clock, Square, Settings, LogOut, Network, RadioTower, Server, Type, PlusCircle, Pencil, Trash2, Rows3, BookOpen } from 'lucide-react'
+import { Plus, Save, ScanLine, ChevronLeft, ChevronRight, LayoutDashboard, Clock, Square, Settings, LogOut, Network, RadioTower, Server, Type, PlusCircle, Pencil, Trash2, Rows3, BookOpen, Loader2 } from 'lucide-react'
import { Logo } from '@/components/ui/Logo'
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
import { useCanvasStore } from '@/stores/canvasStore'
@@ -16,6 +16,7 @@ import { freeUnits } from '@/rack/layout'
import type { Design } from '@/types'
import { toast } from 'sonner'
import { useLatestRelease } from '@/hooks/useLatestRelease'
+import { useScanRunning } from '@/hooks/useScanRunning'
const STANDALONE = import.meta.env.VITE_STANDALONE === 'true'
@@ -49,6 +50,8 @@ export function Sidebar({ onAddNode, onAddGroupRect, onAddText, onScan, onZigbee
const { nodes, hasUnsavedChanges: canvasDirty, floorMap, setFloorMap } = useCanvasStore()
const floorMapEditNonce = useCanvasStore((s) => s.floorMapEditNonce)
const rackDirty = useRackStore((s) => s.hasUnsavedChanges)
+ // Drives the Scan History spinner: a run is visible without opening the modal.
+ const scanRunning = useScanRunning()
const openDeviceEditor = useRackStore((s) => s.openDeviceEditor)
const hasUnsavedChanges = isRack ? rackDirty : canvasDirty
@@ -266,7 +269,8 @@ export function Sidebar({ onAddNode, onAddGroupRect, onAddText, onScan, onZigbee
)}
{!STANDALONE && (
<SidebarItem
- icon={Clock}
+ icon={scanRunning ? Loader2 : Clock}
+ iconClassName={scanRunning ? 'animate-spin text-[#e3b341]' : undefined}
label="Scan History"
collapsed={collapsed}
onClick={onOpenHistory}
@@ -433,6 +437,8 @@ function VersionBadge() {
interface SidebarItemProps {
icon: React.ElementType
+ /** Extra classes on the icon only (e.g. a spin while a scan runs). */
+ iconClassName?: string
label: string
collapsed: boolean
active?: boolean
@@ -443,7 +449,7 @@ interface SidebarItemProps {
dataTour?: string
}
-function SidebarItem({ icon: Icon, label, collapsed, active, badge, accent, onClick, dataTour }: SidebarItemProps) {
+function SidebarItem({ icon: Icon, iconClassName, label, collapsed, active, badge, accent, onClick, dataTour }: SidebarItemProps) {
const btn = (
<button
onClick={onClick}
@@ -456,7 +462,7 @@ function SidebarItem({ icon: Icon, label, collapsed, active, badge, accent, onCl
: 'text-muted-foreground hover:text-foreground hover:bg-[#21262d]'
}`}
>
- <Icon size={16} className="shrink-0" />
+ <Icon size={16} className={`shrink-0${iconClassName ? ` ${iconClassName}` : ''}`} />
{!collapsed && <span className="truncate">{label}</span>}
{badge && (
<span className="absolute top-1 right-1 w-1.5 h-1.5 rounded-full bg-[#e3b341]" />
diff --git a/frontend/src/components/panels/__tests__/Sidebar.test.tsx b/frontend/src/components/panels/__tests__/Sidebar.test.tsx
index 1902e61..2fd96e5 100644
--- a/frontend/src/components/panels/__tests__/Sidebar.test.tsx
+++ b/frontend/src/components/panels/__tests__/Sidebar.test.tsx
@@ -3,6 +3,7 @@ import { render, screen, fireEvent, waitFor, cleanup } from '@testing-library/re
import { Sidebar } from '../Sidebar'
import { useCanvasStore } from '@/stores/canvasStore'
import { useAuthStore } from '@/stores/authStore'
+import { scanApi } from '@/api/client'
import type { Node } from '@xyflow/react'
import type { NodeData, Design } from '@/types'
import * as standaloneStorage from '@/utils/standaloneStorage'
@@ -107,6 +108,20 @@ describe('Sidebar', () => {
expect(screen.getByText('Scan History')).toBeInTheDocument()
})
+ it('spins the Scan History icon while a scan runs, and not otherwise', async () => {
+ const icon = () => screen.getByText('Scan History').closest('button')!.querySelector('svg')!
+
+ vi.mocked(scanApi.runs).mockResolvedValue({ data: [{ status: 'done' }] } as never)
+ render(<Sidebar {...defaultProps} />)
+ await waitFor(() => expect(scanApi.runs).toHaveBeenCalled())
+ expect(icon().getAttribute('class')).not.toContain('animate-spin')
+
+ cleanup()
+ vi.mocked(scanApi.runs).mockResolvedValue({ data: [{ status: 'running' }] } as never)
+ render(<Sidebar {...defaultProps} />)
+ await waitFor(() => expect(icon().getAttribute('class')).toContain('animate-spin'))
+ })
+
// ── Stats ──────────────────────────────────────────────────────────────────
it('displays total / online / offline counts from store', () => {
diff --git a/frontend/src/hooks/__tests__/useScanRunning.test.ts b/frontend/src/hooks/__tests__/useScanRunning.test.ts
new file mode 100644
index 0000000..19e8685
--- /dev/null
+++ b/frontend/src/hooks/__tests__/useScanRunning.test.ts
@@ -0,0 +1,73 @@
+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
+import { renderHook, waitFor, act } from '@testing-library/react'
+import { useScanRunning } from '../useScanRunning'
+import { scanApi } from '@/api/client'
+
+vi.mock('@/api/client', () => ({
+ scanApi: { runs: vi.fn() },
+}))
+
+const runs = vi.mocked(scanApi.runs)
+
+function run(status: string) {
+ return { id: status, status, kind: 'network', ranges: [], devices_found: 0, started_at: '', finished_at: null, error: null }
+}
+
+describe('useScanRunning', () => {
+ beforeEach(() => {
+ runs.mockReset()
+ })
+
+ afterEach(() => {
+ vi.useRealTimers()
+ })
+
+ it('is false until the first poll answers', () => {
+ runs.mockResolvedValue({ data: [] } as never)
+ const { result } = renderHook(() => useScanRunning())
+ expect(result.current).toBe(false)
+ })
+
+ it('reports a running scan', async () => {
+ runs.mockResolvedValue({ data: [run('done'), run('running')] } as never)
+ const { result } = renderHook(() => useScanRunning())
+ await waitFor(() => expect(result.current).toBe(true))
+ })
+
+ it('stays false when every run has finished', async () => {
+ runs.mockResolvedValue({ data: [run('done'), run('error'), run('cancelled')] } as never)
+ const { result } = renderHook(() => useScanRunning())
+ await waitFor(() => expect(runs).toHaveBeenCalled())
+ expect(result.current).toBe(false)
+ })
+
+ it('flips back to false once the run finishes', async () => {
+ vi.useFakeTimers()
+ runs.mockResolvedValue({ data: [run('running')] } as never)
+ const { result } = renderHook(() => useScanRunning())
+ await act(async () => { await Promise.resolve() })
+ expect(result.current).toBe(true)
+
+ runs.mockResolvedValue({ data: [run('done')] } as never)
+ await act(async () => { await vi.advanceTimersByTimeAsync(5000) })
+ expect(result.current).toBe(false)
+ })
+
+ it('treats a failed poll as "nothing running"', async () => {
+ runs.mockRejectedValue(new Error('offline'))
+ const { result } = renderHook(() => useScanRunning())
+ await waitFor(() => expect(runs).toHaveBeenCalled())
+ expect(result.current).toBe(false)
+ })
+
+ it('stops polling after unmount', async () => {
+ vi.useFakeTimers()
+ runs.mockResolvedValue({ data: [] } as never)
+ const { unmount } = renderHook(() => useScanRunning())
+ await act(async () => { await Promise.resolve() })
+ expect(runs).toHaveBeenCalledTimes(1)
+ unmount()
+ await act(async () => { await vi.advanceTimersByTimeAsync(15000) })
+ expect(runs).toHaveBeenCalledTimes(1)
+ })
+})
diff --git a/frontend/src/hooks/useScanRunning.ts b/frontend/src/hooks/useScanRunning.ts
new file mode 100644
index 0000000..8c7acf7
--- /dev/null
+++ b/frontend/src/hooks/useScanRunning.ts
@@ -0,0 +1,45 @@
+import { useEffect, useState } from 'react'
+import { scanApi, type ScanRunSummary } from '@/api/client'
+
+const STANDALONE = import.meta.env.VITE_STANDALONE === 'true'
+
+/** How often to ask. Slow on purpose: this only drives a spinning icon, the
+ * Scan History modal owns the live detail and polls every 3s while open. */
+const POLL_MS = 5000
+
+/**
+ * True while any scan / import run is in flight.
+ *
+ * Lets the sidebar show a scan is running without the user opening Scan
+ * History. A run can be started from anywhere (sidebar, import modals, the
+ * scheduler), so this polls rather than tracking what this tab kicked off.
+ * Standalone has no backend: always false, and never a request.
+ */
+export function useScanRunning(): boolean {
+ const [running, setRunning] = useState(false)
+
+ useEffect(() => {
+ if (STANDALONE) return
+ let cancelled = false
+
+ const check = async () => {
+ try {
+ const res = await scanApi.runs()
+ const runs = (res.data ?? []) as ScanRunSummary[]
+ if (!cancelled) setRunning(runs.some((r) => r.status === 'running'))
+ } catch {
+ // A failed poll is not evidence of a run; stay quiet, the modal reports.
+ if (!cancelled) setRunning(false)
+ }
+ }
+
+ check()
+ const id = setInterval(check, POLL_MS)
+ return () => {
+ cancelled = true
+ clearInterval(id)
+ }
+ }, [])
+
+ return running
+}