Skip to content

Commit

Permalink
feat: system theme resolves as dark or light
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisVorop committed Dec 22, 2023
1 parent 4117d41 commit 600a7b6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
17 changes: 17 additions & 0 deletions src/components/ThemeSetter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useTheme } from 'next-themes';
import { ReactNode, useEffect } from 'react';

import { trpc } from '../utils/trpcClient';

export const ThemeSetter = ({ children }: { children: ReactNode }) => {
const updateSettingsMutation = trpc.user.updateSettings.useMutation();
const { resolvedTheme, theme } = useTheme();

useEffect(() => {
if (theme !== 'system' || resolvedTheme === 'system') return;
updateSettingsMutation.mutateAsync({ theme: resolvedTheme });
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [resolvedTheme]);

return children;
};
13 changes: 8 additions & 5 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { trpc } from '../utils/trpcClient';
import { TLocale, setSSRLocale } from '../utils/getLang';
import { GoalPreviewProvider } from '../components/GoalPreview/GoalPreviewProvider';
import { getTelemetryInstanceSingleton, useWebTelemetryMonitoringInit } from '../utils/telemetry';
import { ThemeSetter } from '../components/ThemeSetter';

const defaultThemes = ['light', 'dark'];

Expand Down Expand Up @@ -38,11 +39,13 @@ const App = ({ Component, pageProps, router }: AppProps) => {

<SessionProvider session={pageProps.session} refetchOnWindowFocus={true}>
<ThemeProvider themes={defaultThemes}>
<GoalPreviewProvider>
<PageLoadProgress height={2} ref={pageLoadRef} />
<Component {...pageProps} />
<ReactQueryDevtools />
</GoalPreviewProvider>
<ThemeSetter>
<GoalPreviewProvider>
<PageLoadProgress height={2} ref={pageLoadRef} />
<Component {...pageProps} />
<ReactQueryDevtools />
</GoalPreviewProvider>
</ThemeSetter>
</ThemeProvider>
</SessionProvider>
</>
Expand Down

0 comments on commit 600a7b6

Please sign in to comment.