From 902d4c83bab934fe93a47eef56ac56c9b79504f4 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Tue, 19 Mar 2024 10:38:16 -0700 Subject: [PATCH] Updated theme check. --- src/components/hooks/useTheme.ts | 10 ++-------- src/store/app.ts | 10 +++++++++- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/components/hooks/useTheme.ts b/src/components/hooks/useTheme.ts index 099bf962..f2a2d448 100644 --- a/src/components/hooks/useTheme.ts +++ b/src/components/hooks/useTheme.ts @@ -1,19 +1,13 @@ import { useEffect } from 'react'; import useStore, { setTheme } from 'store/app'; import { getItem, setItem } from 'next-basics'; -import { THEME_COLORS, THEME_CONFIG } from 'lib/constants'; +import { DEFAULT_THEME, THEME_COLORS, THEME_CONFIG } from 'lib/constants'; import { colord } from 'colord'; const selector = (state: { theme: string }) => state.theme; export function useTheme() { - const defaultTheme = - typeof window !== 'undefined' - ? window?.matchMedia('(prefers-color-scheme: dark)')?.matches - ? 'dark' - : 'light' - : 'light'; - const theme = useStore(selector) || getItem(THEME_CONFIG) || defaultTheme; + const theme = useStore(selector) || getItem(THEME_CONFIG) || DEFAULT_THEME; const primaryColor = colord(THEME_COLORS[theme].primary); const colors = { diff --git a/src/store/app.ts b/src/store/app.ts index 8fa1a0d5..cbd2ac2d 100644 --- a/src/store/app.ts +++ b/src/store/app.ts @@ -9,9 +9,17 @@ import { } from 'lib/constants'; import { getItem } from 'next-basics'; +function getDefaultTheme() { + return typeof window !== 'undefined' + ? window?.matchMedia('(prefers-color-scheme: dark)')?.matches + ? 'dark' + : 'light' + : 'light'; +} + const initialState = { locale: getItem(LOCALE_CONFIG) || DEFAULT_LOCALE, - theme: getItem(THEME_CONFIG) || DEFAULT_THEME, + theme: getItem(THEME_CONFIG) || getDefaultTheme() || DEFAULT_THEME, dateRange: getItem(DATE_RANGE_CONFIG) || DEFAULT_DATE_RANGE, shareToken: null, user: null,