2024-08-26 11:32:40 +02:00
|
|
|
import { useState, useEffect, useCallback } 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';
|
2025-02-20 15:15:19 +01:00
|
|
|
import { useDefaultSubplebbits } from './use-default-subplebbits';
|
2024-06-20 13:17:11 +02:00
|
|
|
import useInitialTheme from './use-initial-theme';
|
2025-03-04 17:53:05 +01:00
|
|
|
import { nsfwTags } from '../constants/nsfwTags';
|
2024-09-08 16:31:21 +02:00
|
|
|
import { useAccountComment } from '@plebbit/plebbit-react-hooks';
|
2024-12-24 17:13:12 +01:00
|
|
|
import useSpecialThemeStore from '../stores/use-special-theme-store';
|
2025-01-26 23:35:50 +01:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-08 16:31:21 +02: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-09-08 16:31:21 +02:00
|
|
|
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();
|
2024-09-08 16:31:21 +02:00
|
|
|
|
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-08-28 22:05:20 +02:00
|
|
|
const initialTheme = useInitialTheme(pendingPostSubplebbitAddress);
|
2024-08-26 17:39:27 +02:00
|
|
|
const [currentTheme, setCurrentTheme] = useState(initialTheme);
|
2024-06-17 12:17:36 +02:00
|
|
|
|
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(() => {
|
2025-01-26 23:35:50 +01:00
|
|
|
const isChristmasTime = isChristmas();
|
2024-12-24 17:13:12 +01:00
|
|
|
const subplebbitAddress = params?.subplebbitAddress || pendingPostSubplebbitAddress;
|
|
|
|
|
|
2025-01-26 23:35:50 +01:00
|
|
|
if (isChristmasTime && isEnabled === null && subplebbitAddress && !isInAllView && !isInSubscriptionsView) {
|
2024-12-24 17:13:12 +01:00
|
|
|
setIsEnabled(true);
|
|
|
|
|
setCurrentTheme('tomorrow');
|
|
|
|
|
updateThemeClass('tomorrow');
|
2025-01-26 23:35:50 +01:00
|
|
|
} else if (!isChristmasTime && isEnabled) {
|
2025-01-26 23:29:26 +01:00
|
|
|
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
|
|
|
|
2024-08-26 11:32:40 +02:00
|
|
|
const getCurrentTheme = useCallback(() => {
|
2024-12-24 17:13:12 +01:00
|
|
|
const { isEnabled } = useSpecialThemeStore.getState();
|
2024-09-08 16:31:21 +02:00
|
|
|
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;
|
2024-08-05 11:58:33 +02:00
|
|
|
if (isInAllView || isInSubscriptionsView) {
|
2024-08-26 17:39:27 +02:00
|
|
|
storedTheme = getTheme('sfw', false);
|
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))) {
|
2024-08-26 17:39:27 +02:00
|
|
|
storedTheme = getTheme('nsfw', false);
|
2024-06-30 21:50:50 +02:00
|
|
|
} else {
|
2024-08-26 17:39:27 +02:00
|
|
|
storedTheme = getTheme('sfw', false);
|
2024-06-30 21:50:50 +02:00
|
|
|
}
|
2024-06-19 22:55:22 +02:00
|
|
|
}
|
2024-06-22 12:36:38 +02:00
|
|
|
|
2024-08-26 11:32:40 +02:00
|
|
|
return storedTheme || initialTheme;
|
2024-09-08 16:31:21 +02:00
|
|
|
}, [location.pathname, params, getTheme, subplebbits, initialTheme, pendingPostSubplebbitAddress]);
|
2024-06-19 22:55:22 +02:00
|
|
|
|
2024-08-26 11:32:40 +02:00
|
|
|
useEffect(() => {
|
2024-08-26 17:39:27 +02:00
|
|
|
const newTheme = getCurrentTheme();
|
|
|
|
|
if (newTheme !== currentTheme) {
|
|
|
|
|
setCurrentTheme(newTheme);
|
|
|
|
|
updateThemeClass(newTheme);
|
|
|
|
|
}
|
|
|
|
|
}, [getCurrentTheme, currentTheme]);
|
2024-06-19 22:55:22 +02:00
|
|
|
|
2024-08-26 17:39:27 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
loadThemes();
|
|
|
|
|
}, [loadThemes]);
|
2024-08-26 11:32:40 +02:00
|
|
|
|
|
|
|
|
const setSubplebbitTheme = useCallback(
|
|
|
|
|
async (newTheme: string) => {
|
2024-09-08 16:31:21 +02:00
|
|
|
const subplebbitAddress = params?.subplebbitAddress || pendingPostSubplebbitAddress;
|
2024-10-29 17:40:09 +01:00
|
|
|
const isInAllView = isAllView(location.pathname);
|
2024-08-26 11:32:40 +02:00
|
|
|
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);
|
|
|
|
|
|
|
|
|
|
if (isInAllView || isInSubscriptionsView) {
|
2024-06-30 21:50:50 +02:00
|
|
|
await setThemeStore('sfw', newTheme);
|
2024-08-26 11:32:40 +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-30 21:50:50 +02:00
|
|
|
}
|
2024-06-22 12:36:38 +02:00
|
|
|
|
2024-08-26 17:39:27 +02:00
|
|
|
setCurrentTheme(newTheme);
|
2024-08-26 11:32:40 +02:00
|
|
|
updateThemeClass(newTheme);
|
|
|
|
|
},
|
2024-09-08 16:31:21 +02:00
|
|
|
[location.pathname, params, setThemeStore, subplebbits, pendingPostSubplebbitAddress],
|
2024-08-26 11:32:40 +02:00
|
|
|
);
|
2024-06-17 12:17:36 +02:00
|
|
|
|
2024-08-26 11:32:40 +02:00
|
|
|
return [currentTheme, setSubplebbitTheme];
|
2024-02-28 21:18:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default useTheme;
|