From 03f32e606bba372008def99e9f6adeec6f4db266 Mon Sep 17 00:00:00 2001 From: Jannie Torrico Date: Fri, 4 Dec 2026 19:33:28 -0500 Subject: [PATCH] fix: handle input history storage quota errors --- .../flow_chat/store/inputHistoryStore.test.ts | 34 +++++++++++++++++++ .../src/flow_chat/store/inputHistoryStore.ts | 19 +++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/web-ui/src/flow_chat/store/inputHistoryStore.test.ts diff --git a/src/web-ui/src/flow_chat/store/inputHistoryStore.test.ts b/src/web-ui/src/flow_chat/store/inputHistoryStore.test.ts new file mode 100644 index 0000000000..78d489e911 --- /dev/null +++ b/src/web-ui/src/flow_chat/store/inputHistoryStore.test.ts @@ -0,0 +1,34 @@ +// @vitest-environment jsdom + +import { describe, it, expect, vi } from 'vitest'; +import { useInputHistoryStore } from './inputHistoryStore'; + +describe('inputHistoryStore', () => { + it('handles storage quota errors without throwing', () => { + const storageSpy = vi + .spyOn(Storage.prototype, 'setItem') + .mockImplementation(() => { + throw new DOMException( + 'Storage quota exceeded', + 'QuotaExceededError' + ); + }); + + try { + expect(() => { + useInputHistoryStore + .getState() + .addMessage('test-session', 'Hello'); + }).not.toThrow(); + + expect( + useInputHistoryStore + .getState() + .getSessionHistory('test-session') + ).toEqual(['Hello']); + } finally { + storageSpy.mockRestore(); + useInputHistoryStore.getState().clearHistory('test-session'); + } + }); +}); \ No newline at end of file diff --git a/src/web-ui/src/flow_chat/store/inputHistoryStore.ts b/src/web-ui/src/flow_chat/store/inputHistoryStore.ts index b03b07c99c..0982d90c39 100644 --- a/src/web-ui/src/flow_chat/store/inputHistoryStore.ts +++ b/src/web-ui/src/flow_chat/store/inputHistoryStore.ts @@ -5,7 +5,7 @@ */ import { create } from 'zustand'; -import { persist } from 'zustand/middleware'; +import { createJSONStorage, persist } from 'zustand/middleware'; export interface InputHistoryState { /** Map of sessionId to list of previously sent messages (most recent first) */ @@ -90,8 +90,21 @@ export const useInputHistoryStore = create()( }, }), { - name: 'openbitfun-input-history', - version: 2, // Bump version to migrate from old format + name: 'openbitfun-input-history', + storage: createJSONStorage(() => ({ + getItem: (name) => localStorage.getItem(name), + + setItem: (name, value) => { + try { + localStorage.setItem(name, value); + } catch { + console.warn('Failed to persist input history'); + } + }, + + removeItem: (name) => localStorage.removeItem(name), + })), + version: 2, // Bump version to migrate from old format migrate: (persistedState: any, version: number) => { if (version < 2) { // Migrate from old global format to new session-scoped format