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

122 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-04-03 22:52:27 +02:00
import { useTranslation } from 'react-i18next';
import { Link, useLocation, useParams } from 'react-router-dom';
2024-04-25 22:02:55 +02:00
import { useAccountComment, useSubscribe } from '@plebbit/plebbit-react-hooks';
import styles from './board-buttons.module.css';
2024-05-17 15:06:30 +02:00
import { isAllView, isCatalogView, isPendingPostView, isPostPageView, isSubscriptionsView } from '../../lib/utils/view-utils';
interface BoardButtonsProps {
address: string | undefined;
2024-04-12 17:47:30 +02:00
isInCatalogView?: boolean;
}
const OptionsButton = () => {
const { t } = useTranslation();
return <button className='button'>{t('options')}</button>;
};
2024-04-12 17:47:30 +02:00
const CatalogButton = ({ address, isInCatalogView }: BoardButtonsProps) => {
const { t } = useTranslation();
2024-04-03 22:52:27 +02:00
return (
<button className='button'>
2024-04-12 17:47:30 +02:00
<Link to={isInCatalogView ? `/p/${address}` : `/p/${address}/catalog`}>{t(isInCatalogView ? 'return' : 'catalog')}</Link>
2024-04-03 22:52:27 +02:00
</button>
);
};
const SubscribeButton = ({ address }: BoardButtonsProps) => {
const { t } = useTranslation();
const { subscribed, subscribe, unsubscribe } = useSubscribe({ subplebbitAddress: address });
return (
<button className='button' onClick={subscribed ? unsubscribe : subscribe}>
{subscribed ? t('unsubscribe') : t('subscribe')}
</button>
);
};
2024-04-03 22:52:27 +02:00
const ReturnButton = ({ address }: BoardButtonsProps) => {
const { t } = useTranslation();
return (
<button className='button'>
<Link to={`/p/${address}`}>{t('return')}</Link>
</button>
);
};
const BottomButton = () => {
const { t } = useTranslation();
return (
<button className='button' onClick={() => window.scrollTo(0, document.body.scrollHeight)}>
{t('bottom')}
</button>
);
};
export const MobileBoardButtons = () => {
2024-04-25 22:02:55 +02:00
const params = useParams();
const location = useLocation();
const accountComment = useAccountComment({ commentIndex: params?.accountCommentIndex as any });
const subplebbitAddress = params?.subplebbitAddress || accountComment?.subplebbitAddress;
const isInPostView = isPostPageView(location.pathname, params);
2024-04-28 17:44:21 +02:00
const isInCatalogView = isCatalogView(location.pathname, params);
2024-04-25 22:02:55 +02:00
const isInPendingPostPage = isPendingPostView(location.pathname, params);
return (
<div className={styles.mobileBoardButtons}>
{isInPostView || isInPendingPostPage ? (
2024-04-03 22:52:27 +02:00
<div className={styles.mobilePostPageButtons}>
2024-04-25 22:02:55 +02:00
<ReturnButton address={subplebbitAddress} />
<CatalogButton address={subplebbitAddress} />
2024-04-03 22:52:27 +02:00
<BottomButton />
<div className={styles.mobilePostPageButtonsSecondRow}>
2024-04-03 22:52:27 +02:00
<OptionsButton />
2024-04-25 22:02:55 +02:00
<SubscribeButton address={subplebbitAddress} />
2024-04-03 22:52:27 +02:00
</div>
</div>
) : (
<>
<OptionsButton />
2024-04-28 17:44:21 +02:00
<CatalogButton address={subplebbitAddress} isInCatalogView={isInCatalogView} />
2024-04-25 22:02:55 +02:00
<SubscribeButton address={subplebbitAddress} />
2024-04-03 22:52:27 +02:00
</>
)}
</div>
);
};
export const DesktopBoardButtons = () => {
2024-04-12 17:47:30 +02:00
const params = useParams();
2024-04-25 22:02:55 +02:00
const location = useLocation();
const accountComment = useAccountComment({ commentIndex: params?.accountCommentIndex as any });
const subplebbitAddress = params?.subplebbitAddress || accountComment?.subplebbitAddress;
2024-04-12 17:47:30 +02:00
const isInCatalogView = isCatalogView(location.pathname, params);
2024-05-17 15:06:30 +02:00
const isInAllView = isAllView(location.pathname);
2024-04-25 22:02:55 +02:00
const isInPendingPostPage = isPendingPostView(location.pathname, params);
const isInPostView = isPostPageView(location.pathname, params);
2024-05-17 15:06:30 +02:00
const isInSubscriptionsView = isSubscriptionsView(location.pathname);
2024-04-12 17:47:30 +02:00
return (
<div className={styles.desktopBoardButtons}>
<hr />
{isInPostView || isInPendingPostPage ? (
2024-04-03 22:52:27 +02:00
<>
2024-04-28 17:44:21 +02:00
[<ReturnButton address={subplebbitAddress} />] [<CatalogButton address={subplebbitAddress} />] [<BottomButton />] [
2024-04-25 22:02:55 +02:00
<OptionsButton />]
2024-04-03 22:52:27 +02:00
<span className={styles.subscribeButton}>
2024-04-25 22:02:55 +02:00
[<SubscribeButton address={subplebbitAddress} />]
2024-04-03 22:52:27 +02:00
</span>
</>
) : (
<>
2024-04-25 22:02:55 +02:00
[<OptionsButton />] [<CatalogButton address={subplebbitAddress} isInCatalogView={isInCatalogView} />]
2024-05-17 15:06:30 +02:00
{!(isInAllView || isInSubscriptionsView) && (
<span className={styles.subscribeButton}>
[<SubscribeButton address={subplebbitAddress} />]
</span>
)}
2024-04-03 22:52:27 +02:00
</>
)}
</div>
);
};