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-30 21:50:50 +02:00
|
|
|
import useDefaultSubplebbits from './use-default-subplebbits';
|
2024-06-20 13:17:11 +02:00
|
|
|
import useInitialTheme from './use-initial-theme';
|
2024-06-30 21:50:50 +02:00
|
|
|
import { nsfwTags } from '../views/home/home';
|
2024-02-28 21:18:37 +01:00
|
|
|
|
2024-06-22 12:43:37 +02:00
|
|
|
const themeClasses = ['yotsuba', 'yotsuba-b', 'futaba', 'burichan', 'tomorrow', 'photon'];
|
|
|
|
|
|
2024-06-22 12:36:38 +02:00
|
|
|
const updateThemeClass = (newTheme: string) => {
|
2024-06-22 12:43:37 +02:00
|
|
|
document.body.classList.remove(...themeClasses);
|
2024-06-22 12:36:38 +02:00
|
|
|
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-30 21:50:50 +02:00
|
|
|
const params = useParams<{ subplebbitAddress: string }>();
|
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-30 21:50:50 +02:00
|
|
|
const subplebbits = useDefaultSubplebbits();
|
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;
|
2024-06-30 21:50:50 +02:00
|
|
|
if (isInAllView) {
|
2024-06-22 12:36:38 +02:00
|
|
|
storedTheme = getTheme('all');
|
|
|
|
|
} else if (isInSubscriptionsView) {
|
|
|
|
|
storedTheme = getTheme('subscriptions');
|
2024-06-30 21:50:50 +02:00
|
|
|
} 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');
|
|
|
|
|
} else {
|
|
|
|
|
storedTheme = getTheme('sfw');
|
|
|
|
|
}
|
2024-06-19 22:55:22 +02:00
|
|
|
}
|
2024-06-22 12:36:38 +02:00
|
|
|
|
|
|
|
|
const themeToSet = storedTheme || initialTheme;
|
|
|
|
|
setLocalTheme(themeToSet);
|
|
|
|
|
updateThemeClass(themeToSet);
|
2024-06-30 21:50:50 +02:00
|
|
|
}, [initialTheme, location.pathname, params, getTheme, themesLoaded, subplebbits]);
|
2024-06-19 22:55:22 +02:00
|
|
|
|
2024-06-22 12:43:37 +02:00
|
|
|
const setSubplebbitTheme = async (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-30 21:50:50 +02:00
|
|
|
if (isInAllView) {
|
2024-06-22 12:43:37 +02:00
|
|
|
await setThemeStore('all', newTheme);
|
2024-06-17 15:39:49 +02:00
|
|
|
} else if (isInSubscriptionsView) {
|
2024-06-22 12:43:37 +02:00
|
|
|
await setThemeStore('subscriptions', newTheme);
|
2024-06-30 21:50:50 +02:00
|
|
|
} else if (subplebbitAddress) {
|
|
|
|
|
const subplebbit = subplebbits.find((s) => s.address === subplebbitAddress);
|
|
|
|
|
if (subplebbit && subplebbit.tags && subplebbit.tags.some((tag) => nsfwTags.includes(tag))) {
|
|
|
|
|
await setThemeStore('nsfw', newTheme);
|
|
|
|
|
} else {
|
|
|
|
|
await setThemeStore('sfw', 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;
|