utopia-ui/lib/src/Utils/localStorage.ts
Anton Tranelis 5dad538577 fix: prettier formatting + add lint check hook
- Fix prettier formatting for localStorage utility
- Add pre-PR lint check script to prevent CI failures
- Create Claude Code hook configuration for automatic linting
2025-08-05 09:47:52 +02:00

36 lines
976 B
TypeScript

/* eslint-disable no-catch-all/no-catch-all */
/* eslint-disable no-console */
/**
* Safe localStorage utility that handles SecurityError gracefully
* when localStorage is not available (e.g., private browsing mode)
*/
export const safeLocalStorage = {
getItem: (key: string): string | null => {
try {
return localStorage.getItem(key)
} catch (error) {
console.warn(`localStorage.getItem failed for key "${key}":`, error)
return null
}
},
setItem: (key: string, value: string): void => {
try {
localStorage.setItem(key, value)
} catch (error) {
console.warn(`localStorage.setItem failed for key "${key}":`, error)
// Silently fail - functionality will work in memory-only mode
}
},
removeItem: (key: string): void => {
try {
localStorage.removeItem(key)
} catch (error) {
console.warn(`localStorage.removeItem failed for key "${key}":`, error)
// Silently fail
}
},
}