mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2025-01-06 14:13:06 -06:00
Optimise logic used for OS theme syncing. #5932
This commit is contained in:
parent
2b45a1f7d5
commit
fedbe550db
@ -774,7 +774,6 @@ function getFinalTheme(baseTheme) {
|
||||
export default function Theme({children}) {
|
||||
const prefStore = usePreferences();
|
||||
const [theme, setTheme] = useState(prefStore.getPreferencesForModule('misc')?.theme);
|
||||
const [selectedMode, setSelectedMode] = useState(prefStore.getPreferencesForModule('misc')?.theme || 'light');
|
||||
|
||||
const themeObj = useMemo(()=>{
|
||||
let baseTheme = getLightTheme(basicSettings);
|
||||
@ -789,29 +788,26 @@ export default function Theme({children}) {
|
||||
return getFinalTheme(baseTheme);
|
||||
}, [theme]);
|
||||
|
||||
useEffect(() => usePreferences.subscribe(
|
||||
state => {
|
||||
setSelectedMode(state.getPreferencesForModule('misc').theme);
|
||||
setTheme(state.getPreferencesForModule('misc').theme);
|
||||
}),[theme]);
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedMode !== 'system') {
|
||||
setTheme(selectedMode);
|
||||
const selectedTheme = prefStore.getPreferencesForModule('misc').theme;
|
||||
if(theme && theme === selectedTheme) {
|
||||
return;
|
||||
}else{
|
||||
if (selectedTheme !== 'system') {
|
||||
setTheme(selectedTheme);
|
||||
return;
|
||||
}
|
||||
const isSystemInDarkMode = matchMedia('(prefers-color-scheme: dark)');
|
||||
setTheme(isSystemInDarkMode.matches ? 'dark' : 'light');
|
||||
const listener = (event) => {
|
||||
setTheme(event.matches ? 'dark' : 'light');
|
||||
};
|
||||
isSystemInDarkMode.addEventListener('change',listener);
|
||||
return () => {
|
||||
isSystemInDarkMode.removeEventListener('change',listener);
|
||||
};
|
||||
}
|
||||
|
||||
const isSystemInDarkMode = matchMedia('(prefers-color-scheme: dark)');
|
||||
setTheme(isSystemInDarkMode.matches ? 'dark' : 'light');
|
||||
|
||||
const listener = (event) => {
|
||||
setTheme(event.matches ? 'dark' : 'light');
|
||||
};
|
||||
isSystemInDarkMode.addEventListener('change',listener);
|
||||
return () => {
|
||||
isSystemInDarkMode.removeEventListener('change',listener);
|
||||
};
|
||||
},[selectedMode]);
|
||||
},[prefStore]);
|
||||
|
||||
return (
|
||||
<ThemeProvider theme={themeObj}>
|
||||
|
Loading…
Reference in New Issue
Block a user