Skip to content

Commit d42adeb

Browse files
committed
fix: safely access local storage
1 parent c33163e commit d42adeb

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

src/components/privacy.tsx

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,33 @@ const PrivacyCtx = createContext<{
99
setMode: (m: PrivacyMode) => void;
1010
}>({
1111
mode: "off",
12+
/* eslint-disable-next-line @typescript-eslint/no-empty-function */
1213
setMode: () => {},
1314
});
1415

1516
export function PrivacyProvider({ children }: { children: React.ReactNode }) {
16-
const [mode, setMode] = useState<PrivacyMode>(
17-
() => (localStorage.getItem("vaultPrivacyMode") as PrivacyMode) || "off",
18-
);
17+
const [mode, setMode] = useState<PrivacyMode>("off");
18+
19+
useEffect(() => {
20+
try {
21+
const stored = window.localStorage.getItem(
22+
"vaultPrivacyMode",
23+
) as PrivacyMode | null;
24+
if (stored === "off" || stored === "blur" || stored === "hoverToReveal") {
25+
setMode(stored);
26+
}
27+
} catch {
28+
// ignore access errors (e.g., disabled storage)
29+
}
30+
}, []);
1931

20-
useEffect(() => localStorage.setItem("vaultPrivacyMode", mode), [mode]);
32+
useEffect(() => {
33+
try {
34+
window.localStorage.setItem("vaultPrivacyMode", mode);
35+
} catch {
36+
// ignore quota/access errors
37+
}
38+
}, [mode]);
2139

2240
return (
2341
<PrivacyCtx.Provider value={{ mode, setMode }}>

0 commit comments

Comments
 (0)