diff --git a/frontend/packages/core/src/store/theme/reducers.ts b/frontend/packages/core/src/store/theme/reducers.ts index df83f6c7efa07007e080b3c81df0ca27d749b41d..ca9e0dd3b64744b5855ec54d5e0c4288265d772b 100644 --- a/frontend/packages/core/src/store/theme/reducers.ts +++ b/frontend/packages/core/src/store/theme/reducers.ts @@ -14,15 +14,14 @@ * limitations under the License. */ -import {THEME, autoTheme} from '~/utils/theme'; +import {THEME, autoTheme, parseTheme} from '~/utils/theme'; import type {ThemeActionTypes, ThemeState} from './types'; import {ActionTypes} from './types'; -import type {Theme} from '~/utils/theme'; const STORAGE_KEY = 'theme'; -const theme = THEME || (window.localStorage.getItem(STORAGE_KEY) as Theme | undefined) || 'auto'; +const theme = THEME || parseTheme(window.localStorage.getItem(STORAGE_KEY) || '') || 'auto'; const initState: ThemeState = { theme: theme === 'auto' ? autoTheme : theme, diff --git a/frontend/packages/core/src/utils/theme.ts b/frontend/packages/core/src/utils/theme.ts index 65895f9a041c751136fe3c5595d8963e85e67006..45e02a8217a1dfe1d36d00698f6747d577e2cfb0 100644 --- a/frontend/packages/core/src/utils/theme.ts +++ b/frontend/packages/core/src/utils/theme.ts @@ -21,7 +21,9 @@ import kebabCase from 'lodash/kebabCase'; export type Theme = 'light' | 'dark'; -export const THEME: Theme | undefined = import.meta.env.SNOWPACK_PUBLIC_THEME; +export const parseTheme = (t: string): Theme | undefined => (['light', 'dark'].includes(t) ? (t as Theme) : undefined); + +export const THEME = parseTheme(import.meta.env.SNOWPACK_PUBLIC_THEME); export const matchMedia = window.matchMedia('(prefers-color-scheme: dark)');