mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix(themes): changing theme would bug out
This commit is contained in:
+32
-23
@@ -4,44 +4,53 @@ import { isAllView, isSubscriptionsView } from '../lib/utils/view-utils';
|
|||||||
import useThemeStore from '../stores/use-theme-store';
|
import useThemeStore from '../stores/use-theme-store';
|
||||||
import useInitialTheme from './use-initial-theme';
|
import useInitialTheme from './use-initial-theme';
|
||||||
|
|
||||||
|
const updateThemeClass = (newTheme: string) => {
|
||||||
|
document.body.classList.remove('yotsuba', 'yotsuba-b', 'futaba', 'burichan', 'tomorrow', 'photon');
|
||||||
|
if (newTheme) {
|
||||||
|
document.body.classList.add(newTheme);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const useTheme = (): [string, (theme: string) => void] => {
|
const useTheme = (): [string, (theme: string) => void] => {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const params = useParams();
|
const params = useParams();
|
||||||
const setThemeStore = useThemeStore((state) => state.setTheme);
|
const setThemeStore = useThemeStore((state) => state.setTheme);
|
||||||
|
const getTheme = useThemeStore((state) => state.getTheme);
|
||||||
const loadThemes = useThemeStore((state) => state.loadThemes);
|
const loadThemes = useThemeStore((state) => state.loadThemes);
|
||||||
|
|
||||||
|
const initialTheme = useInitialTheme();
|
||||||
|
const [theme, setLocalTheme] = useState<string>(() => initialTheme);
|
||||||
const [themesLoaded, setThemesLoaded] = useState(false);
|
const [themesLoaded, setThemesLoaded] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const load = async () => {
|
const loadAndApplyThemes = async () => {
|
||||||
await loadThemes();
|
await loadThemes();
|
||||||
setThemesLoaded(true);
|
setThemesLoaded(true);
|
||||||
};
|
};
|
||||||
load();
|
|
||||||
|
loadAndApplyThemes();
|
||||||
}, [loadThemes]);
|
}, [loadThemes]);
|
||||||
|
|
||||||
const initialTheme = useInitialTheme();
|
|
||||||
|
|
||||||
const [theme, setLocalTheme] = useState(initialTheme);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const previousTheme = document.body.className;
|
if (!themesLoaded) return;
|
||||||
document.body.classList.add(initialTheme);
|
|
||||||
return () => {
|
|
||||||
document.body.classList.remove(initialTheme);
|
|
||||||
if (previousTheme) {
|
|
||||||
document.body.classList.add(previousTheme);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}, [initialTheme]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
const subplebbitAddress = params?.subplebbitAddress;
|
||||||
if (themesLoaded) {
|
const isInAllView = isAllView(location.pathname, params);
|
||||||
document.body.classList.remove('yotsuba', 'yotsuba-b', 'futaba', 'burichan', 'tomorrow', 'photon');
|
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);
|
||||||
document.body.classList.add(initialTheme);
|
|
||||||
setLocalTheme(initialTheme);
|
let storedTheme = null;
|
||||||
|
if (subplebbitAddress) {
|
||||||
|
storedTheme = getTheme(subplebbitAddress);
|
||||||
|
} else if (isInAllView) {
|
||||||
|
storedTheme = getTheme('all');
|
||||||
|
} else if (isInSubscriptionsView) {
|
||||||
|
storedTheme = getTheme('subscriptions');
|
||||||
}
|
}
|
||||||
}, [initialTheme, themesLoaded]);
|
|
||||||
|
const themeToSet = storedTheme || initialTheme;
|
||||||
|
setLocalTheme(themeToSet);
|
||||||
|
updateThemeClass(themeToSet);
|
||||||
|
}, [initialTheme, location.pathname, params, getTheme, themesLoaded]);
|
||||||
|
|
||||||
const setSubplebbitTheme = (newTheme: string) => {
|
const setSubplebbitTheme = (newTheme: string) => {
|
||||||
const subplebbitAddress = params?.subplebbitAddress;
|
const subplebbitAddress = params?.subplebbitAddress;
|
||||||
@@ -55,9 +64,9 @@ const useTheme = (): [string, (theme: string) => void] => {
|
|||||||
} else if (isInSubscriptionsView) {
|
} else if (isInSubscriptionsView) {
|
||||||
setThemeStore('subscriptions', newTheme);
|
setThemeStore('subscriptions', newTheme);
|
||||||
}
|
}
|
||||||
|
|
||||||
setLocalTheme(newTheme);
|
setLocalTheme(newTheme);
|
||||||
document.body.classList.remove('yotsuba', 'yotsuba-b', 'futaba', 'burichan', 'tomorrow', 'photon');
|
updateThemeClass(newTheme);
|
||||||
document.body.classList.add(newTheme);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return [theme, setSubplebbitTheme];
|
return [theme, setSubplebbitTheme];
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ interface ThemeState {
|
|||||||
themes: Record<string, string>;
|
themes: Record<string, string>;
|
||||||
setTheme: (subplebbitAddress: string, theme: string) => void;
|
setTheme: (subplebbitAddress: string, theme: string) => void;
|
||||||
getTheme: (subplebbitAddress: string) => string | null;
|
getTheme: (subplebbitAddress: string) => string | null;
|
||||||
loadThemes: () => void;
|
loadThemes: () => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const themeStore = localForageLru.createInstance({
|
const themeStore = localForageLru.createInstance({
|
||||||
@@ -35,6 +35,7 @@ const useThemeStore = create<ThemeState>((set: StoreApi<ThemeState>['setState'],
|
|||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
// Load themes on store initialization
|
||||||
useThemeStore.getState().loadThemes();
|
useThemeStore.getState().loadThemes();
|
||||||
|
|
||||||
export default useThemeStore;
|
export default useThemeStore;
|
||||||
|
|||||||
Reference in New Issue
Block a user