Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring
TypeScript diagramhomelabhomelab-infrastructure-observabilitynetworkproxmox-veself-hostedzigbeezwavezwave-js
fix(unifi): stop invalid UniFi settings from half-saving the whole dialog
Pouzor2cfeb4d247ce
diff --git a/frontend/src/components/modals/SettingsModal.tsx b/frontend/src/components/modals/SettingsModal.tsxindex 55ce597..845c197 100644--- a/frontend/src/components/modals/SettingsModal.tsx+++ b/frontend/src/components/modals/SettingsModal.tsx@@ -117,6 +117,10 @@ function MeshAutoSync({}/** The controller's three inventories, described where the user ticks them. */+// Mirrors UnifiSyncConfig.sync_interval (ge=300, le=86400) on the backend.+const UNIFI_MIN_INTERVAL = 300+const UNIFI_MAX_INTERVAL = 86400+const UNIFI_SOURCES: { key: keyof UnifiImportModes; label: string; hint: string }[] = [{ key: 'infrastructure', label: 'Infrastructure', hint: 'stat/device — adopted APs, switches, gateways.' },{ key: 'known_clients', label: 'Known clients', hint: 'list/user — every client ever recorded. No IP, and long on a busy site.' },@@ -152,6 +156,9 @@ export function SettingsModal({ open, onClose }: SettingsModalProps) {const [alignment, setAlignment] = useState<AlignmentSettings>(readAlignmentSettings)const [autosave, setAutosave] = useState<AutosaveSettings>(readAutosaveSettings)const anyUnifiSource = unModes.infrastructure || unModes.known_clients || unModes.active_clients+ // Saving with every source unticked 422s on the backend, aborting the save+ // after the other configs already persisted — refuse it up front instead.+ const unifiBlocksSave = !STANDALONE && unConfig !== null && !anyUnifiSourceconst hideIp = useCanvasStore((s) => s.hideIp)const setHideIp = useCanvasStore((s) => s.setHideIp)@@ -297,9 +304,14 @@ export function SettingsModal({ open, onClose }: SettingsModalProps) {if (unConfig) {// Connection config is env-only; the activation and which of the// controller's three inventories to pull are what persist.+ // Clamp: the input's min/max never stopped a typed value, and a+ // sync_interval outside the range 422s — which would abort the save+ // after the configs above already persisted.+ const clamped = Math.min(UNIFI_MAX_INTERVAL, Math.max(UNIFI_MIN_INTERVAL, Math.round(unInterval)))+ if (clamped !== unInterval) setUnInterval(clamped)await unifiApi.saveConfig({sync_enabled: unSyncEnabled,- sync_interval: unInterval,+ sync_interval: clamped,modes: unModes,})}@@ -657,7 +669,8 @@ export function SettingsModal({ open, onClose }: SettingsModalProps) {<Button variant="ghost" onClick={onClose}>Cancel</Button><ButtononClick={handleSave}- disabled={saving}+ disabled={saving || unifiBlocksSave}+ title={unifiBlocksSave ? 'Select at least one UniFi source to import.' : undefined}style={{ background: '#00d4ff', color: '#0d1117' }}>{saving ? 'Saving…' : 'Save'}diff --git a/frontend/src/components/modals/__tests__/SettingsModal.test.tsx b/frontend/src/components/modals/__tests__/SettingsModal.test.tsxindex 8be378d..f5e2d20 100644--- a/frontend/src/components/modals/__tests__/SettingsModal.test.tsx+++ b/frontend/src/components/modals/__tests__/SettingsModal.test.tsx@@ -300,6 +300,55 @@ describe('SettingsModal — UniFi', () => {expect(screen.getByText(/select at least one source/i)).toBeInTheDocument()})+ it('blocks Save when no source is selected, so nothing partially persists', async () => {+ vi.mocked(unifiApi.getConfig).mockResolvedValue(config() as never)+ render(<SettingsModal open onClose={vi.fn()} />)+ await waitFor(() => expect(screen.getByText('UniFi auto-sync')).toBeInTheDocument())++ fireEvent.click(screen.getByLabelText('Import Infrastructure'))++ const save = screen.getByRole('button', { name: /save/i })+ expect(save).toBeDisabled()+ fireEvent.click(save)+ // Before the fix the status-check config saved, then UniFi 422'd and the+ // user got a cause-less "Failed to save settings".+ expect(settingsApi.save).not.toHaveBeenCalled()+ expect(unifiApi.saveConfig).not.toHaveBeenCalled()+ })++ it('leaves Save enabled when the UniFi panel is absent', async () => {+ vi.mocked(unifiApi.getConfig).mockRejectedValue(new Error('not configured'))+ render(<SettingsModal open onClose={vi.fn()} />)+ await waitFor(() => expect(unifiApi.getConfig).toHaveBeenCalled())+ expect(screen.getByRole('button', { name: /save/i })).not.toBeDisabled()+ })++ it('clamps a typed sync interval below the backend minimum', async () => {+ vi.mocked(unifiApi.getConfig).mockResolvedValue(config() as never)+ render(<SettingsModal open onClose={vi.fn()} />)+ await waitFor(() => expect(screen.getByText('UniFi auto-sync')).toBeInTheDocument())++ // min={300} on the input never stopped a typed value; the backend's+ // sync_interval ge=300 would 422 and abort the whole save.+ fireEvent.change(screen.getByLabelText('UniFi sync interval'), { target: { value: '100' } })+ fireEvent.click(screen.getByRole('button', { name: /save/i }))++ await waitFor(() => expect(unifiApi.saveConfig).toHaveBeenCalled())+ expect(vi.mocked(unifiApi.saveConfig).mock.calls[0][0].sync_interval).toBe(300)+ })++ it('clamps a typed sync interval above the backend maximum', async () => {+ vi.mocked(unifiApi.getConfig).mockResolvedValue(config() as never)+ render(<SettingsModal open onClose={vi.fn()} />)+ await waitFor(() => expect(screen.getByText('UniFi auto-sync')).toBeInTheDocument())++ fireEvent.change(screen.getByLabelText('UniFi sync interval'), { target: { value: '999999' } })+ fireEvent.click(screen.getByRole('button', { name: /save/i }))++ await waitFor(() => expect(unifiApi.saveConfig).toHaveBeenCalled())+ expect(vi.mocked(unifiApi.saveConfig).mock.calls[0][0].sync_interval).toBe(86400)+ })+it('reports what a manual re-sync imported', async () => {vi.mocked(unifiApi.getConfig).mockResolvedValue(config() as never)render(<SettingsModal open onClose={vi.fn()} />)