Files
5chan/src/hooks/use-theme.ts
T

91 lines
3.2 KiB
TypeScript
Raw Normal View History

import { useState, useEffect } from 'react';
import { useLocation, useParams } from 'react-router-dom';
import { isAllView, isSubscriptionsView } from '../lib/utils/view-utils';
2024-05-31 20:14:32 +02:00
import useThemeStore from '../stores/use-theme-store';
import useDefaultSubplebbits from './use-default-subplebbits';
import useInitialTheme from './use-initial-theme';
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] => {
const location = useLocation();
const params = useParams<{ subplebbitAddress: string }>();
const setThemeStore = useThemeStore((state) => state.setTheme);
2024-06-22 12:36:38 +02:00
const getTheme = useThemeStore((state) => state.getTheme);
const loadThemes = useThemeStore((state) => state.loadThemes);
const subplebbits = useDefaultSubplebbits();
2024-06-22 12:36:38 +02:00
const initialTheme = useInitialTheme();
const [theme, setLocalTheme] = useState<string>(() => initialTheme);
const [themesLoaded, setThemesLoaded] = useState(false);
useEffect(() => {
2024-06-22 12:36:38 +02:00
const loadAndApplyThemes = async () => {
await loadThemes();
setThemesLoaded(true);
};
2024-06-22 12:36:38 +02:00
loadAndApplyThemes();
}, [loadThemes]);
useEffect(() => {
2024-06-22 12:36:38 +02:00
if (!themesLoaded) return;
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 (isInAllView) {
2024-06-22 12:36:38 +02:00
storedTheme = getTheme('all');
} else if (isInSubscriptionsView) {
storedTheme = getTheme('subscriptions');
} 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-22 12:36:38 +02:00
const themeToSet = storedTheme || initialTheme;
setLocalTheme(themeToSet);
updateThemeClass(themeToSet);
}, [initialTheme, location.pathname, params, getTheme, themesLoaded, subplebbits]);
2024-06-22 12:43:37 +02:00
const setSubplebbitTheme = async (newTheme: string) => {
const subplebbitAddress = params?.subplebbitAddress;
const isInAllView = isAllView(location.pathname, params);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);
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);
} 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-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);
};
return [theme, setSubplebbitTheme];
2024-02-28 21:18:37 +01:00
};
export default useTheme;