2026-07-30 00:29:12 +07:00
|
|
|
import { memo, useState } from 'react';
|
2025-06-04 16:37:17 +02:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2025-11-14 13:05:52 +01:00
|
|
|
import { useLocation, useParams, useNavigate } from 'react-router-dom';
|
2026-06-24 16:23:42 +07:00
|
|
|
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';
|
2026-02-26 16:42:36 +08:00
|
|
|
import getShortAddress from '../../lib/get-short-address';
|
2026-04-17 12:51:14 +07:00
|
|
|
import { useCommunityIdentifier } from '../../hooks/use-community-identifiers';
|
2026-03-13 13:25:10 +08:00
|
|
|
import { useStableCommunity } from '../../hooks/use-stable-community';
|
2025-03-05 17:38:34 +01:00
|
|
|
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';
|
2024-05-29 21:00:13 +02:00
|
|
|
import styles from './board-header.module.css';
|
2026-05-21 17:59:52 +07:00
|
|
|
import { useDirectories } from '../../hooks/use-directories';
|
2026-03-13 13:25:10 +08:00
|
|
|
import { useResolvedCommunityAddress } from '../../hooks/use-resolved-community-address';
|
2026-06-24 16:23:42 +07:00
|
|
|
import { normalizeAccountCommentIndex } from '../../lib/utils/account-comment-index-utils';
|
2024-06-30 11:47:09 +02:00
|
|
|
import useIsMobile from '../../hooks/use-is-mobile';
|
2026-03-13 13:25:10 +08:00
|
|
|
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';
|
2025-11-01 19:39:22 +01:00
|
|
|
import { BANNERS } from '../../generated/asset-manifest';
|
2024-03-19 21:12:57 +01:00
|
|
|
|
|
|
|
|
const ImageBanner = () => {
|
2025-11-01 19:39:22 +01:00
|
|
|
const [banner] = useState(() => BANNERS[Math.floor(Math.random() * BANNERS.length)]);
|
2024-03-19 21:12:57 +01:00
|
|
|
|
2025-11-01 16:46:51 +01:00
|
|
|
return <img src={banner} alt='' />;
|
2024-03-19 21:12:57 +01:00
|
|
|
};
|
|
|
|
|
|
2026-07-24 16:36:09 +07:00
|
|
|
// Separate component for offline indicator to isolate rerenders from sync lifecycle changes.
|
2026-03-13 13:25:10 +08:00
|
|
|
const OfflineIndicator = ({ communityAddress }: { communityAddress: string | undefined }) => {
|
2026-04-17 12:51:14 +07:00
|
|
|
const communityIdentifier = useCommunityIdentifier(communityAddress);
|
|
|
|
|
const community = useCommunity(communityIdentifier ? { community: communityIdentifier } : undefined);
|
|
|
|
|
const { isOffline, isOnlineStatusLoading, offlineIconClass, offlineTitle } = useIsCommunityOffline(community, communityAddress);
|
2026-01-02 16:42:16 +01:00
|
|
|
|
|
|
|
|
if (!isOffline && !isOnlineStatusLoading) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<span className={styles.offlineIconWrapper}>
|
|
|
|
|
<Tooltip content={offlineTitle}>
|
|
|
|
|
<span className={`${styles.offlineIcon} ${offlineIconClass}`} />
|
|
|
|
|
</Tooltip>
|
|
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-30 00:29:12 +07:00
|
|
|
// No props, so parent rerenders never change its output.
|
|
|
|
|
const BoardHeader = memo(() => {
|
2025-06-04 16:37:17 +02:00
|
|
|
const { t } = useTranslation();
|
2024-05-17 14:55:41 +02:00
|
|
|
const location = useLocation();
|
2024-04-25 22:02:55 +02:00
|
|
|
const params = useParams();
|
2025-11-14 13:05:52 +01:00
|
|
|
const navigate = useNavigate();
|
2024-10-29 17:40:09 +01:00
|
|
|
const isInAllView = isAllView(location.pathname);
|
2024-06-17 12:17:36 +02:00
|
|
|
const isInSubscriptionsView = isSubscriptionsView(location.pathname, useParams());
|
2025-03-05 17:38:34 +01:00
|
|
|
const isInModView = isModView(location.pathname);
|
2026-03-13 16:06:36 +08:00
|
|
|
const isInArchiveView = isArchiveRoute(location.pathname);
|
2026-05-19 23:34:33 +07:00
|
|
|
const isInDirectoryListView = isDirectoryListRoute(location.pathname);
|
2026-06-24 16:23:42 +07:00
|
|
|
const accountComment = useAccountComment({ commentIndex: normalizeAccountCommentIndex(params?.accountCommentIndex) });
|
2026-03-13 13:25:10 +08:00
|
|
|
const resolvedAddress = useResolvedCommunityAddress();
|
|
|
|
|
const communityAddress = resolvedAddress || accountComment?.communityAddress;
|
2024-04-25 22:02:55 +02:00
|
|
|
|
2026-03-13 13:25:10 +08:00
|
|
|
// Use stable community for display fields to avoid rerenders from updatingState
|
|
|
|
|
const stableCommunity = useStableCommunity(communityAddress);
|
|
|
|
|
const { address, shortAddress } = stableCommunity || {};
|
2024-05-17 14:55:41 +02:00
|
|
|
|
2026-01-28 16:51:27 +08:00
|
|
|
const directories = useDirectories();
|
2025-11-06 17:24:09 +01:00
|
|
|
|
2026-03-13 13:25:10 +08:00
|
|
|
// 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);
|
2025-11-06 17:24:09 +01:00
|
|
|
|
2026-01-02 16:42:16 +01:00
|
|
|
// 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 });
|
2025-11-14 12:59:26 +01:00
|
|
|
|
2025-06-04 16:37:17 +02:00
|
|
|
const title = isInAllView
|
2026-05-21 17:59:52 +07:00
|
|
|
? '/all/ - All 5chan Directories'
|
2025-06-04 16:37:17 +02:00
|
|
|
: isInSubscriptionsView
|
2026-01-02 16:42:16 +01:00
|
|
|
? '/subs/ - Subscriptions'
|
|
|
|
|
: isInModView
|
2026-05-21 17:59:52 +07:00
|
|
|
? '/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
|
2026-06-08 22:30:14 +07:00
|
|
|
? t('all_subtitle')
|
2026-05-19 23:34:33 +07:00
|
|
|
: isInSubscriptionsView
|
|
|
|
|
? subscriptionsSubtitle
|
|
|
|
|
: isInModView
|
2026-05-21 17:59:52 +07:00
|
|
|
? ''
|
2026-05-19 23:34:33 +07:00
|
|
|
: isInDirectoryListView
|
|
|
|
|
? t('directory_subtitle', { boardIdentifier: params.boardIdentifier })
|
2026-07-07 23:45:13 +07:00
|
|
|
: `${specialBoard?.address || address || communityAddress || ''}`;
|
2024-04-08 17:13:02 +02:00
|
|
|
|
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}>
|
2026-03-13 13:25:10 +08:00
|
|
|
<ImageBanner key={isInAllView ? 'all' : isInSubscriptionsView ? 'subscriptions' : communityAddress} />
|
2024-06-30 11:47:09 +02:00
|
|
|
</div>
|
|
|
|
|
)}
|
2024-06-08 21:22:17 +02:00
|
|
|
<div className={styles.boardTitle}>
|
2025-06-04 14:54:11 +02:00
|
|
|
{title ||
|
|
|
|
|
(shortAddress
|
|
|
|
|
? shortAddress.endsWith('.eth') || shortAddress.endsWith('.sol')
|
|
|
|
|
? shortAddress.slice(0, -4)
|
|
|
|
|
: shortAddress
|
2026-03-13 13:25:10 +08:00
|
|
|
: communityAddress && getShortAddress(communityAddress))}
|
2026-05-19 23:34:33 +07:00
|
|
|
{!isInAllView && !isInSubscriptionsView && !isInModView && !isInDirectoryListView && <OfflineIndicator communityAddress={communityAddress} />}
|
2024-06-08 21:22:17 +02:00
|
|
|
</div>
|
2025-11-14 13:14:34 +01:00
|
|
|
<div className={styles.boardSubtitle}>
|
|
|
|
|
{isInSubscriptionsView ? (
|
2026-05-29 17:09:07 +07:00
|
|
|
<button
|
|
|
|
|
type='button'
|
2026-02-24 15:15:44 +08:00
|
|
|
className={styles.clickableSubtitle}
|
|
|
|
|
tabIndex={0}
|
|
|
|
|
onKeyDown={(e) => {
|
|
|
|
|
if (e.key === 'Enter' || e.key === ' ') {
|
|
|
|
|
e.preventDefault();
|
2026-07-11 00:09:30 +07:00
|
|
|
navigate('/subs/settings?section=subscriptions-settings');
|
2026-02-24 15:15:44 +08:00
|
|
|
}
|
|
|
|
|
}}
|
2026-07-11 00:09:30 +07:00
|
|
|
onClick={() => navigate('/subs/settings?section=subscriptions-settings')}
|
2026-02-24 15:15:44 +08:00
|
|
|
>
|
2025-11-14 13:14:34 +01:00
|
|
|
{subtitle}
|
2026-05-29 17:09:07 +07:00
|
|
|
</button>
|
2026-06-08 22:30:14 +07:00
|
|
|
) : isInDirectoryListView || isInAllView ? (
|
2026-05-19 23:34:33 +07:00
|
|
|
<span>{subtitle}</span>
|
2026-06-08 22:30:14 +07:00
|
|
|
) : !isInModView && subtitle ? (
|
2026-02-12 18:15:11 +08:00
|
|
|
<span title={t('board_address_tooltip')}>{subtitle}</span>
|
2025-11-14 13:14:34 +01:00
|
|
|
) : (
|
|
|
|
|
subtitle
|
|
|
|
|
)}
|
2025-11-14 13:05:52 +01:00
|
|
|
</div>
|
2026-03-13 16:06:36 +08:00
|
|
|
{!isInArchiveView && <hr />}
|
2024-03-19 21:12:57 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
2026-07-30 00:29:12 +07:00
|
|
|
});
|
|
|
|
|
BoardHeader.displayName = 'BoardHeader';
|
2024-03-19 21:12:57 +01:00
|
|
|
|
2024-05-29 21:00:13 +02:00
|
|
|
export default BoardHeader;
|