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

71 lines
2.6 KiB
TypeScript
Raw Normal View History

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