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

101 lines
3.9 KiB
TypeScript

import { useEffect, useCallback, useMemo } from 'react';
import { useLocation, useParams } from 'react-router-dom';
import { isAllView, isSubscriptionsView, isModView } from '../lib/utils/view-utils';
import useThemeStore from '../stores/use-theme-store';
import { useDefaultSubplebbits } from './use-default-subplebbits';
import { useAccountComment } from '@plebbit/plebbit-react-hooks';
import useSpecialThemeStore from '../stores/use-special-theme-store';
import { isChristmas } from '../lib/utils/time-utils';
const themeClasses = ['yotsuba', 'yotsuba-b', 'futaba', 'burichan', 'tomorrow', 'photon'];
const updateThemeClass = (newTheme: string) => {
document.body.classList.remove(...themeClasses);
if (newTheme) {
document.body.classList.add(newTheme);
}
};
const useTheme = (): [string, (theme: string) => void] => {
const location = useLocation();
const params = useParams<{ subplebbitAddress: string }>();
const pendingPostParams = useParams<{ accountCommentIndex?: string }>();
const pendingPostCommentIndex = pendingPostParams?.accountCommentIndex ? parseInt(pendingPostParams.accountCommentIndex) : undefined;
const pendingPost = useAccountComment({ commentIndex: pendingPostCommentIndex });
const pendingPostSubplebbitAddress = pendingPost?.subplebbitAddress;
const { isEnabled, setIsEnabled } = useSpecialThemeStore();
const setThemeStore = useThemeStore((state) => state.setTheme);
// Subscribe to the actual themes data, not just the getter function
const themes = useThemeStore((state) => state.themes);
const subplebbits = useDefaultSubplebbits();
const isInAllView = isAllView(location.pathname);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);
const isInModView = isModView(location.pathname);
const subplebbitAddress = params?.subplebbitAddress || pendingPostSubplebbitAddress;
// Check for Christmas and initialize special theme if needed
useEffect(() => {
const isChristmasTime = isChristmas();
if (isChristmasTime && isEnabled === null && subplebbitAddress && !isInAllView && !isInSubscriptionsView && !isInModView) {
setIsEnabled(true);
} else if (!isChristmasTime && isEnabled) {
setIsEnabled(false);
}
}, [isEnabled, setIsEnabled, subplebbitAddress, isInAllView, isInSubscriptionsView, isInModView]);
// Calculate current theme during render - no effects needed
const currentTheme = useMemo(() => {
// Always use yotsuba for home page
if (location.pathname === '/') {
return 'yotsuba';
}
// If special theme is enabled, use tomorrow
if (isEnabled) {
return 'tomorrow';
}
let storedTheme = null;
if (isInAllView || isInSubscriptionsView || isInModView) {
storedTheme = themes.sfw;
} else if (subplebbitAddress) {
const subplebbit = subplebbits.find((s) => s.address === subplebbitAddress);
if (subplebbit?.nsfw) {
storedTheme = themes.nsfw;
} else {
storedTheme = themes.sfw;
}
}
return storedTheme || 'yotsuba';
}, [location.pathname, isEnabled, isInAllView, isInSubscriptionsView, isInModView, subplebbitAddress, subplebbits, themes]);
// Update DOM class when theme changes
useEffect(() => {
updateThemeClass(currentTheme);
}, [currentTheme]);
const setSubplebbitTheme = useCallback(
async (newTheme: string) => {
if (isInAllView || isInSubscriptionsView || isInModView) {
await setThemeStore('sfw', newTheme);
} else if (subplebbitAddress) {
const subplebbit = subplebbits.find((s) => s.address === subplebbitAddress);
if (subplebbit?.nsfw) {
await setThemeStore('nsfw', newTheme);
} else {
await setThemeStore('sfw', newTheme);
}
}
},
[isInAllView, isInSubscriptionsView, isInModView, subplebbitAddress, subplebbits, setThemeStore],
);
return [currentTheme, setSubplebbitTheme];
};
export default useTheme;