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

65 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-03-19 21:12:57 +01:00
import { useState } from 'react';
import { useLocation, useParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { useAccountComment, useSubplebbit } from '@plebbit/plebbit-react-hooks';
import { getFormattedTimeAgo } from '../../lib/utils/time-utils';
import { isAllView, isSubscriptionsView } from '../../lib/utils/view-utils';
import styles from './board-header.module.css';
import { useMultisubMetadata } from '../../hooks/use-default-subplebbits';
2024-06-30 11:47:09 +02:00
import useIsMobile from '../../hooks/use-is-mobile';
2024-03-19 21:12:57 +01:00
2024-07-04 12:13:57 +02:00
const totalBanners = 60;
2024-03-19 21:12:57 +01:00
const ImageBanner = () => {
const [imagePath] = useState(() => {
const randomBannerIndex = Math.floor(Math.random() * totalBanners) + 1;
2024-06-20 17:02:11 +02:00
return `assets/banners/banner-${randomBannerIndex}.jpg`;
2024-03-19 21:12:57 +01:00
});
2024-04-13 20:53:25 +02:00
return <img src={imagePath} alt='' />;
2024-03-19 21:12:57 +01:00
};
const BoardHeader = () => {
const { t } = useTranslation();
const location = useLocation();
2024-04-25 22:02:55 +02:00
const params = useParams();
const isInAllView = isAllView(location.pathname, params);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, useParams());
2024-04-25 22:02:55 +02:00
const accountComment = useAccountComment({ commentIndex: params?.accountCommentIndex as any });
const subplebbitAddress = params?.subplebbitAddress || accountComment?.subplebbitAddress;
const subplebbit = useSubplebbit({ subplebbitAddress });
const { address, shortAddress, updatedAt } = subplebbit || {};
const multisubMetadata = useMultisubMetadata();
const title = isInAllView ? multisubMetadata?.title || 'all' : isInSubscriptionsView ? 'Subscriptions' : subplebbit?.title;
const subtitle = isInAllView ? 'p/all' : isInSubscriptionsView ? 'p/subscriptions' : `p/${address}`;
const isBoardOffline = updatedAt && updatedAt < Date.now() / 1000 - 60 * 60;
const offlineMessage = updatedAt
? isBoardOffline && t('posts_last_synced_info', { time: getFormattedTimeAgo(updatedAt), interpolation: { escapeValue: false } })
: t('subplebbit_offline_info');
2024-03-19 21:12:57 +01:00
return (
<div className={styles.content}>
2024-06-30 11:47:09 +02:00
{!useIsMobile() && (
<div className={styles.bannerCnt}>
<ImageBanner key={isInAllView ? 'all' : isInSubscriptionsView ? 'subscriptions' : address} />
</div>
)}
<div className={styles.boardTitle}>
{title || `p/${shortAddress || subplebbitAddress}`}
{isBoardOffline && <span className={styles.offlineIcon} title={offlineMessage} />}
</div>
<div className={styles.boardSubtitle}>{subtitle}</div>
2024-03-19 21:12:57 +01:00
<hr />
</div>
);
};
export default BoardHeader;