2024-06-19 22:55:22 +02:00
|
|
|
import { useState, useEffect } from 'react';
|
2024-06-17 12:17:36 +02:00
|
|
|
import { useLocation, useParams } from 'react-router-dom';
|
2024-06-19 22:55:22 +02:00
|
|
|
import { isAllView, isSubscriptionsView } from '../lib/utils/view-utils';
|
2024-05-31 20:14:32 +02:00
|
|
|
import useThemeStore from '../stores/use-theme-store';
|
2024-06-20 13:17:11 +02:00
|
|
|
import useInitialTheme from './use-initial-theme';
|
2024-02-28 21:18:37 +01:00
|
|
|
|
2024-06-22 12:36:38 +02:00
|
|
|
const updateThemeClass = (newTheme: string) => {
|
|
|
|
|
document.body.classList.remove('yotsuba', 'yotsuba-b', 'futaba', 'burichan', 'tomorrow', 'photon');
|
|
|
|
|
if (newTheme) {
|
|
|
|
|
document.body.classList.add(newTheme);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-02-28 21:18:37 +01:00
|
|
|
const useTheme = (): [string, (theme: string) => void] => {
|
2024-06-17 12:17:36 +02:00
|
|
|
const location = useLocation();
|
2024-06-17 15:39:49 +02:00
|
|
|
const params = useParams();
|
2024-06-19 22:55:22 +02:00
|
|
|
const setThemeStore = useThemeStore((state) => state.setTheme);
|
2024-06-22 12:36:38 +02:00
|
|
|
const getTheme = useThemeStore((state) => state.getTheme);
|
2024-06-19 22:55:22 +02:00
|
|
|
const loadThemes = useThemeStore((state) => state.loadThemes);
|
2024-06-17 12:17:36 +02:00
|
|
|
|
2024-06-22 12:36:38 +02:00
|
|
|
const initialTheme = useInitialTheme();
|
|
|
|
|
const [theme, setLocalTheme] = useState<string>(() => initialTheme);
|
2024-06-19 22:55:22 +02:00
|
|
|
const [themesLoaded, setThemesLoaded] = useState(false);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-06-22 12:36:38 +02:00
|
|
|
const loadAndApplyThemes = async () => {
|
2024-06-19 22:55:22 +02:00
|
|
|
await loadThemes();
|
|
|
|
|
setThemesLoaded(true);
|
|
|
|
|
};
|
2024-06-22 12:36:38 +02:00
|
|
|
|
|
|
|
|
loadAndApplyThemes();
|
2024-06-19 22:55:22 +02:00
|
|
|
}, [loadThemes]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-06-22 12:36:38 +02:00
|
|
|
if (!themesLoaded) return;
|
2024-06-17 12:17:36 +02:00
|
|
|
|
2024-06-22 12:36:38 +02:00
|
|
|
const subplebbitAddress = params?.subplebbitAddress;
|
|
|
|
|
const isInAllView = isAllView(location.pathname, params);
|
|
|
|
|
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);
|
|
|
|
|
|
|
|
|
|
let storedTheme = null;
|
|
|
|
|
if (subplebbitAddress) {
|
|
|
|
|
storedTheme = getTheme(subplebbitAddress);
|
|
|
|
|
} else if (isInAllView) {
|
|
|
|
|
storedTheme = getTheme('all');
|
|
|
|
|
} else if (isInSubscriptionsView) {
|
|
|
|
|
storedTheme = getTheme('subscriptions');
|
2024-06-19 22:55:22 +02:00
|
|
|
}
|
2024-06-22 12:36:38 +02:00
|
|
|
|
|
|
|
|
const themeToSet = storedTheme || initialTheme;
|
|
|
|
|
setLocalTheme(themeToSet);
|
|
|
|
|
updateThemeClass(themeToSet);
|
|
|
|
|
}, [initialTheme, location.pathname, params, getTheme, themesLoaded]);
|
2024-06-19 22:55:22 +02:00
|
|
|
|
2024-06-17 15:39:49 +02:00
|
|
|
const setSubplebbitTheme = (newTheme: string) => {
|
2024-06-19 22:55:22 +02:00
|
|
|
const subplebbitAddress = params?.subplebbitAddress;
|
|
|
|
|
const isInAllView = isAllView(location.pathname, params);
|
|
|
|
|
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);
|
|
|
|
|
|
2024-06-17 12:17:36 +02:00
|
|
|
if (subplebbitAddress) {
|
2024-06-17 15:39:49 +02:00
|
|
|
setThemeStore(subplebbitAddress, newTheme);
|
|
|
|
|
} else if (isInAllView) {
|
|
|
|
|
setThemeStore('all', newTheme);
|
|
|
|
|
} else if (isInSubscriptionsView) {
|
|
|
|
|
setThemeStore('subscriptions', newTheme);
|
2024-06-17 12:17:36 +02:00
|
|
|
}
|
2024-06-22 12:36:38 +02:00
|
|
|
|
2024-06-17 15:39:49 +02:00
|
|
|
setLocalTheme(newTheme);
|
2024-06-22 12:36:38 +02:00
|
|
|
updateThemeClass(newTheme);
|
2024-06-17 12:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return [theme, setSubplebbitTheme];
|
2024-02-28 21:18:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default useTheme;
|