22
33import { useEffect } from 'react' ;
44import { Switch } from '@headlessui/react' ;
5- import { useDispatchContext , useThemeContext } from '@/hooks/State' ;
5+ import { useDispatchContextTheme , useThemeContext } from '@/hooks/State' ;
66import useStorage from '@/hooks/Storage' ;
77import handleState from '@/state/actions' ;
88import { isLightTheme , checkCookies } from '@/utilities/utils' ;
@@ -19,7 +19,7 @@ const ThemeSwitch = (): React.ReactNode => {
1919 const themeSaved = getStorage ( LABEL ) ?? '' ;
2020 const cookiesStorage = getStorage ( ACTION . COOKIES ) ;
2121 const theme = useThemeContext ( ) ;
22- const dispatch = useDispatchContext ( ) ;
22+ const dispatch = useDispatchContextTheme ( ) ;
2323
2424 useEffect ( ( ) => {
2525 // User preference + system-aware detection
@@ -28,7 +28,7 @@ const ThemeSwitch = (): React.ReactNode => {
2828
2929 handleState (
3030 { type : ACTION . THEME , element : isDark ? DARK : LIGHT } ,
31- dispatch ,
31+ dispatch as keyof typeof dispatch ,
3232 ) ;
3333 enableTheme ( isDark ) ;
3434 } , [ ] ) ;
@@ -61,18 +61,12 @@ const ThemeSwitch = (): React.ReactNode => {
6161 const handleTheme = ( value : boolean ) : void => {
6262 enableTheme ( value ) ;
6363
64- // Storage + state check
65- if (
66- themeSaved !== '' ||
67- ( cookiesStorage &&
68- checkCookies ( JSON . parse ( cookiesStorage ) , Cookie . Essentials ) )
69- ) {
70- setStorage ( LABEL , value ? LIGHT : DARK ) ;
71- }
64+ const selected = value ? DARK : LIGHT ;
7265
66+ setStorage ( LABEL , selected ) ;
7367 handleState (
74- { type : ACTION . THEME , element : value ? DARK : LIGHT } ,
75- dispatch ,
68+ { type : ACTION . THEME , element : selected } ,
69+ dispatch as keyof typeof dispatch ,
7670 ) ;
7771 } ;
7872
0 commit comments