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

103 lines
4.0 KiB
TypeScript
Raw Normal View History

import { useEffect, useCallback, useMemo } from 'react';
import { useLocation, useParams } from 'react-router-dom';
import { isAllView, isSubscriptionsView, isModView } 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 { useResolvedSubplebbitAddress } from './use-resolved-subplebbit-address';
import { useAccountComment } from '@plebbit/plebbit-react-hooks';
2024-12-24 17:13:12 +01:00
import useSpecialThemeStore from '../stores/use-special-theme-store';
import { isChristmas } from '../lib/utils/time-utils';
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);
}
};
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;
2024-12-24 17:13:12 +01:00
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();
2024-12-24 17:13:12 +01:00
const isInAllView = isAllView(location.pathname);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);
const isInModView = isModView(location.pathname);
const resolvedAddress = useResolvedSubplebbitAddress();
const subplebbitAddress = resolvedAddress || pendingPostSubplebbitAddress;
2024-12-24 17:13:12 +01:00
// Check for Christmas and initialize special theme if needed
useEffect(() => {
const isChristmasTime = isChristmas();
2024-12-24 17:13:12 +01:00
if (isChristmasTime && isEnabled === null && subplebbitAddress && !isInAllView && !isInSubscriptionsView && !isInModView) {
2024-12-24 17:13:12 +01:00
setIsEnabled(true);
} else if (!isChristmasTime && isEnabled) {
setIsEnabled(false);
2024-12-24 17:13:12 +01:00
}
}, [isEnabled, setIsEnabled, subplebbitAddress, isInAllView, isInSubscriptionsView, isInModView]);
2024-06-22 12:36:38 +02:00
// Calculate current theme during render - no effects needed
const currentTheme = useMemo(() => {
2024-12-24 17:13:12 +01:00
// Always use yotsuba for home page
if (location.pathname === '/') {
return 'yotsuba';
}
// If special theme is enabled, use tomorrow
if (isEnabled) {
return 'tomorrow';
}
2024-06-22 12:36:38 +02:00
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;
}
}
2024-06-22 12:36:38 +02:00
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];
2024-02-28 21:18:37 +01:00
};
export default useTheme;