mirror of
https://github.com/supabase/supabase.git
synced 2026-05-07 01:10:15 -04:00
a4cfcd9b2e
* docs: user nav dropdown * www: user dropdown nav * update menus * chore: add complete local storage allowlist * move all local-storage to common * reload after logOut * add local storage key changes from #35175 * fix errors * add more keys * fix merge bugs --------- Co-authored-by: Alaister Young <a@alaisteryoung.com>
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import { LOCAL_STORAGE_KEYS } from 'common'
|
|
|
|
type LocalStorageKey = (typeof LOCAL_STORAGE_KEYS)[keyof typeof LOCAL_STORAGE_KEYS]
|
|
type StorageType = 'local' | 'session'
|
|
|
|
function getStorage(storageType: StorageType) {
|
|
return storageType === 'local' ? window.localStorage : window.sessionStorage
|
|
}
|
|
|
|
export function store(storageType: StorageType, key: LocalStorageKey, value: string) {
|
|
if (typeof window === 'undefined') return
|
|
const storage = getStorage(storageType)
|
|
|
|
try {
|
|
storage.setItem(key as string, value)
|
|
} catch {
|
|
console.error(`Failed to set storage item with key "${key}"`)
|
|
}
|
|
}
|
|
|
|
export function retrieve(storageType: StorageType, key: LocalStorageKey) {
|
|
if (typeof window === 'undefined') return
|
|
const storage = getStorage(storageType)
|
|
return storage.getItem(key as string)
|
|
}
|
|
|
|
export function remove(storageType: StorageType, key: LocalStorageKey) {
|
|
if (typeof window === 'undefined') return
|
|
const storage = getStorage(storageType)
|
|
return storage.removeItem(key as string)
|
|
}
|
|
|
|
export function storeOrRemoveNull(
|
|
storageType: StorageType,
|
|
key: LocalStorageKey,
|
|
value: string | null | undefined
|
|
) {
|
|
if (typeof window === 'undefined') return
|
|
if (value === null || value === undefined) {
|
|
remove(storageType, key)
|
|
} else {
|
|
store(storageType, key, value)
|
|
}
|
|
}
|