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

125 lines
4.9 KiB
TypeScript
Raw Normal View History

import { useState, useEffect, useCallback } 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 '../constants/nsfwTags';
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);
2024-06-22 12:36:38 +02:00
const getTheme = useThemeStore((state) => state.getTheme);
const loadThemes = useThemeStore((state) => state.loadThemes);
const subplebbits = useDefaultSubplebbits();
const initialTheme = useInitialTheme(pendingPostSubplebbitAddress);
2024-08-26 17:39:27 +02:00
const [currentTheme, setCurrentTheme] = useState(initialTheme);
2024-12-24 17:13:12 +01:00
const isInAllView = isAllView(location.pathname);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);
// Check for Christmas and initialize special theme if needed
useEffect(() => {
const isChristmasTime = isChristmas();
2024-12-24 17:13:12 +01:00
const subplebbitAddress = params?.subplebbitAddress || pendingPostSubplebbitAddress;
if (isChristmasTime && isEnabled === null && subplebbitAddress && !isInAllView && !isInSubscriptionsView) {
2024-12-24 17:13:12 +01:00
setIsEnabled(true);
setCurrentTheme('tomorrow');
updateThemeClass('tomorrow');
} else if (!isChristmasTime && isEnabled) {
setIsEnabled(false);
2024-12-24 17:13:12 +01:00
}
2024-12-24 17:15:51 +01:00
}, [isEnabled, setIsEnabled, params, pendingPostSubplebbitAddress, location.pathname, isInAllView, isInSubscriptionsView]);
2024-12-24 17:13:12 +01:00
const getCurrentTheme = useCallback(() => {
2024-12-24 17:13:12 +01:00
const { isEnabled } = useSpecialThemeStore.getState();
const subplebbitAddress = params?.subplebbitAddress || pendingPostSubplebbitAddress;
2024-10-29 17:40:09 +01:00
const isInAllView = isAllView(location.pathname);
2024-06-22 12:36:38 +02:00
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);
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) {
2024-08-26 17:39:27 +02:00
storedTheme = getTheme('sfw', false);
} else if (subplebbitAddress) {
const subplebbit = subplebbits.find((s) => s.address === subplebbitAddress);
if (subplebbit && subplebbit.tags && subplebbit.tags.some((tag) => nsfwTags.includes(tag))) {
2024-08-26 17:39:27 +02:00
storedTheme = getTheme('nsfw', false);
} else {
2024-08-26 17:39:27 +02:00
storedTheme = getTheme('sfw', false);
}
}
2024-06-22 12:36:38 +02:00
return storedTheme || initialTheme;
}, [location.pathname, params, getTheme, subplebbits, initialTheme, pendingPostSubplebbitAddress]);
useEffect(() => {
2024-08-26 17:39:27 +02:00
const newTheme = getCurrentTheme();
if (newTheme !== currentTheme) {
setCurrentTheme(newTheme);
updateThemeClass(newTheme);
}
}, [getCurrentTheme, currentTheme]);
2024-08-26 17:39:27 +02:00
useEffect(() => {
loadThemes();
}, [loadThemes]);
const setSubplebbitTheme = useCallback(
async (newTheme: string) => {
const subplebbitAddress = params?.subplebbitAddress || pendingPostSubplebbitAddress;
2024-10-29 17:40:09 +01:00
const isInAllView = isAllView(location.pathname);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);
if (isInAllView || isInSubscriptionsView) {
await setThemeStore('sfw', 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-08-26 17:39:27 +02:00
setCurrentTheme(newTheme);
updateThemeClass(newTheme);
},
[location.pathname, params, setThemeStore, subplebbits, pendingPostSubplebbitAddress],
);
return [currentTheme, setSubplebbitTheme];
2024-02-28 21:18:37 +01:00
};
export default useTheme;