Skip to content

Commit

Permalink
Fix #1507 to ignore invalid theme value in localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamahl19 committed Feb 1, 2024
1 parent 5b6e3ca commit a03a9de
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 38 deletions.
50 changes: 21 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"react": "18.2.0",
"react-dom": "18.2.0",
"react-error-boundary": "4.0.12",
"react-hook-form": "7.49.3",
"react-hook-form": "7.50.0",
"react-i18next": "14.0.1",
"react-router-dom": "6.21.3",
"sonner": "1.4.0",
Expand All @@ -79,14 +79,14 @@
"@swc-jotai/debug-label": "0.1.0",
"@swc-jotai/react-refresh": "0.1.0",
"@tanstack/eslint-plugin-query": "5.18.0",
"@testing-library/jest-dom": "6.4.0",
"@testing-library/jest-dom": "6.4.1",
"@testing-library/react": "14.2.0",
"@testing-library/user-event": "14.5.2",
"@total-typescript/ts-reset": "0.5.1",
"@tsconfig/strictest": "2.0.2",
"@tsconfig/vite-react": "3.0.0",
"@types/node": "20.11.14",
"@types/react": "18.2.48",
"@types/node": "20.11.15",
"@types/react": "18.2.51",
"@types/react-dom": "18.2.18",
"@typescript-eslint/eslint-plugin": "6.20.0",
"@typescript-eslint/parser": "6.20.0",
Expand Down Expand Up @@ -126,7 +126,7 @@
"terser": "5.27.0",
"typescript": "5.3.3",
"vite": "5.0.12",
"vite-plugin-checker": "0.6.3",
"vite-plugin-checker": "0.6.4",
"vite-plugin-svgr": "4.2.0",
"vite-tsconfig-paths": "4.3.1",
"vitest": "1.2.2"
Expand Down
13 changes: 9 additions & 4 deletions src/app/providers/Theme.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, type ReactNode, useMemo, useCallback } from 'react';
import { useTranslation } from 'react-i18next';
import { useAtom } from 'jotai';
import { atom, useAtom } from 'jotai';
import { atomWithStorage } from 'jotai/utils';

const STORAGE_KEY = 'theme';
Expand All @@ -27,15 +27,20 @@ const setThemeAttr = (theme: string) => {
rootEl.classList.add(theme);
};

const themePersistedAtom = atomWithStorage<Theme | typeof SYSTEM_THEME>(
const persistedThemeAtom = atomWithStorage<Theme | typeof SYSTEM_THEME>(
STORAGE_KEY,
getValidTheme(localStorage.getItem(STORAGE_KEY) ?? ''),
);

const themeAtom = atom(
(get) => getValidTheme(get(persistedThemeAtom)),
(_, set, theme: string) => set(persistedThemeAtom, getValidTheme(theme)),
);

export const useTheme = () => {
const { t } = useTranslation('global');

const [theme, setTheme] = useAtom(themePersistedAtom);
const [theme, setTheme] = useAtom(themeAtom);

const themes = useMemo(
() => [
Expand All @@ -51,7 +56,7 @@ export const useTheme = () => {
({
theme: theme === SYSTEM_THEME ? getSystemTheme() : theme,
rawTheme: theme,
setTheme: (theme: string) => setTheme(getValidTheme(theme)),
setTheme,
themes,
}) as const,
[theme, setTheme, themes],
Expand Down

0 comments on commit a03a9de

Please sign in to comment.