Skip to content

Commit

Permalink
Fixes the code to load the theme from preferences (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasdotcom authored Oct 25, 2023
1 parent 22afedc commit 8844cb8
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,15 @@ function MyApp({ Component, pageProps }: AppProps) {
// Used to create the theme for the website and starts of in dark to not blind dark theme users
const [colorMode, setColorMode] = useState<"light" | "dark" | string>("dark");
function updateColorMode(theme: "light" | "dark" | string, force = false) {
if (force || (session && session?.user.theme !== theme)) {
if (force) {
fetch("/api/user", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ theme }),
});
localStorage.theme = theme;
}
if (colorMode !== theme) {
setColorMode(theme);
Expand All @@ -210,12 +211,9 @@ function MyApp({ Component, pageProps }: AppProps) {
// Sets the color scheme based on preferences)
useEffect(() => {
// Grabs the online preferences if they exist
if (
session &&
(session.user.theme === "dark" || session.user.theme === "light")
) {
if (session && session.user.theme !== "" && session.user.theme) {
updateColorMode(session.user.theme);
} else if (localStorage.theme !== "") {
} else if (localStorage.theme !== "" && localStorage.theme) {
// Uses localstorage if available
updateColorMode(localStorage.theme);
} else {
Expand Down

0 comments on commit 8844cb8

Please sign in to comment.