2024-03-19 21:12:57 +01:00
|
|
|
import { 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-01-02 16:42:16 +01:00
|
|
|
import { useAccountComment } from '@plebbit/plebbit-react-hooks';
|
|
|
|
|
import useAccountsStore from '@plebbit/plebbit-react-hooks/dist/stores/accounts';
|
2025-03-06 13:15:33 +01:00
|
|
|
import useSubplebbitsStore from '@plebbit/plebbit-react-hooks/dist/stores/subplebbits';
|
2026-01-02 16:42:16 +01:00
|
|
|
import Plebbit from '@plebbit/plebbit-js';
|
|
|
|
|
import { useStableSubplebbit } from '../../hooks/use-stable-subplebbit';
|
2025-03-05 17:38:34 +01:00
|
|
|
import { isAllView, isSubscriptionsView, isModView } from '../../lib/utils/view-utils';
|
2024-05-29 21:00:13 +02:00
|
|
|
import styles from './board-header.module.css';
|
2026-01-28 16:51:27 +08:00
|
|
|
import { useDirectoriesMetadata, useDirectories } from '../../hooks/use-directories';
|
2025-11-07 12:50:58 +01:00
|
|
|
import { useResolvedSubplebbitAddress } from '../../hooks/use-resolved-subplebbit-address';
|
2024-06-30 11:47:09 +02:00
|
|
|
import useIsMobile from '../../hooks/use-is-mobile';
|
2024-07-07 20:02:58 +02:00
|
|
|
import useIsSubplebbitOffline from '../../hooks/use-is-subplebbit-offline';
|
2024-12-24 17:13:12 +01:00
|
|
|
import { shouldShowSnow } from '../../lib/snow';
|
2025-02-21 17:31:16 +01:00
|
|
|
import Tooltip from '../tooltip';
|
2026-02-18 15:31:01 +08:00
|
|
|
import startCase from 'lodash/startCase';
|
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-01-02 16:42:16 +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 = ({ subplebbitAddress }: { subplebbitAddress: string | undefined }) => {
|
|
|
|
|
// Subscribe to full subplebbit including transient state for offline detection
|
|
|
|
|
const subplebbit = useSubplebbitsStore((state) => (subplebbitAddress ? state.subplebbits[subplebbitAddress] : undefined));
|
|
|
|
|
const { isOffline, isOnlineStatusLoading, offlineIconClass, offlineTitle } = useIsSubplebbitOffline(subplebbit);
|
|
|
|
|
|
|
|
|
|
if (!isOffline && !isOnlineStatusLoading) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<span className={styles.offlineIconWrapper}>
|
|
|
|
|
<Tooltip content={offlineTitle}>
|
|
|
|
|
<span className={`${styles.offlineIcon} ${offlineIconClass}`} />
|
|
|
|
|
</Tooltip>
|
|
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-29 21:00:13 +02:00
|
|
|
const BoardHeader = () => {
|
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);
|
2024-04-25 22:02:55 +02:00
|
|
|
const accountComment = useAccountComment({ commentIndex: params?.accountCommentIndex as any });
|
2025-11-07 12:50:58 +01:00
|
|
|
const resolvedAddress = useResolvedSubplebbitAddress();
|
|
|
|
|
const subplebbitAddress = resolvedAddress || accountComment?.subplebbitAddress;
|
2024-04-25 22:02:55 +02:00
|
|
|
|
2026-01-02 16:42:16 +01:00
|
|
|
// Use stable subplebbit for display fields to avoid rerenders from updatingState
|
|
|
|
|
const stableSubplebbit = useStableSubplebbit(subplebbitAddress);
|
|
|
|
|
const { address, shortAddress } = stableSubplebbit || {};
|
2024-05-17 14:55:41 +02:00
|
|
|
|
2026-01-28 16:51:27 +08:00
|
|
|
const directoriesMetadata = useDirectoriesMetadata();
|
|
|
|
|
const directories = useDirectories();
|
2025-11-06 17:24:09 +01:00
|
|
|
|
|
|
|
|
// Find matching subplebbit from default list to get its title
|
2026-01-28 16:51:27 +08:00
|
|
|
const defaultSubplebbit = subplebbitAddress ? directories.find((s) => s.address === subplebbitAddress) : null;
|
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-01-28 16:51:27 +08:00
|
|
|
? directoriesMetadata?.title || '/all/ - 5chan Directories'
|
2025-06-04 16:37:17 +02:00
|
|
|
: isInSubscriptionsView
|
2026-01-02 16:42:16 +01:00
|
|
|
? '/subs/ - Subscriptions'
|
|
|
|
|
: isInModView
|
2026-02-11 19:01:09 +08:00
|
|
|
? startCase(t('boards_you_moderate'))
|
2026-01-02 16:42:16 +01:00
|
|
|
: defaultSubplebbit?.title || stableSubplebbit?.title;
|
2025-11-14 12:59:26 +01:00
|
|
|
const subtitle = isInAllView ? '' : isInSubscriptionsView ? subscriptionsSubtitle : isInModView ? '/mod/' : `${address || subplebbitAddress || ''}`;
|
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}>
|
2025-02-24 12:21:59 +01:00
|
|
|
<ImageBanner key={isInAllView ? 'all' : isInSubscriptionsView ? 'subscriptions' : subplebbitAddress} />
|
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
|
2025-12-24 13:29:09 +01:00
|
|
|
: subplebbitAddress && Plebbit.getShortAddress({ address: subplebbitAddress }))}
|
2026-01-02 16:42:16 +01:00
|
|
|
{!isInAllView && !isInSubscriptionsView && !isInModView && <OfflineIndicator subplebbitAddress={subplebbitAddress} />}
|
2024-06-08 21:22:17 +02:00
|
|
|
</div>
|
2025-11-14 13:14:34 +01:00
|
|
|
<div className={styles.boardSubtitle}>
|
|
|
|
|
{isInSubscriptionsView ? (
|
2026-02-24 15:15:44 +08:00
|
|
|
<span
|
|
|
|
|
className={styles.clickableSubtitle}
|
|
|
|
|
role='button'
|
|
|
|
|
tabIndex={0}
|
|
|
|
|
onKeyDown={(e) => {
|
|
|
|
|
if (e.key === 'Enter' || e.key === ' ') {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
navigate('/subs/settings#subscriptions-settings');
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
onClick={() => navigate('/subs/settings#subscriptions-settings')}
|
|
|
|
|
>
|
2025-11-14 13:14:34 +01:00
|
|
|
{subtitle}
|
|
|
|
|
</span>
|
2026-02-12 18:15:11 +08:00
|
|
|
) : !isInAllView && !isInModView && subtitle ? (
|
|
|
|
|
<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>
|
2024-03-19 21:12:57 +01:00
|
|
|
<hr />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-29 21:00:13 +02:00
|
|
|
export default BoardHeader;
|