diff --git a/src/utils/store/GoldRush.tsx b/src/utils/store/GoldRush.tsx index 702e801e..b256ff2b 100644 --- a/src/utils/store/GoldRush.tsx +++ b/src/utils/store/GoldRush.tsx @@ -10,11 +10,11 @@ import { type GoldRushThemeType, type GoldRushContextType, type GoldRushProviderProps, - type GoldRushThemeColorType, } from "../types/store.types"; import { type ChainItem, CovalentClient } from "@covalenthq/client-sdk"; import { primaryShades } from "../functions"; import { SEARCH_RESULTS_TYPE } from "../constants/shared.constants"; +import defaultsDeep from "lodash/defaultsDeep"; const GoldRushContext = createContext( {} as GoldRushContextType @@ -55,7 +55,12 @@ export const GoldRushProvider: React.FC = ({ const [chains, setChains] = useState(null); const [selectedChain, setSelectedChain] = useState(null); - const [theme, setTheme] = useState(defaultTheme); + const [theme, setTheme] = useState( + defaultsDeep( + JSON.parse(localStorage.getItem("goldrush_theme") || "null") ?? {}, + defaultsDeep(newTheme, defaultTheme) + ) + ); useEffect(() => { (async () => { @@ -72,111 +77,69 @@ export const GoldRushProvider: React.FC = ({ }, []); useEffect(() => { - const existingTheme = localStorage.getItem("goldrush_theme") || null; - - if (newTheme) { - updateThemeHandler(newTheme); - } else if (existingTheme) { - applyThemeHandler(JSON.parse(existingTheme)); - } else { - applyThemeHandler(defaultTheme); - } - }, []); + localStorage.setItem("goldrush_theme", JSON.stringify(theme)); + const { borderRadius, colors, mode, style } = theme; - const updateThemeHandler = useCallback( - ({ borderRadius, colors, mode, style }: Partial) => { - const updatedTheme: GoldRushThemeType = { ...theme }; + const body = document.body; + const root = document.documentElement; - if (borderRadius) { - updatedTheme.borderRadius = borderRadius; + switch (mode) { + case "dark": { + body.classList.add("dark"); + root.classList.add("dark"); + break; } - if (mode) { - updatedTheme.mode = mode; - } - if (style) { - updatedTheme.style = style; - } - if (colors) { - Object.entries(colors).forEach(([_mode, _types]) => { - Object.entries(_types).forEach(([_type, value]) => { - updatedTheme.colors[_mode as "dark" | "light"]![ - _type as keyof GoldRushThemeColorType - ] = value; - }); - }); + case "light": { + body.classList.remove("dark"); + root.classList.remove("dark"); + break; } + } - applyThemeHandler(updatedTheme); - }, - [theme] - ); - - const applyThemeHandler = useCallback( - ({ borderRadius, colors, mode, style }: GoldRushThemeType) => { - const body = document.body; - const root = document.documentElement; - - switch (mode) { - case "dark": { - body.classList.add("dark"); - root.classList.add("dark"); - break; - } - case "light": { - body.classList.remove("dark"); - root.classList.remove("dark"); - break; - } + switch (style) { + case "neo": { + body.classList.add("neo"); + root.classList.add("neo"); + break; } - - switch (style) { - case "neo": { - body.classList.add("neo"); - root.classList.add("neo"); - break; - } - case "classic": { - body.classList.remove("neo"); - root.classList.remove("neo"); - break; - } + case "classic": { + body.classList.remove("neo"); + root.classList.remove("neo"); + break; } + } - root.style.setProperty("--grk-border-radius", `${borderRadius}px`); + root.style.setProperty("--grk-border-radius", `${borderRadius}px`); - Object.entries(colors).forEach(([_mode, _types]) => { - Object.entries(_types).forEach(([_type, value]) => { - if (_type === "primary") { - const shades = primaryShades( - value, - _mode as GoldRushThemeType["mode"] - ); - Object.entries(shades).forEach(([shade, color]) => { - root.style.setProperty( - `--grk-${_type}-${_mode}-${shade}`, - color - ); - }); - } else { + Object.entries(colors).forEach(([_mode, _types]) => { + Object.entries(_types).forEach(([_type, value]) => { + if (_type === "primary") { + const shades = primaryShades( + value, + _mode as GoldRushThemeType["mode"] + ); + Object.entries(shades).forEach(([shade, color]) => { root.style.setProperty( - `--grk-${_type}-${_mode}`, - value + `--grk-${_type}-${_mode}-${shade}`, + color ); - } - }); + }); + } else { + root.style.setProperty(`--grk-${_type}-${_mode}`, value); + } }); + }); + }, [theme]); - const _theme: GoldRushThemeType = { - borderRadius, - colors, - mode, - style, - }; - - setTheme(_theme); - localStorage.setItem("goldrush_theme", JSON.stringify(_theme)); + const updateThemeHandler = useCallback( + (updateTheme: Partial) => { + const updatedTheme: GoldRushThemeType = defaultsDeep( + updateTheme, + theme + ); + setTheme(updatedTheme); }, - [] + [theme] ); const resetThemeHandler = useCallback(() => {