mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
* fix: database bugs * fix: calendar picker * fix: the position of collapse menu button * fix: modified some style * fix: slash command * fix: selection style * fix: support toggle inline formula * fix: block color effect grid block * fix: if isRange and date is greater than endDate, swap date and endDate * fix: remove sorting before insert row * fix: toggle property visible status * fix: modified tauri window size * fix: placeholder should be hidden when composing * fix: support href shortcut * fix: prevent submit when the formula has error * fix: modified layout selection * fix: add padding for record edit * fix: remove sorts before drag row * fix: modified chip style * fix: if previous node is an embed, merge the current node to another node which is not an embed * fix: modified emoji picker
67 lines
2.0 KiB
TypeScript
67 lines
2.0 KiB
TypeScript
import { useAppDispatch, useAppSelector } from '$app/stores/store';
|
|
import { useEffect, useMemo } from 'react';
|
|
import { currentUserActions } from '$app_reducers/current-user/slice';
|
|
import { Theme as ThemeType, ThemeMode } from '$app/stores/reducers/current-user/slice';
|
|
import { createTheme } from '@mui/material/styles';
|
|
import { getDesignTokens } from '$app/utils/mui';
|
|
import { useTranslation } from 'react-i18next';
|
|
import { UserService } from '$app/application/user/user.service';
|
|
|
|
export function useUserSetting() {
|
|
const dispatch = useAppDispatch();
|
|
const { i18n } = useTranslation();
|
|
const { themeMode = ThemeMode.System, theme: themeType = ThemeType.Default } = useAppSelector((state) => {
|
|
return {
|
|
themeMode: state.currentUser.userSetting.themeMode,
|
|
theme: state.currentUser.userSetting.theme,
|
|
};
|
|
});
|
|
|
|
const isDark =
|
|
themeMode === ThemeMode.Dark ||
|
|
(themeMode === ThemeMode.System && window.matchMedia('(prefers-color-scheme: dark)').matches);
|
|
|
|
useEffect(() => {
|
|
void (async () => {
|
|
const settings = await UserService.getAppearanceSetting();
|
|
|
|
if (!settings) return;
|
|
dispatch(currentUserActions.setUserSetting(settings));
|
|
await i18n.changeLanguage(settings.language);
|
|
})();
|
|
}, [dispatch, i18n]);
|
|
|
|
useEffect(() => {
|
|
const html = document.documentElement;
|
|
|
|
html?.setAttribute('data-dark-mode', String(isDark));
|
|
}, [isDark]);
|
|
|
|
useEffect(() => {
|
|
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
|
|
|
|
const handleSystemThemeChange = () => {
|
|
if (themeMode !== ThemeMode.System) return;
|
|
dispatch(
|
|
currentUserActions.setUserSetting({
|
|
isDark: mediaQuery.matches,
|
|
})
|
|
);
|
|
};
|
|
|
|
mediaQuery.addEventListener('change', handleSystemThemeChange);
|
|
|
|
return () => {
|
|
mediaQuery.removeEventListener('change', handleSystemThemeChange);
|
|
};
|
|
}, [dispatch, themeMode]);
|
|
|
|
const muiTheme = useMemo(() => createTheme(getDesignTokens(isDark)), [isDark]);
|
|
|
|
return {
|
|
muiTheme,
|
|
themeMode,
|
|
themeType,
|
|
};
|
|
}
|