-
Notifications
You must be signed in to change notification settings - Fork 209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added User theme(Light/Dark) preference detection #1072
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -39,6 +39,14 @@ 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 = () => { | ||||||||||||||||||||||||||||||||||||||||
if(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { | ||||||||||||||||||||||||||||||||||||||||
return 'dark'; | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
return 'light'; | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
function App() { | ||||||||||||||||||||||||||||||||||||||||
const AdminCheckedRegister = withAdminCheck(Register); | ||||||||||||||||||||||||||||||||||||||||
const MonitorsWithAdminProp = withAdminProp(Monitors); | ||||||||||||||||||||||||||||||||||||||||
|
@@ -58,6 +66,11 @@ function App() { | |||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
}, [dispatch, authToken]); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
useEffect(() => { | ||||||||||||||||||||||||||||||||||||||||
const theme = getPreferredTheme(); | ||||||||||||||||||||||||||||||||||||||||
dispatch(setMode(theme)); | ||||||||||||||||||||||||||||||||||||||||
},[dispatch]); | ||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Yo, this effect needs some real-time magic! 🎵 The useEffect hook only sets the initial theme but doesn't react to system theme changes. Here's how to make it responsive: 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]); This will keep your app's theme in sync with system preferences in real-time! 🚀 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
// Cleanup | ||||||||||||||||||||||||||||||||||||||||
useEffect(() => { | ||||||||||||||||||||||||||||||||||||||||
return () => { | ||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Yo dawg, let's make this function bulletproof! 🔥
The theme detection logic needs some defensive programming:
Here's a more robust implementation:
Also consider:
addListener
oraddEventListener
📝 Committable suggestion
🧰 Tools
🪛 Biome