Fix initial theme setting

This commit is contained in:
Kaspar Emanuel 2022-12-15 18:41:10 +00:00 committed by blessedcoolant
parent 6e0f3475b4
commit 0b10835269

View File

@ -1,3 +1,4 @@
import { useEffect } from 'react';
import { useColorMode, VStack } from '@chakra-ui/react';
import { RootState } from 'app/store';
import { useAppDispatch, useAppSelector } from 'app/storeHooks';
@ -10,14 +11,21 @@ import IAIButton from 'common/components/IAIButton';
const THEMES = ['dark', 'light', 'green'];
export default function ThemeChanger() {
const { setColorMode } = useColorMode();
const { setColorMode, colorMode } = useColorMode();
const dispatch = useAppDispatch();
const currentTheme = useAppSelector(
(state: RootState) => state.options.currentTheme
);
useEffect(() => {
// syncs the redux store theme to the chakra's theme on startup and when
// setCurrentTheme is dispatched
if (colorMode !== currentTheme) {
setColorMode(currentTheme);
}
}, [colorMode, currentTheme]);
const handleChangeTheme = (theme: string) => {
setColorMode(theme);
dispatch(setCurrentTheme(theme));
};