2024-08-26 12:31:15 +02:00
|
|
|
import { useMemo } from 'react';
|
2024-06-19 22:55:22 +02:00
|
|
|
import { useLocation, useParams } from 'react-router-dom';
|
|
|
|
|
import useThemeStore from '../stores/use-theme-store';
|
2025-02-20 15:15:19 +01:00
|
|
|
import { useDefaultSubplebbits } from './use-default-subplebbits';
|
2025-03-05 17:38:34 +01:00
|
|
|
import { isAllView, isHomeView, isNotFoundView, isPendingPostView, isSubscriptionsView, isModView } from '../lib/utils/view-utils';
|
2024-08-28 22:05:20 +02:00
|
|
|
import { useAccountComment } from '@plebbit/plebbit-react-hooks';
|
2024-06-19 22:55:22 +02:00
|
|
|
|
2024-08-28 22:05:20 +02:00
|
|
|
const useInitialTheme = (pendingPostSubplebbitAddress?: string) => {
|
2024-06-19 22:55:22 +02:00
|
|
|
const location = useLocation();
|
2024-08-28 22:05:20 +02:00
|
|
|
const { subplebbitAddress: paramsSubplebbitAddress, accountCommentIndex } = useParams<{ subplebbitAddress: string; accountCommentIndex?: string }>();
|
|
|
|
|
const commentIndex = accountCommentIndex ? parseInt(accountCommentIndex) : undefined;
|
|
|
|
|
const pendingPost = useAccountComment({ commentIndex });
|
2025-10-23 21:44:43 +02:00
|
|
|
// Subscribe to the actual themes data, not just functions
|
|
|
|
|
const themes = useThemeStore((state) => state.themes);
|
2024-06-19 22:55:22 +02:00
|
|
|
const subplebbits = useDefaultSubplebbits();
|
|
|
|
|
const params = useParams();
|
|
|
|
|
const isInHomeView = isHomeView(location.pathname);
|
|
|
|
|
const isInNotFoundView = isNotFoundView(location.pathname, params);
|
2024-10-29 17:40:09 +01:00
|
|
|
const isInAllView = isAllView(location.pathname);
|
2024-06-19 22:55:22 +02:00
|
|
|
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);
|
2025-03-05 17:38:34 +01:00
|
|
|
const isInModView = isModView(location.pathname);
|
2024-07-10 12:36:44 +02:00
|
|
|
const isInPendingPostView = isPendingPostView(location.pathname, params);
|
2024-06-19 22:55:22 +02:00
|
|
|
|
2024-08-26 12:31:15 +02:00
|
|
|
const initialTheme = useMemo(() => {
|
2024-08-26 11:32:40 +02:00
|
|
|
let theme = 'yotsuba';
|
|
|
|
|
|
|
|
|
|
if (isInPendingPostView) {
|
2024-08-28 22:05:20 +02:00
|
|
|
const subplebbitAddress = pendingPostSubplebbitAddress || pendingPost?.subplebbitAddress;
|
|
|
|
|
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
|
|
|
theme = themes.nsfw || 'yotsuba';
|
2024-08-28 22:05:20 +02:00
|
|
|
} else {
|
2025-10-23 21:44:43 +02:00
|
|
|
theme = themes.sfw || 'yotsuba-b';
|
2024-08-28 22:05:20 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
2025-10-23 21:44:43 +02:00
|
|
|
theme = 'yotsuba';
|
2024-08-28 22:05:20 +02:00
|
|
|
}
|
2025-03-05 17:38:34 +01:00
|
|
|
} else if (isInAllView || isInSubscriptionsView || isInModView) {
|
2025-10-23 21:44:43 +02:00
|
|
|
theme = themes.sfw || 'yotsuba-b';
|
2024-08-26 11:32:40 +02:00
|
|
|
} else if (isInHomeView || isInNotFoundView) {
|
|
|
|
|
theme = 'yotsuba';
|
2024-08-28 22:05:20 +02:00
|
|
|
} else if (paramsSubplebbitAddress) {
|
|
|
|
|
const subplebbit = subplebbits.find((s) => s.address === paramsSubplebbitAddress);
|
2025-11-03 22:06:18 +01:00
|
|
|
if (subplebbit?.nsfw) {
|
2025-10-23 21:44:43 +02:00
|
|
|
theme = themes.nsfw || 'yotsuba';
|
2024-08-26 11:32:40 +02:00
|
|
|
} else {
|
2025-10-23 21:44:43 +02:00
|
|
|
theme = themes.sfw || 'yotsuba-b';
|
2024-08-26 11:32:40 +02:00
|
|
|
}
|
2024-06-19 22:55:22 +02:00
|
|
|
}
|
2024-08-26 11:32:40 +02:00
|
|
|
|
2024-08-26 12:31:15 +02:00
|
|
|
return theme;
|
2024-08-28 22:05:20 +02:00
|
|
|
}, [
|
|
|
|
|
isInPendingPostView,
|
|
|
|
|
isInAllView,
|
|
|
|
|
isInSubscriptionsView,
|
2025-03-05 17:38:34 +01:00
|
|
|
isInModView,
|
2024-08-28 22:05:20 +02:00
|
|
|
isInHomeView,
|
|
|
|
|
isInNotFoundView,
|
|
|
|
|
paramsSubplebbitAddress,
|
2025-10-23 21:44:43 +02:00
|
|
|
themes,
|
2024-08-28 22:05:20 +02:00
|
|
|
subplebbits,
|
|
|
|
|
pendingPostSubplebbitAddress,
|
|
|
|
|
pendingPost,
|
|
|
|
|
]);
|
2024-08-26 11:32:40 +02:00
|
|
|
|
|
|
|
|
return initialTheme;
|
2024-06-19 22:55:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default useInitialTheme;
|