fix: add try/catch around metadata store localStorage persistence to handle QuotaExceededError
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.
This commit is contained in:
+16
-2
@@ -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 = <ValueType>() =>
|
||||
createJSONStorage<ValueType>(() => ({
|
||||
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 = <ValueType, FamilyKey>({
|
||||
key,
|
||||
defaultValue,
|
||||
@@ -36,7 +50,7 @@ export const createAtomFamilyState = <ValueType, FamilyKey>({
|
||||
? atomWithStorage<ValueType>(
|
||||
atomKey,
|
||||
defaultValue,
|
||||
undefined,
|
||||
createSafeLocalStorage<ValueType>(),
|
||||
localStorageOptions ?? undefined,
|
||||
)
|
||||
: atom(defaultValue);
|
||||
|
||||
+14
-2
@@ -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 = <ValueType>({
|
||||
{ getOnInit: true },
|
||||
) as StateAtom<ValueType>;
|
||||
} else if (useLocalStorage) {
|
||||
const safeStorage = createJSONStorage<ValueType>(() => ({
|
||||
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<ValueType>(
|
||||
key,
|
||||
defaultValue,
|
||||
undefined,
|
||||
safeStorage,
|
||||
localStorageOptions ?? undefined,
|
||||
) as StateAtom<ValueType>;
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user