29 lines
825 B
TypeScript
29 lines
825 B
TypeScript
// TODO: fine for now however will leak state between tests (we don't really have) in future. Ideally should use a provider
|
|
export const LocalStorage = {
|
|
getItem: (key: string) => {
|
|
if (typeof window === 'undefined') return null;
|
|
try {
|
|
const item = window.localStorage.getItem(key);
|
|
return item ? JSON.parse(item) : null;
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
},
|
|
setItem: (key: string, value: any) => {
|
|
if (typeof window === 'undefined') return;
|
|
try {
|
|
window.localStorage.setItem(key, JSON.stringify(value));
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
},
|
|
removeItem: (key: string) => {
|
|
if (typeof window === 'undefined') return;
|
|
try {
|
|
window.localStorage.removeItem(key);
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
},
|
|
};
|