mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 17:35:10 +03:00
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES ## What kind of change does this PR introduce? - Noticing our code we have many patterns of calling localstorage and handling those errors - We should add those in a single well tested file - Handle those errors in the singleton which makes it easier for us to debug customer issues. Logger is outputing local storage warnings for feature we expose - Side effect of this is random crashes on studio when local storage isn't available or handled correctly <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Improved browser storage handling across the app for more reliable persistence and graceful behavior in restricted or non-browser environments (settings, previews, charts, tabs, sign-in/session flows, integrations, and UI state). * **New Features** * Introduced a safe storage layer to standardize and harden local/session persistence. * **Tests** * Added comprehensive tests covering the new safe storage behavior. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
33 lines
1008 B
TypeScript
33 lines
1008 B
TypeScript
import { LOCAL_STORAGE_KEYS, safeLocalStorage, safeSessionStorage } from 'common'
|
|
|
|
type LocalStorageKey = (typeof LOCAL_STORAGE_KEYS)[keyof typeof LOCAL_STORAGE_KEYS]
|
|
type StorageType = 'local' | 'session'
|
|
|
|
function getStorage(storageType: StorageType) {
|
|
return storageType === 'local' ? safeLocalStorage : safeSessionStorage
|
|
}
|
|
|
|
export function store(storageType: StorageType, key: LocalStorageKey, value: string) {
|
|
getStorage(storageType).setItem(key as string, value)
|
|
}
|
|
|
|
export function retrieve(storageType: StorageType, key: LocalStorageKey): string | null {
|
|
return getStorage(storageType).getItem(key as string)
|
|
}
|
|
|
|
export function remove(storageType: StorageType, key: LocalStorageKey) {
|
|
getStorage(storageType).removeItem(key as string)
|
|
}
|
|
|
|
export function storeOrRemoveNull(
|
|
storageType: StorageType,
|
|
key: LocalStorageKey,
|
|
value: string | null | undefined
|
|
) {
|
|
if (value === null || value === undefined) {
|
|
remove(storageType, key)
|
|
} else {
|
|
store(storageType, key, value)
|
|
}
|
|
}
|