Skip to content

Commit

Permalink
Theme preference logic moved to UISlice
Browse files Browse the repository at this point in the history
  • Loading branch information
aarya-16 committed Oct 27, 2024
1 parent 8c2151d commit 0c3d824
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 28 deletions.
27 changes: 0 additions & 27 deletions Client/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@ import { useDispatch } from "react-redux";
import { getAppSettings, updateAppSettings } from "./Features/Settings/settingsSlice";
import { logger } from "./Utils/Logger"; // Import the logger
import { networkService } from "./main";
import { setMode } from "./Features/UI/uiSlice";

const getPreferredTheme = () => {
try {
return window?.matchMedia?.('(prefers-color-scheme: dark)')?.matches? 'dark': 'light';
}
catch {
return 'light';
}
}

function App() {
const AdminCheckedRegister = withAdminCheck(Register);
const MonitorsWithAdminProp = withAdminProp(Monitors);
Expand All @@ -69,22 +58,6 @@ function App() {
}
}, [dispatch, authToken]);

useEffect(() => {
const theme = getPreferredTheme();
dispatch(setMode(theme));

const mediaQuery = window?.matchMedia?.('(prefers-color-scheme: dark)');
const handleThemeChange = (e) => {
dispatch(setMode(e.matches ? 'dark' : 'light'));
};

mediaQuery?.addEventListener?.('change', handleThemeChange);

return () => {
mediaQuery?.removeEventListener?.('change', handleThemeChange);
};
},[dispatch]);

// Cleanup
useEffect(() => {
return () => {
Expand Down
2 changes: 1 addition & 1 deletion Client/src/Features/UI/uiSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const initialState = {
sidebar: {
collapsed: false,
},
mode: "light",
mode: window?.matchMedia?.('(prefers-color-scheme: dark)')?.matches? "dark": "light",
greeting: { index: 0, lastUpdate: null },
timezone: "America/Toronto",
};
Expand Down

0 comments on commit 0c3d824

Please sign in to comment.