From f6eba12008906da328fb04bd7c1ad80e9a8b89f8 Mon Sep 17 00:00:00 2001 From: Sonarly Claude Code Date: Fri, 20 Mar 2026 04:26:51 +0000 Subject: [PATCH] fix: add try/catch around metadata store localStorage persistence to handle QuotaExceededError MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sonarly.com/issue/16799?type=bug The metadata store persists all 25 entity collections (including fieldMetadataItems with full field definitions for every object) to localStorage via Jotai's atomWithStorage, which has no error handling for quota limits. Users with many custom objects exceed the ~5MB localStorage limit. Fix: **Root cause fix**: The `createAtomFamilyState` and `createAtomState` utilities pass `undefined` as the storage parameter to Jotai's `atomWithStorage`, which uses the default `createJSONStorage(() => localStorage)` — a storage implementation that calls `localStorage.setItem` without any error handling. **Fix**: Both files now provide a custom storage via `createJSONStorage()` that wraps `localStorage.setItem` in a try/catch. When a `QuotaExceededError` (or any write error) occurs, the write is silently skipped. The in-memory Jotai atom state remains correct — localStorage persistence is treated as best-effort for faster initial loads, not as the source of truth. This matches the team's existing pattern of custom storage implementations (see `createJotaiCookieStorage.ts`) and uses `createJSONStorage` from `jotai/utils` which is the documented Jotai API for this purpose. The `getItem` and `removeItem` methods delegate directly to `localStorage` since they don't have quota concerns. --- .../state/jotai/utils/createAtomFamilyState.ts | 18 ++++++++++++++++-- .../state/jotai/utils/createAtomState.ts | 16 ++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/packages/twenty-front/src/modules/ui/utilities/state/jotai/utils/createAtomFamilyState.ts b/packages/twenty-front/src/modules/ui/utilities/state/jotai/utils/createAtomFamilyState.ts index cecd0c76b11..710c78bd39c 100644 --- a/packages/twenty-front/src/modules/ui/utilities/state/jotai/utils/createAtomFamilyState.ts +++ b/packages/twenty-front/src/modules/ui/utilities/state/jotai/utils/createAtomFamilyState.ts @@ -1,8 +1,22 @@ import { atom } from 'jotai'; -import { atomWithStorage } from 'jotai/utils'; +import { atomWithStorage, createJSONStorage } from 'jotai/utils'; import { type FamilyState } from '@/ui/utilities/state/jotai/types/FamilyState'; +const createSafeLocalStorage = () => + createJSONStorage(() => ({ + getItem: (key: string) => localStorage.getItem(key), + setItem: (key: string, value: string) => { + try { + localStorage.setItem(key, value); + } catch { + // Silently ignore QuotaExceededError — the in-memory Jotai state + // remains correct; persistence is best-effort. + } + }, + removeItem: (key: string) => localStorage.removeItem(key), + })); + export const createAtomFamilyState = ({ key, defaultValue, @@ -36,7 +50,7 @@ export const createAtomFamilyState = ({ ? atomWithStorage( atomKey, defaultValue, - undefined, + createSafeLocalStorage(), localStorageOptions ?? undefined, ) : atom(defaultValue); diff --git a/packages/twenty-front/src/modules/ui/utilities/state/jotai/utils/createAtomState.ts b/packages/twenty-front/src/modules/ui/utilities/state/jotai/utils/createAtomState.ts index c9cdac315c3..bf6e876b963 100644 --- a/packages/twenty-front/src/modules/ui/utilities/state/jotai/utils/createAtomState.ts +++ b/packages/twenty-front/src/modules/ui/utilities/state/jotai/utils/createAtomState.ts @@ -1,5 +1,5 @@ import { atom, type WritableAtom } from 'jotai'; -import { atomWithStorage } from 'jotai/utils'; +import { atomWithStorage, createJSONStorage } from 'jotai/utils'; import { isDefined } from 'twenty-shared/utils'; import { type State } from '@/ui/utilities/state/jotai/types/State'; @@ -52,10 +52,22 @@ export const createAtomState = ({ { getOnInit: true }, ) as StateAtom; } else if (useLocalStorage) { + const safeStorage = createJSONStorage(() => ({ + getItem: (k: string) => localStorage.getItem(k), + setItem: (k: string, v: string) => { + try { + localStorage.setItem(k, v); + } catch { + // Silently ignore QuotaExceededError — the in-memory Jotai state + // remains correct; persistence is best-effort. + } + }, + removeItem: (k: string) => localStorage.removeItem(k), + })); baseAtom = atomWithStorage( key, defaultValue, - undefined, + safeStorage, localStorageOptions ?? undefined, ) as StateAtom; } else {