2023-01-11 14:39:06 +05:30
|
|
|
import { theme as antdTheme } from 'antd';
|
|
|
|
|
import { ThemeConfig } from 'antd/es/config-provider/context';
|
|
|
|
|
import get from 'api/browser/localstorage/get';
|
|
|
|
|
import set from 'api/browser/localstorage/set';
|
|
|
|
|
import { LOCALSTORAGE } from 'constants/localStorage';
|
2023-05-19 13:14:32 +05:30
|
|
|
import {
|
|
|
|
|
createContext,
|
|
|
|
|
ReactNode,
|
|
|
|
|
useCallback,
|
|
|
|
|
useContext,
|
|
|
|
|
useMemo,
|
|
|
|
|
useState,
|
|
|
|
|
} from 'react';
|
2023-01-11 14:39:06 +05:30
|
|
|
|
|
|
|
|
import { THEME_MODE } from './constant';
|
|
|
|
|
|
|
|
|
|
export const ThemeContext = createContext({
|
|
|
|
|
theme: THEME_MODE.DARK,
|
|
|
|
|
toggleTheme: () => {},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export function ThemeProvider({ children }: ThemeProviderProps): JSX.Element {
|
|
|
|
|
const [theme, setTheme] = useState(get(LOCALSTORAGE.THEME) || THEME_MODE.DARK);
|
|
|
|
|
|
|
|
|
|
const toggleTheme = useCallback(() => {
|
|
|
|
|
if (theme === THEME_MODE.LIGHT) {
|
|
|
|
|
setTheme(THEME_MODE.DARK);
|
|
|
|
|
set(LOCALSTORAGE.THEME, THEME_MODE.DARK);
|
|
|
|
|
} else {
|
|
|
|
|
setTheme(THEME_MODE.LIGHT);
|
|
|
|
|
set(LOCALSTORAGE.THEME, THEME_MODE.LIGHT);
|
|
|
|
|
}
|
2024-06-06 17:04:00 +05:30
|
|
|
set(LOCALSTORAGE.THEME_ANALYTICS_V1, '');
|
2023-01-11 14:39:06 +05:30
|
|
|
}, [theme]);
|
|
|
|
|
|
|
|
|
|
const value = useMemo(
|
|
|
|
|
() => ({
|
|
|
|
|
theme,
|
|
|
|
|
toggleTheme,
|
|
|
|
|
}),
|
|
|
|
|
[theme, toggleTheme],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ThemeProviderProps {
|
2023-05-19 13:14:32 +05:30
|
|
|
children: ReactNode;
|
2023-01-11 14:39:06 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ThemeMode {
|
|
|
|
|
theme: string;
|
|
|
|
|
toggleTheme: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const useThemeMode = (): ThemeMode => {
|
2023-05-19 13:14:32 +05:30
|
|
|
const { theme, toggleTheme } = useContext(ThemeContext);
|
2023-01-11 14:39:06 +05:30
|
|
|
|
|
|
|
|
return { theme, toggleTheme };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useIsDarkMode = (): boolean => {
|
2023-05-19 13:14:32 +05:30
|
|
|
const { theme } = useContext(ThemeContext);
|
2023-01-11 14:39:06 +05:30
|
|
|
|
|
|
|
|
return theme === THEME_MODE.DARK;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useThemeConfig = (): ThemeConfig => {
|
|
|
|
|
const isDarkMode = useIsDarkMode();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
algorithm: isDarkMode ? antdTheme.darkAlgorithm : antdTheme.defaultAlgorithm,
|
|
|
|
|
token: {
|
|
|
|
|
borderRadius: 2,
|
|
|
|
|
borderRadiusLG: 2,
|
|
|
|
|
borderRadiusSM: 2,
|
|
|
|
|
borderRadiusXS: 2,
|
2023-11-20 14:53:13 +05:30
|
|
|
fontFamily: 'Inter',
|
|
|
|
|
fontSize: 13,
|
2024-01-05 11:15:31 +05:30
|
|
|
colorPrimary: '#4E74F8',
|
|
|
|
|
colorBgBase: isDarkMode ? '#0B0C0E' : '#fff',
|
|
|
|
|
colorBgContainer: isDarkMode ? '#121317' : '#fff',
|
|
|
|
|
colorLink: '#4E74F8',
|
|
|
|
|
colorPrimaryText: '#3F5ECC',
|
2023-01-11 14:39:06 +05:30
|
|
|
},
|
2024-02-12 00:23:19 +05:30
|
|
|
components: {
|
|
|
|
|
Dropdown: {
|
|
|
|
|
colorBgElevated: isDarkMode ? '#121317' : '#fff',
|
|
|
|
|
controlItemBgHover: isDarkMode ? '#1D212D' : '#fff',
|
|
|
|
|
colorText: isDarkMode ? '#C0C1C3' : '#121317',
|
|
|
|
|
fontSize: 12,
|
|
|
|
|
},
|
|
|
|
|
Select: {
|
|
|
|
|
colorBgElevated: isDarkMode ? '#121317' : '#fff',
|
|
|
|
|
controlItemBgHover: isDarkMode ? '#1D212D' : '#fff',
|
|
|
|
|
boxShadowSecondary: isDarkMode
|
|
|
|
|
? '4px 10px 16px 2px rgba(0, 0, 0, 0.30)'
|
|
|
|
|
: '#fff',
|
|
|
|
|
colorText: isDarkMode ? '#C0C1C3' : '#121317',
|
|
|
|
|
fontSize: 12,
|
|
|
|
|
},
|
|
|
|
|
Button: {
|
|
|
|
|
paddingInline: 12,
|
|
|
|
|
fontSize: 12,
|
|
|
|
|
},
|
|
|
|
|
Input: {
|
|
|
|
|
colorBorder: isDarkMode ? '#1D212D' : '#E9E9E9',
|
|
|
|
|
},
|
2025-01-30 21:51:49 +05:30
|
|
|
Breadcrumb: {
|
|
|
|
|
separatorMargin: 4,
|
|
|
|
|
},
|
2024-02-12 00:23:19 +05:30
|
|
|
},
|
2023-01-11 14:39:06 +05:30
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default useThemeMode;
|