mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix theme warning
This commit is contained in:
+15
-6
@@ -1,4 +1,4 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { Outlet, Route, Routes, useLocation, useParams } from 'react-router-dom';
|
||||
import { isAllView, isSubscriptionsView } from './lib/utils/view-utils';
|
||||
import useIsMobile from './hooks/use-is-mobile';
|
||||
@@ -61,13 +61,22 @@ const BoardLayout = () => {
|
||||
};
|
||||
|
||||
const GlobalLayout = () => {
|
||||
const [theme] = useTheme();
|
||||
const [theme, setTheme] = useState('');
|
||||
const [currentTheme] = useTheme();
|
||||
|
||||
useEffect(() => {
|
||||
document.body.classList.add(theme);
|
||||
return () => {
|
||||
document.body.classList.remove(theme);
|
||||
};
|
||||
if (currentTheme !== theme) {
|
||||
setTheme(currentTheme);
|
||||
}
|
||||
}, [currentTheme, theme]);
|
||||
|
||||
useEffect(() => {
|
||||
if (theme) {
|
||||
document.body.classList.add(theme);
|
||||
return () => {
|
||||
document.body.classList.remove(theme);
|
||||
};
|
||||
}
|
||||
}, [theme]);
|
||||
|
||||
return (
|
||||
|
||||
@@ -24,15 +24,15 @@ const useInitialTheme = () => {
|
||||
if (isInPendingPostView) {
|
||||
theme = currentTheme || 'yotsuba';
|
||||
} else if (isInAllView || isInSubscriptionsView) {
|
||||
theme = getTheme('sfw') || 'yotsuba-b';
|
||||
theme = getTheme('sfw', false) || 'yotsuba-b'; // Add 'false' parameter
|
||||
} else if (isInHomeView || isInNotFoundView) {
|
||||
theme = 'yotsuba';
|
||||
} else if (subplebbitAddress) {
|
||||
const subplebbit = subplebbits.find((s) => s.address === subplebbitAddress);
|
||||
if (subplebbit && subplebbit.tags && subplebbit.tags.some((tag) => nsfwTags.includes(tag))) {
|
||||
theme = getTheme('nsfw') || 'yotsuba';
|
||||
theme = getTheme('nsfw', false) || 'yotsuba'; // Add 'false' parameter
|
||||
} else {
|
||||
theme = getTheme('sfw') || 'yotsuba-b';
|
||||
theme = getTheme('sfw', false) || 'yotsuba-b'; // Add 'false' parameter
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+14
-14
@@ -24,7 +24,7 @@ const useTheme = (): [string, (theme: string) => void] => {
|
||||
const subplebbits = useDefaultSubplebbits();
|
||||
|
||||
const initialTheme = useInitialTheme();
|
||||
const [userSetTheme, setUserSetTheme] = useState<string | null>(null);
|
||||
const [currentTheme, setCurrentTheme] = useState(initialTheme);
|
||||
|
||||
const getCurrentTheme = useCallback(() => {
|
||||
const subplebbitAddress = params?.subplebbitAddress;
|
||||
@@ -33,13 +33,13 @@ const useTheme = (): [string, (theme: string) => void] => {
|
||||
|
||||
let storedTheme = null;
|
||||
if (isInAllView || isInSubscriptionsView) {
|
||||
storedTheme = getTheme('sfw');
|
||||
storedTheme = getTheme('sfw', false);
|
||||
} else if (subplebbitAddress) {
|
||||
const subplebbit = subplebbits.find((s) => s.address === subplebbitAddress);
|
||||
if (subplebbit && subplebbit.tags && subplebbit.tags.some((tag) => nsfwTags.includes(tag))) {
|
||||
storedTheme = getTheme('nsfw');
|
||||
storedTheme = getTheme('nsfw', false);
|
||||
} else {
|
||||
storedTheme = getTheme('sfw');
|
||||
storedTheme = getTheme('sfw', false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,14 +47,16 @@ const useTheme = (): [string, (theme: string) => void] => {
|
||||
}, [location.pathname, params, getTheme, subplebbits, initialTheme]);
|
||||
|
||||
useEffect(() => {
|
||||
const initializeTheme = async () => {
|
||||
await loadThemes();
|
||||
const currentTheme = getCurrentTheme();
|
||||
updateThemeClass(currentTheme);
|
||||
};
|
||||
const newTheme = getCurrentTheme();
|
||||
if (newTheme !== currentTheme) {
|
||||
setCurrentTheme(newTheme);
|
||||
updateThemeClass(newTheme);
|
||||
}
|
||||
}, [getCurrentTheme, currentTheme]);
|
||||
|
||||
initializeTheme();
|
||||
}, [loadThemes, getCurrentTheme]);
|
||||
useEffect(() => {
|
||||
loadThemes();
|
||||
}, [loadThemes]);
|
||||
|
||||
const setSubplebbitTheme = useCallback(
|
||||
async (newTheme: string) => {
|
||||
@@ -73,14 +75,12 @@ const useTheme = (): [string, (theme: string) => void] => {
|
||||
}
|
||||
}
|
||||
|
||||
setUserSetTheme(newTheme);
|
||||
setCurrentTheme(newTheme);
|
||||
updateThemeClass(newTheme);
|
||||
},
|
||||
[location.pathname, params, setThemeStore, subplebbits],
|
||||
);
|
||||
|
||||
const currentTheme = userSetTheme || getCurrentTheme();
|
||||
|
||||
return [currentTheme, setSubplebbitTheme];
|
||||
};
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ interface ThemeState {
|
||||
};
|
||||
currentTheme: string | null;
|
||||
setTheme: (category: keyof ThemeState['themes'], theme: string) => void;
|
||||
getTheme: (category: keyof ThemeState['themes']) => string | null;
|
||||
getTheme: (category: keyof ThemeState['themes'], updateCurrentTheme?: boolean) => string | null;
|
||||
loadThemes: () => Promise<void>;
|
||||
}
|
||||
|
||||
@@ -29,10 +29,12 @@ const useThemeStore = create<ThemeState>((set: StoreApi<ThemeState>['setState'],
|
||||
await themeStore.setItem(category, theme);
|
||||
set({ themes: updatedThemes, currentTheme: theme });
|
||||
},
|
||||
getTheme: (category) => {
|
||||
getTheme: (category, updateCurrentTheme = true) => {
|
||||
const currentThemes = get().themes;
|
||||
const theme = currentThemes[category] || null;
|
||||
set({ currentTheme: theme });
|
||||
if (updateCurrentTheme) {
|
||||
set({ currentTheme: theme });
|
||||
}
|
||||
return theme;
|
||||
},
|
||||
loadThemes: async () => {
|
||||
|
||||
Reference in New Issue
Block a user