36 lines
747 B
TypeScript
36 lines
747 B
TypeScript
const STORAGE_PREFIX = 'cmpp-platform-v2';
|
|
|
|
export function readLocalData<T>(key: string, fallback: T): T {
|
|
if (typeof window === 'undefined') {
|
|
return fallback;
|
|
}
|
|
|
|
const rawValue = window.localStorage.getItem(`${STORAGE_PREFIX}:${key}`);
|
|
|
|
if (!rawValue) {
|
|
return fallback;
|
|
}
|
|
|
|
try {
|
|
return JSON.parse(rawValue) as T;
|
|
} catch {
|
|
return fallback;
|
|
}
|
|
}
|
|
|
|
export function writeLocalData<T>(key: string, value: T) {
|
|
if (typeof window === 'undefined') {
|
|
return;
|
|
}
|
|
|
|
window.localStorage.setItem(`${STORAGE_PREFIX}:${key}`, JSON.stringify(value));
|
|
}
|
|
|
|
export function resetLocalData(key: string) {
|
|
if (typeof window === 'undefined') {
|
|
return;
|
|
}
|
|
|
|
window.localStorage.removeItem(`${STORAGE_PREFIX}:${key}`);
|
|
}
|