From 6f4cbad0832d5bd96ba3fcfbc6e80a12861f7cf6 Mon Sep 17 00:00:00 2001 From: Zaiba Machhaliya Date: Sat, 22 Aug 2026 12:55:48 +0530 Subject: [PATCH 1/3] fix: add validation for localStorage theme values --- src/context/ThemeContext.jsx | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/context/ThemeContext.jsx b/src/context/ThemeContext.jsx index b27d1fc..21beb24 100644 --- a/src/context/ThemeContext.jsx +++ b/src/context/ThemeContext.jsx @@ -3,8 +3,23 @@ import { createContext, useContext, useState, useEffect } from 'react' const ThemeCtx = createContext(null) export function ThemeProvider({ children }) { + const [theme, setTheme] = useState(() => { - return localStorage.getItem('oe_theme') || 'dark' + try { + const stored = localStorage.getItem('oe_theme') + + if (stored === 'dark' || stored === 'light') { + return stored + } + + if (stored) { + console.warn(`Invalid theme value found in localStorage: "${stored}". Falling back to 'dark'.`) + } + } catch (error) { + console.warn('Failed to read theme from localStorage:', error) + } + + return 'dark' }) useEffect(() => { From 232934587788fca104c515c2252994d328140f3f Mon Sep 17 00:00:00 2001 From: Zaiba Machhaliya Date: Sat, 22 Aug 2026 13:05:17 +0530 Subject: [PATCH 2/3] fix: add try/catch for localStorage write operation --- src/context/ThemeContext.jsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/context/ThemeContext.jsx b/src/context/ThemeContext.jsx index 21beb24..92073fe 100644 --- a/src/context/ThemeContext.jsx +++ b/src/context/ThemeContext.jsx @@ -24,7 +24,11 @@ export function ThemeProvider({ children }) { useEffect(() => { document.documentElement.setAttribute('data-theme', theme) - localStorage.setItem('oe_theme', theme) + try { + localStorage.setItem('oe_theme', theme) + } catch (error) { + console.warn('Failed to save theme to localStorage:', error) + } }, [theme]) const toggleTheme = () => { From bd3fa8a2bcfcb45d5008202ee3a14a514f2f0e6e Mon Sep 17 00:00:00 2001 From: Zaiba Machhaliya Date: Sat, 22 Aug 2026 13:42:16 +0530 Subject: [PATCH 3/3] fix: add complete localStorage theme validation --- src/context/ThemeContext.jsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/context/ThemeContext.jsx b/src/context/ThemeContext.jsx index 92073fe..3f1ce89 100644 --- a/src/context/ThemeContext.jsx +++ b/src/context/ThemeContext.jsx @@ -12,9 +12,10 @@ export function ThemeProvider({ children }) { return stored } - if (stored) { + if (stored !== null) { console.warn(`Invalid theme value found in localStorage: "${stored}". Falling back to 'dark'.`) } + } catch (error) { console.warn('Failed to read theme from localStorage:', error) }