Files
outline/app/utils/Storage.ts
Tom Moor e4e98286f4 fix: Embed disabled state should persist (#3407)
* Normalize code around localStorage
Persist disabled embed state

* fix: Cannot view more than 10 starred items on load

* More tidying of sidebar state
2022-04-17 10:24:40 -07:00

57 lines
1.2 KiB
TypeScript

/**
* Storage is a wrapper class for localStorage that allow safe usage when
* localStorage is not available.
*/
export default class Storage {
/**
* Set a value in localStorage. For efficiency, this method will remove the
* value if it is undefined.
*
* @param key The key to set under.
* @param value The value to set
*/
static set<T>(key: string, value: T) {
try {
if (value === undefined) {
this.remove(key);
} else {
localStorage.setItem(key, JSON.stringify(value));
}
} catch (error) {
// no-op Safari private mode
}
}
/**
* Get a value from localStorage.
*
* @param key The key to get.
* @returns The value or undefined if it doesn't exist.
*/
static get(key: string) {
try {
const value = localStorage.getItem(key);
if (typeof value === "string") {
return JSON.parse(value);
}
} catch (error) {
// no-op Safari private mode
}
return undefined;
}
/**
* Remove a value from localStorage.
*
* @param key The key to remove.
*/
static remove(key: string) {
try {
localStorage.removeItem(key);
} catch (error) {
// no-op Safari private mode
}
}
}