From 4a50fe584cd00b8096e88ee7daf3177da2683659 Mon Sep 17 00:00:00 2001 From: Himself65 Date: Thu, 4 May 2023 22:22:53 -0500 Subject: [PATCH] fix(electron): system theme (#2237) --- apps/web/src/providers/ThemeProvider.tsx | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/apps/web/src/providers/ThemeProvider.tsx b/apps/web/src/providers/ThemeProvider.tsx index ad4f5ae529..c34b401a14 100644 --- a/apps/web/src/providers/ThemeProvider.tsx +++ b/apps/web/src/providers/ThemeProvider.tsx @@ -2,17 +2,19 @@ import type { ThemeProviderProps } from '@affine/component'; import { ThemeProvider as NextThemeProvider, useTheme } from 'next-themes'; import type { PropsWithChildren } from 'react'; import type React from 'react'; -import { memo, useEffect } from 'react'; +import { memo, useRef } from 'react'; const themes = ['dark', 'light']; const DesktopThemeSync = memo(function DesktopThemeSync() { - const { resolvedTheme } = useTheme(); - useEffect(() => { - if (environment.isDesktop) { - window.apis?.onThemeChange(resolvedTheme === 'dark' ? 'dark' : 'light'); + const { theme } = useTheme(); + const lastThemeRef = useRef(theme); + if (lastThemeRef.current !== theme) { + if (environment.isDesktop && theme) { + window.apis?.onThemeChange(theme); } - }, [resolvedTheme]); + lastThemeRef.current = theme; + } return null; });