diff --git a/workbench-ui/src/design/components/SplitPane/SplitPane.tsx b/workbench-ui/src/design/components/SplitPane/SplitPane.tsx index e4ca6991..c93544d9 100644 --- a/workbench-ui/src/design/components/SplitPane/SplitPane.tsx +++ b/workbench-ui/src/design/components/SplitPane/SplitPane.tsx @@ -23,6 +23,11 @@ function clamp(value: number, min: number, max: number) { return Math.max(min, Math.min(max, value)); } +function getLocalStorage(): Storage | null { + if (typeof globalThis.localStorage === "undefined") return null; + return globalThis.localStorage; +} + export function SplitPane({ direction, defaultSplit = 50, @@ -32,7 +37,7 @@ export function SplitPane({ }: SplitPaneProps) { const [split, setSplit] = useState(() => { if (id) { - const stored = localStorage.getItem(storageKey(id)); + const stored = getLocalStorage()?.getItem(storageKey(id)); if (stored !== null) { const parsed = Number(stored); if (!Number.isNaN(parsed)) return parsed; @@ -44,7 +49,7 @@ export function SplitPane({ const dragging = useRef(false); useEffect(() => { - if (id) localStorage.setItem(storageKey(id), String(split)); + if (id) getLocalStorage()?.setItem(storageKey(id), String(split)); }, [split, id]); const onPointerDown = useCallback( diff --git a/workbench-ui/src/test/setup.ts b/workbench-ui/src/test/setup.ts index 08fce702..d483af4b 100644 --- a/workbench-ui/src/test/setup.ts +++ b/workbench-ui/src/test/setup.ts @@ -6,3 +6,25 @@ Object.defineProperty(navigator, "clipboard", { writeText: vi.fn().mockResolvedValue(undefined), }, }); + +if (typeof globalThis.localStorage === "undefined") { + const store = new Map(); + + Object.defineProperty(globalThis, "localStorage", { + configurable: true, + value: { + clear: vi.fn(() => store.clear()), + getItem: vi.fn((key: string) => store.get(key) ?? null), + key: vi.fn((index: number) => Array.from(store.keys())[index] ?? null), + removeItem: vi.fn((key: string) => { + store.delete(key); + }), + setItem: vi.fn((key: string, value: string) => { + store.set(key, String(value)); + }), + get length() { + return store.size; + }, + }, + }); +}