From 9a23aa0c2a49b38de5162b5666edc047f238992c Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Thu, 1 Oct 2026 18:25:25 -0400 Subject: [PATCH] fix(frontend): keep sidebar state usable when localStorage throws --- .../src/hooks/useSidebarCollapsed.test.tsx | 19 +++++++++++ frontend/src/hooks/useSidebarCollapsed.ts | 34 +++++++++++-------- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/frontend/src/hooks/useSidebarCollapsed.test.tsx b/frontend/src/hooks/useSidebarCollapsed.test.tsx index 5e1a2feb..08595cfb 100644 --- a/frontend/src/hooks/useSidebarCollapsed.test.tsx +++ b/frontend/src/hooks/useSidebarCollapsed.test.tsx @@ -157,6 +157,25 @@ describe('sidebar collapse hooks', () => { ) }) + it('defaults open and keeps toggling in memory when storage throws', () => { + localStorageMock.getItem.mockImplementationOnce(() => { + throw new Error('SecurityError') + }) + localStorageMock.setItem.mockImplementationOnce(() => { + throw new Error('QuotaExceededError') + }) + + const { result } = renderHook(() => useSidebarSections(sections)) + + expect(result.current.isSectionOpen('sessions')).toBe(true) + + act(() => { + result.current.toggleSection('sessions') + }) + + expect(result.current.isSectionOpen('sessions')).toBe(false) + }) + it('tracks each section independently', () => { localStorageMock.getItem.mockReturnValue(null) diff --git a/frontend/src/hooks/useSidebarCollapsed.ts b/frontend/src/hooks/useSidebarCollapsed.ts index 0ba3acf6..785259fc 100644 --- a/frontend/src/hooks/useSidebarCollapsed.ts +++ b/frontend/src/hooks/useSidebarCollapsed.ts @@ -3,11 +3,24 @@ import { useState, useCallback } from 'react' const STORAGE_KEY = 'oc:sidebar:collapsed' const CLOSED_SECTIONS_STORAGE_KEY = `${STORAGE_KEY}:closed-sections` -function readStoredBoolean(key: string, fallback: boolean): boolean { - if (typeof window === 'undefined') { - return fallback +function readStorage(key: string): string | null { + try { + return localStorage.getItem(key) + } catch { + return null } - const stored = localStorage.getItem(key) +} + +function writeStorage(key: string, value: unknown): void { + try { + localStorage.setItem(key, JSON.stringify(value)) + } catch { + return + } +} + +function readStoredBoolean(key: string, fallback: boolean): boolean { + const stored = readStorage(key) if (stored === null) { return fallback } @@ -25,9 +38,7 @@ function usePersistentBoolean(key: string, fallback: boolean): [boolean, () => v const toggle = useCallback(() => { setValue((prev: boolean) => { const newValue = !prev - if (typeof window !== 'undefined') { - localStorage.setItem(key, JSON.stringify(newValue)) - } + writeStorage(key, newValue) return newValue }) }, [key]) @@ -36,10 +47,7 @@ function usePersistentBoolean(key: string, fallback: boolean): [boolean, () => v } function readStoredClosedSections(sections: readonly T[]): T[] { - if (typeof window === 'undefined') { - return [] - } - const stored = localStorage.getItem(CLOSED_SECTIONS_STORAGE_KEY) + const stored = readStorage(CLOSED_SECTIONS_STORAGE_KEY) if (stored === null) { return [] } @@ -69,9 +77,7 @@ export function useSidebarSections( const next = prev.includes(section) ? prev.filter((item) => item !== section) : [...prev, section] - if (typeof window !== 'undefined') { - localStorage.setItem(CLOSED_SECTIONS_STORAGE_KEY, JSON.stringify(next)) - } + writeStorage(CLOSED_SECTIONS_STORAGE_KEY, next) return next }) }, [])