2025-10-23 21:44:43 +02:00
|
|
|
import { useEffect, useCallback, useMemo } from 'react';
|
2024-06-17 12:17:36 +02:00
|
|
|
import { useLocation, useParams } from 'react-router-dom';
|
2025-03-05 17:38:34 +01:00
|
|
|
import { isAllView, isSubscriptionsView, isModView } 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';
|
2025-11-07 12:50:58 +01:00
|
|
|
import { useResolvedSubplebbitAddress } from './use-resolved-subplebbit-address';
|
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);
|
2025-10-23 21:44:43 +02:00
|
|
|
// Subscribe to the actual themes data, not just the getter function
|
|
|
|
|
const themes = useThemeStore((state) => state.themes);
|
2024-06-30 21:50:50 +02:00
|
|
|
const subplebbits = useDefaultSubplebbits();
|
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);
|
2025-03-05 17:38:34 +01:00
|
|
|
const isInModView = isModView(location.pathname);
|
2025-11-07 12:50:58 +01:00
|
|
|
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(() => {
|
2025-01-26 23:35:50 +01:00
|
|
|
const isChristmasTime = isChristmas();
|
2024-12-24 17:13:12 +01:00
|
|
|
|
2025-03-05 17:38:34 +01:00
|
|
|
if (isChristmasTime && isEnabled === null && subplebbitAddress && !isInAllView && !isInSubscriptionsView && !isInModView) {
|
2024-12-24 17:13:12 +01:00
|
|
|
setIsEnabled(true);
|
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
|
|
|
}
|
2025-10-23 21:44:43 +02:00
|
|
|
}, [isEnabled, setIsEnabled, subplebbitAddress, isInAllView, isInSubscriptionsView, isInModView]);
|
2024-06-22 12:36:38 +02:00
|
|
|
|
2025-10-23 21:44:43 +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;
|
2025-03-05 17:38:34 +01:00
|
|
|
if (isInAllView || isInSubscriptionsView || isInModView) {
|
2025-10-23 21:44:43 +02:00
|
|
|
storedTheme = themes.sfw;
|
2024-06-30 21:50:50 +02:00
|
|
|
} else if (subplebbitAddress) {
|
|
|
|
|
const subplebbit = subplebbits.find((s) => s.address === subplebbitAddress);
|
2025-11-03 22:06:18 +01:00
|
|
|
if (subplebbit?.nsfw) {
|
2025-10-23 21:44:43 +02:00
|
|
|
storedTheme = themes.nsfw;
|
2024-06-30 21:50:50 +02:00
|
|
|
} else {
|
2025-10-23 21:44:43 +02:00
|
|
|
storedTheme = themes.sfw;
|
2024-06-30 21:50:50 +02:00
|
|
|
}
|
2024-06-19 22:55:22 +02:00
|
|
|
}
|
2024-06-22 12:36:38 +02:00
|
|
|
|
2025-10-23 21:44:43 +02:00
|
|
|
return storedTheme || 'yotsuba';
|
|
|
|
|
}, [location.pathname, isEnabled, isInAllView, isInSubscriptionsView, isInModView, subplebbitAddress, subplebbits, themes]);
|
2024-06-19 22:55:22 +02:00
|
|
|
|
2025-10-23 21:44:43 +02:00
|
|
|
// Update DOM class when theme changes
|
2024-08-26 11:32:40 +02:00
|
|
|
useEffect(() => {
|
2025-10-23 21:44:43 +02:00
|
|
|
updateThemeClass(currentTheme);
|
|
|
|
|
}, [currentTheme]);
|
2024-08-26 11:32:40 +02:00
|
|
|
|
|
|
|
|
const setSubplebbitTheme = useCallback(
|
|
|
|
|
async (newTheme: string) => {
|
2025-03-05 17:38:34 +01:00
|
|
|
if (isInAllView || isInSubscriptionsView || isInModView) {
|
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);
|
2025-11-03 22:06:18 +01:00
|
|
|
if (subplebbit?.nsfw) {
|
2024-08-26 11:32:40 +02:00
|
|
|
await setThemeStore('nsfw', newTheme);
|
|
|
|
|
} else {
|
|
|
|
|
await setThemeStore('sfw', newTheme);
|
|
|
|
|
}
|
2024-06-30 21:50:50 +02:00
|
|
|
}
|
2024-08-26 11:32:40 +02:00
|
|
|
},
|
2025-10-23 21:44:43 +02:00
|
|
|
[isInAllView, isInSubscriptionsView, isInModView, subplebbitAddress, subplebbits, setThemeStore],
|
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;
|