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';
|
|
|
|
|
import useDefaultSubplebbits from './use-default-subplebbits';
|
2024-07-10 12:36:44 +02:00
|
|
|
import { isAllView, isHomeView, isNotFoundView, isPendingPostView, isSubscriptionsView } from '../lib/utils/view-utils';
|
2024-06-19 22:55:22 +02:00
|
|
|
import { nsfwTags } from '../views/home/home';
|
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 });
|
2024-06-19 22:55:22 +02:00
|
|
|
const getTheme = useThemeStore((state) => state.getTheme);
|
2024-07-10 12:36:44 +02:00
|
|
|
const currentTheme = useThemeStore((state) => state.currentTheme);
|
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);
|
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);
|
|
|
|
|
if (subplebbit && subplebbit.tags && subplebbit.tags.some((tag) => nsfwTags.includes(tag))) {
|
|
|
|
|
theme = getTheme('nsfw', false) || 'yotsuba';
|
|
|
|
|
} else {
|
|
|
|
|
theme = getTheme('sfw', false) || 'yotsuba-b';
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
theme = currentTheme || 'yotsuba';
|
|
|
|
|
}
|
2024-08-26 11:32:40 +02:00
|
|
|
} else if (isInAllView || isInSubscriptionsView) {
|
2024-08-26 17:39:27 +02:00
|
|
|
theme = getTheme('sfw', false) || 'yotsuba-b'; // Add 'false' parameter
|
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);
|
2024-08-26 11:32:40 +02:00
|
|
|
if (subplebbit && subplebbit.tags && subplebbit.tags.some((tag) => nsfwTags.includes(tag))) {
|
2024-08-26 17:39:27 +02:00
|
|
|
theme = getTheme('nsfw', false) || 'yotsuba'; // Add 'false' parameter
|
2024-08-26 11:32:40 +02:00
|
|
|
} else {
|
2024-08-26 17:39:27 +02:00
|
|
|
theme = getTheme('sfw', false) || 'yotsuba-b'; // Add 'false' parameter
|
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,
|
|
|
|
|
isInHomeView,
|
|
|
|
|
isInNotFoundView,
|
|
|
|
|
paramsSubplebbitAddress,
|
|
|
|
|
getTheme,
|
|
|
|
|
currentTheme,
|
|
|
|
|
subplebbits,
|
|
|
|
|
pendingPostSubplebbitAddress,
|
|
|
|
|
pendingPost,
|
|
|
|
|
]);
|
2024-08-26 11:32:40 +02:00
|
|
|
|
|
|
|
|
return initialTheme;
|
2024-06-19 22:55:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default useInitialTheme;
|