Files
5chan/src/components/board-header/board-header.tsx
T

143 lines
6.1 KiB
TypeScript
Raw Normal View History

2024-03-19 21:12:57 +01:00
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useLocation, useParams, useNavigate } from 'react-router-dom';
import { useAccountComment, useCommunity } from '@bitsocial/bitsocial-react-hooks';
2026-06-23 18:48:47 +07:00
import { accountsStore as useAccountsStore } from '../../lib/bitsocial-internals/stores';
import getShortAddress from '../../lib/get-short-address';
import { useCommunityIdentifier } from '../../hooks/use-community-identifiers';
import { useStableCommunity } from '../../hooks/use-stable-community';
import { isAllView, isSubscriptionsView, isModView } from '../../lib/utils/view-utils';
2026-05-19 23:34:33 +07:00
import { isArchiveRoute, isDirectoryListRoute } from '../../lib/utils/route-utils';
2026-07-02 21:22:19 +07:00
import { getSpecialBoardByAddress } from '../../lib/special-boards';
import styles from './board-header.module.css';
import { useDirectories } from '../../hooks/use-directories';
import { useResolvedCommunityAddress } from '../../hooks/use-resolved-community-address';
import { normalizeAccountCommentIndex } from '../../lib/utils/account-comment-index-utils';
2024-06-30 11:47:09 +02:00
import useIsMobile from '../../hooks/use-is-mobile';
import useIsCommunityOffline from '../../hooks/use-is-community-offline';
2024-12-24 17:13:12 +01:00
import { shouldShowSnow } from '../../lib/snow';
2026-06-23 18:48:47 +07:00
import Tooltip from '../tooltip/tooltip';
import { BANNERS } from '../../generated/asset-manifest';
2024-03-19 21:12:57 +01:00
const ImageBanner = () => {
const [banner] = useState(() => BANNERS[Math.floor(Math.random() * BANNERS.length)]);
2024-03-19 21:12:57 +01:00
return <img src={banner} alt='' />;
2024-03-19 21:12:57 +01:00
};
// Separate component for offline indicator to isolate rerenders from updatingState
// Only this component will rerender when updatingState changes, not the whole BoardHeader
const OfflineIndicator = ({ communityAddress }: { communityAddress: string | undefined }) => {
const communityIdentifier = useCommunityIdentifier(communityAddress);
const community = useCommunity(communityIdentifier ? { community: communityIdentifier } : undefined);
const { isOffline, isOnlineStatusLoading, offlineIconClass, offlineTitle } = useIsCommunityOffline(community, communityAddress);
if (!isOffline && !isOnlineStatusLoading) {
return null;
}
return (
<span className={styles.offlineIconWrapper}>
<Tooltip content={offlineTitle}>
<span className={`${styles.offlineIcon} ${offlineIconClass}`} />
</Tooltip>
</span>
);
};
const BoardHeader = () => {
const { t } = useTranslation();
const location = useLocation();
2024-04-25 22:02:55 +02:00
const params = useParams();
const navigate = useNavigate();
2024-10-29 17:40:09 +01:00
const isInAllView = isAllView(location.pathname);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, useParams());
const isInModView = isModView(location.pathname);
const isInArchiveView = isArchiveRoute(location.pathname);
2026-05-19 23:34:33 +07:00
const isInDirectoryListView = isDirectoryListRoute(location.pathname);
const accountComment = useAccountComment({ commentIndex: normalizeAccountCommentIndex(params?.accountCommentIndex) });
const resolvedAddress = useResolvedCommunityAddress();
const communityAddress = resolvedAddress || accountComment?.communityAddress;
2024-04-25 22:02:55 +02:00
// Use stable community for display fields to avoid rerenders from updatingState
const stableCommunity = useStableCommunity(communityAddress);
const { address, shortAddress } = stableCommunity || {};
const directories = useDirectories();
// Find matching community from default list to get its title
const defaultCommunity = communityAddress ? directories.find((s) => s.address === communityAddress) : null;
2026-07-02 21:22:19 +07:00
const specialBoard = getSpecialBoardByAddress(communityAddress);
// Use accounts store with selector to only subscribe to subscriptions count
const subscriptionsCount = useAccountsStore((state) => {
const activeAccountId = state.activeAccountId;
const activeAccount = activeAccountId ? state.accounts[activeAccountId] : undefined;
return activeAccount?.subscriptions?.length || 0;
});
const subscriptionsSubtitle = t('subscriptions_subtitle', { count: subscriptionsCount });
const title = isInAllView
? '/all/ - All 5chan Directories'
: isInSubscriptionsView
? '/subs/ - Subscriptions'
: isInModView
? '/mod/ - Boards You Moderate'
2026-07-02 21:22:19 +07:00
: defaultCommunity?.title || specialBoard?.title || stableCommunity?.title;
2026-05-19 23:34:33 +07:00
const subtitle = isInAllView
? t('all_subtitle')
2026-05-19 23:34:33 +07:00
: isInSubscriptionsView
? subscriptionsSubtitle
: isInModView
? ''
2026-05-19 23:34:33 +07:00
: isInDirectoryListView
? t('directory_subtitle', { boardIdentifier: params.boardIdentifier })
: `${address || communityAddress || ''}`;
2024-03-19 21:12:57 +01:00
return (
2024-12-24 17:13:12 +01:00
<div className={`${styles.content} ${shouldShowSnow() ? styles.garland : ''}`}>
2024-06-30 11:47:09 +02:00
{!useIsMobile() && (
<div className={styles.bannerCnt}>
<ImageBanner key={isInAllView ? 'all' : isInSubscriptionsView ? 'subscriptions' : communityAddress} />
2024-06-30 11:47:09 +02:00
</div>
)}
<div className={styles.boardTitle}>
{title ||
(shortAddress
? shortAddress.endsWith('.eth') || shortAddress.endsWith('.sol')
? shortAddress.slice(0, -4)
: shortAddress
: communityAddress && getShortAddress(communityAddress))}
2026-05-19 23:34:33 +07:00
{!isInAllView && !isInSubscriptionsView && !isInModView && !isInDirectoryListView && <OfflineIndicator communityAddress={communityAddress} />}
</div>
<div className={styles.boardSubtitle}>
{isInSubscriptionsView ? (
2026-05-29 17:09:07 +07:00
<button
type='button'
className={styles.clickableSubtitle}
tabIndex={0}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
navigate('/subs/settings#subscriptions-settings');
}
}}
onClick={() => navigate('/subs/settings#subscriptions-settings')}
>
{subtitle}
2026-05-29 17:09:07 +07:00
</button>
) : isInDirectoryListView || isInAllView ? (
2026-05-19 23:34:33 +07:00
<span>{subtitle}</span>
) : !isInModView && subtitle ? (
<span title={t('board_address_tooltip')}>{subtitle}</span>
) : (
subtitle
)}
</div>
{!isInArchiveView && <hr />}
2024-03-19 21:12:57 +01:00
</div>
);
};
export default BoardHeader;