forked from Creditra/Creditra-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.ts
More file actions
48 lines (45 loc) · 1.36 KB
/
Copy pathstorage.ts
File metadata and controls
48 lines (45 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Safe wrappers around `localStorage`.
*
* Storage can throw in private browsing modes, in iframes with strict
* cookie policies, and when the quota is exceeded. These helpers return
* sensible fallbacks instead of letting the exception bubble up and
* crash the React render.
*/
/**
* Read and JSON-parse a value from localStorage.
*
* Returns `fallback` if the key is missing, parsing fails, or storage is
* unavailable.
*/
export function readJson<T>(key: string, fallback: T): T {
try {
if (typeof window === 'undefined' || !window.localStorage) return fallback;
const raw = window.localStorage.getItem(key);
if (raw === null) return fallback;
return JSON.parse(raw) as T;
} catch {
return fallback;
}
}
/**
* JSON-serialize a value and write it to localStorage. Silently no-ops
* on failure.
*/
export function writeJson<T>(key: string, value: T): void {
try {
if (typeof window === 'undefined' || !window.localStorage) return;
window.localStorage.setItem(key, JSON.stringify(value));
} catch {
/* swallow — quota or private-mode write failure */
}
}
/** Remove a key from localStorage. Silently no-ops on failure. */
export function removeKey(key: string): void {
try {
if (typeof window === 'undefined' || !window.localStorage) return;
window.localStorage.removeItem(key);
} catch {
/* swallow */
}
}