2022-08-23 14:20:02 +02:00
|
|
|
import UIContext, { themeMode } from 'contexts/UIContext';
|
|
|
|
import { useContext } from 'react';
|
|
|
|
import { setLocalStorageItem } from 'utils/storage';
|
|
|
|
import mainTheme from 'themes/theme';
|
|
|
|
import darkTheme from 'themes/dark-theme';
|
|
|
|
import { Theme } from '@emotion/react';
|
|
|
|
|
|
|
|
interface IUseThemeModeOutput {
|
|
|
|
resolveTheme: () => Theme;
|
|
|
|
onSetThemeMode: () => void;
|
|
|
|
themeMode: themeMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useThemeMode = (): IUseThemeModeOutput => {
|
|
|
|
const { themeMode, setThemeMode } = useContext(UIContext);
|
2023-03-22 15:37:40 +01:00
|
|
|
const key = 'unleash-theme';
|
2022-08-23 14:20:02 +02:00
|
|
|
|
|
|
|
const resolveTheme = () => {
|
|
|
|
if (themeMode === 'light') {
|
|
|
|
return mainTheme;
|
|
|
|
}
|
|
|
|
|
|
|
|
return darkTheme;
|
|
|
|
};
|
|
|
|
|
|
|
|
const onSetThemeMode = () => {
|
|
|
|
setThemeMode((prev: themeMode) => {
|
|
|
|
if (prev === 'light') {
|
2022-11-23 14:58:02 +01:00
|
|
|
setLocalStorageItem(key, 'dark');
|
2022-08-23 14:20:02 +02:00
|
|
|
return 'dark';
|
|
|
|
}
|
2022-11-23 14:58:02 +01:00
|
|
|
setLocalStorageItem(key, 'light');
|
2022-08-23 14:20:02 +02:00
|
|
|
return 'light';
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return { resolveTheme, onSetThemeMode, themeMode };
|
|
|
|
};
|