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

408 lines
14 KiB
TypeScript
Raw Normal View History

2024-04-03 22:52:27 +02:00
import { useTranslation } from 'react-i18next';
import { Link, useLocation, useNavigate, useParams } from 'react-router-dom';
2024-09-18 12:37:39 +02:00
import { useAccountComment, useComment, useSubplebbit, useSubscribe } from '@plebbit/plebbit-react-hooks';
2024-09-15 21:38:16 +02:00
import { isAllView, isCatalogView, isDescriptionView, isPendingPostView, isPostPageView, isSubscriptionsView } from '../../lib/utils/view-utils';
2024-06-20 15:00:31 +02:00
import useCatalogStyleStore from '../../stores/use-catalog-style-store';
2024-05-31 10:41:44 +02:00
import useFeedResetStore from '../../stores/use-feed-reset-store';
2024-06-20 15:00:31 +02:00
import useSortingStore from '../../stores/use-sorting-store';
import useTimeFilter from '../../hooks/use-time-filter';
import CatalogFilters from '../../views/catalog/catalog-filters/';
2024-05-31 10:41:44 +02:00
import styles from './board-buttons.module.css';
import Tooltip from '../tooltip';
import useCountLinksInReplies from '../../hooks/use-count-links-in-replies';
import _ from 'lodash';
import useIsMobile from '../../hooks/use-is-mobile';
import useCatalogFiltersStore from '../../stores/use-catalog-filters-store';
import { useEffect } from 'react';
interface BoardButtonsProps {
address?: string | undefined;
2024-06-09 16:50:51 +02:00
isInAllView?: boolean;
2024-04-12 17:47:30 +02:00
isInCatalogView?: boolean;
2024-05-17 16:39:02 +02:00
isInSubscriptionsView?: boolean;
2024-06-09 16:50:51 +02:00
isTopbar?: boolean;
}
const CatalogButton = ({ address, isInAllView, isInSubscriptionsView }: BoardButtonsProps) => {
const { t } = useTranslation();
2024-06-11 15:13:04 +02:00
const params = useParams();
const createCatalogLink = () => {
if (isInAllView) {
if (params?.timeFilterName) return `/p/all/catalog/${params.timeFilterName}`;
return `/p/all/catalog`;
} else if (isInSubscriptionsView) {
if (params?.timeFilterName) return `/p/subscriptions/catalog/${params.timeFilterName}`;
return `/p/subscriptions/catalog`;
}
return `/p/${address}/catalog`;
};
2024-05-17 21:27:06 +02:00
return (
<button className='button'>
2024-06-11 15:13:04 +02:00
<Link to={createCatalogLink()}>{t('catalog')}</Link>
2024-05-17 21:27:06 +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>
);
};
const ReturnButton = ({ address, isInAllView, isInSubscriptionsView }: BoardButtonsProps) => {
2024-04-03 22:52:27 +02:00
const { t } = useTranslation();
2024-06-11 15:13:04 +02:00
const params = useParams();
const createReturnLink = () => {
if (isInAllView) {
if (params?.timeFilterName) return `/p/all/${params.timeFilterName}`;
return `/p/all`;
} else if (isInSubscriptionsView) {
if (params?.timeFilterName) return `/p/subscriptions/${params.timeFilterName}`;
return `/p/subscriptions`;
}
return `/p/${address}`;
};
2024-04-03 22:52:27 +02:00
return (
<button className='button'>
2024-06-11 15:13:04 +02:00
<Link to={createReturnLink()}>{t('return')}</Link>
2024-04-03 22:52:27 +02:00
</button>
);
};
const RefreshButton = () => {
2024-05-31 17:09:20 +02:00
const { t } = useTranslation();
const reset = useFeedResetStore((state) => state.reset);
2024-04-03 22:52:27 +02:00
return (
<button className='button' onClick={() => reset && reset()}>
2024-05-31 17:09:20 +02:00
{t('refresh')}
2024-04-03 22:52:27 +02:00
</button>
);
};
const UpdateButton = () => {
const { t } = useTranslation();
const isMobile = useIsMobile();
const handleUpdate = () => {
window.alert('Manual updates are not available yet. Posts update automatically every ~2 minutes.');
};
return (
<>
{/* TODO: Implement update button once available in API */}
{isMobile ? (
<button className={`button ${styles.disabledButton}`} onClick={handleUpdate}>
{t('update')}
</button>
) : (
<button className={`button ${styles.disabledButton}`} onClick={handleUpdate}>
{t('update')}
</button>
)}
</>
);
};
const AutoButton = () => {
const { t } = useTranslation();
const isMobile = useIsMobile();
return (
<>
{isMobile ? (
<button className='button'>
<label>
<input type='checkbox' className={styles.autoCheckbox} checked disabled />
{t('Auto')}
</label>
</button>
) : (
<label>
{' '}
<input type='checkbox' className={styles.autoCheckbox} checked disabled /> {t('Auto')}
</label>
)}
</>
);
};
2024-05-31 18:18:42 +02:00
const SortOptions = () => {
const { t } = useTranslation();
const { sortType, setSortType } = useSortingStore();
2024-05-31 18:18:42 +02:00
const handleSortChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
const type = event.target.value as 'active' | 'new';
setSortType(type);
};
return (
<>
2024-09-04 20:48:20 +02:00
<span>{t('sort_by')}</span>:&nbsp;
2024-06-22 10:32:19 +02:00
<select className='capitalize' value={sortType} onChange={handleSortChange}>
<option value='active'>{t('bump_order')}</option>
<option value='new'>{t('creation_date')}</option>
2024-06-01 18:13:41 +02:00
</select>
2024-05-31 18:18:42 +02:00
</>
);
};
const ImageSizeOptions = () => {
2024-06-22 10:32:19 +02:00
const { t } = useTranslation();
const { imageSize, setImageSize } = useCatalogStyleStore();
return (
<>
2024-09-04 20:48:20 +02:00
<span>{t('image_size')}:</span>&nbsp;
2024-06-22 10:32:19 +02:00
<select className='capitalize' value={imageSize} onChange={(e) => setImageSize(e.target.value as 'Small' | 'Large')}>
<option value='Small'>{t('small')}</option>
<option value='Large'>{t('large')}</option>
</select>
</>
);
};
const ShowOPCommentOption = () => {
2024-06-22 10:32:19 +02:00
const { t } = useTranslation();
const { showOPComment, setShowOPComment } = useCatalogStyleStore();
return (
<>
2024-09-04 20:48:20 +02:00
<span>{t('show_op_comment')}:</span>&nbsp;
2024-06-22 10:32:19 +02:00
<select className='capitalize' value={showOPComment ? 'On' : 'Off'} onChange={(e) => setShowOPComment(e.target.value === 'On')}>
<option value='Off'>{t('off')}</option>
<option value='On'>{t('on')}</option>
</select>
</>
);
};
2024-06-09 16:50:51 +02:00
export const TimeFilter = ({ isInAllView, isInCatalogView, isInSubscriptionsView, isTopbar = false }: BoardButtonsProps) => {
const { t } = useTranslation();
const navigate = useNavigate();
2024-10-29 17:40:09 +01:00
const { timeFilterName, timeFilterNames } = useTimeFilter();
const changeTimeFilter = (event: React.ChangeEvent<HTMLSelectElement>) => {
const timeFilterName = event.target.value;
2024-06-09 16:50:51 +02:00
const link = isInAllView
? isInCatalogView
? `/p/all/catalog/${timeFilterName}`
: `/p/all/${timeFilterName}`
: isInSubscriptionsView
? isInCatalogView
? `/p/subscriptions/catalog/${timeFilterName}`
: `/p/subscriptions/${timeFilterName}`
: null;
link && navigate(link);
};
const { sortType } = useSortingStore();
const allTimeFilterNames = timeFilterName ? Array.from(new Set([timeFilterName, ...timeFilterNames])) : timeFilterNames;
2024-10-29 17:40:09 +01:00
return (
<>
{!isTopbar ? (
<>
<span>{isInCatalogView ? (sortType === 'active' ? t('last_bumped') : t('newer_than')) : t('last_bumped')}</span>:&nbsp;
</>
) : (
<> </>
)}
2024-10-29 17:40:09 +01:00
<select onChange={changeTimeFilter} className={[styles.feedName, styles.menuItem, 'capitalize'].join(' ')} value={timeFilterName}>
{allTimeFilterNames.map((name, i) => (
<option key={name + i} value={name}>
{name}
</option>
))}
</select>
</>
);
};
export const MobileBoardButtons = () => {
2024-09-10 19:19:39 +02:00
const { t } = useTranslation();
2024-04-25 22:02:55 +02:00
const params = useParams();
const location = useLocation();
2024-10-29 17:40:09 +01:00
const isInAllView = isAllView(location.pathname);
const isInCatalogView = isCatalogView(location.pathname, params);
2024-04-25 22:02:55 +02:00
const isInPendingPostPage = isPendingPostView(location.pathname, params);
2024-05-17 17:25:25 +02:00
const isInPostView = isPostPageView(location.pathname, params);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, useParams());
2024-05-17 17:25:25 +02:00
const accountComment = useAccountComment({ commentIndex: params?.accountCommentIndex as any });
const subplebbitAddress = params?.subplebbitAddress || accountComment?.subplebbitAddress;
2024-04-25 22:02:55 +02:00
const { filteredCount, resetFilteredCount } = useCatalogFiltersStore();
useEffect(() => {
if (subplebbitAddress) {
resetFilteredCount();
}
}, [subplebbitAddress, resetFilteredCount]);
return (
2024-06-30 11:47:09 +02:00
<div className={`${styles.mobileBoardButtons} ${!isInCatalogView ? styles.addMargin : ''}`}>
{isInPostView || isInPendingPostPage ? (
<>
<ReturnButton address={subplebbitAddress} isInAllView={isInAllView} isInSubscriptionsView={isInSubscriptionsView} />
<CatalogButton address={subplebbitAddress} isInAllView={isInAllView} isInSubscriptionsView={isInSubscriptionsView} />
<SubscribeButton address={subplebbitAddress} />
<div className={styles.secondRow}>
<UpdateButton />
<AutoButton />
</div>
</>
2024-04-03 22:52:27 +02:00
) : (
<>
{isInCatalogView ? (
<ReturnButton address={subplebbitAddress} isInAllView={isInAllView} isInSubscriptionsView={isInSubscriptionsView} />
) : (
<CatalogButton address={subplebbitAddress} isInAllView={isInAllView} isInSubscriptionsView={isInSubscriptionsView} />
)}
{!(isInAllView || isInSubscriptionsView) && <SubscribeButton address={subplebbitAddress} />}
<RefreshButton />
{isInCatalogView && filteredCount > 0 && (
<span className={styles.filteredThreadsCount}>
{' '}
2024-09-10 19:19:39 +02:00
{t('filtered_threads')}: <strong>{filteredCount}</strong>
</span>
)}
2024-05-31 18:18:42 +02:00
{isInCatalogView && (
<>
<hr />
<div className={styles.options}>
<div>
<SortOptions /> <ImageSizeOptions />
</div>
<div className={styles.mobileCatalogOptionsPadding}>
<ShowOPCommentOption /> <CatalogFilters />
</div>
2024-05-31 18:18:42 +02:00
</div>
</>
)}
2024-04-03 22:52:27 +02:00
</>
)}
</div>
);
};
const PostPageStats = () => {
const { t } = useTranslation();
const params = useParams();
const location = useLocation();
const isInDescriptionView = isDescriptionView(location.pathname, params);
const comment = useComment({ commentCid: params?.commentCid });
2024-09-18 12:37:39 +02:00
const subplebbit = useSubplebbit({ subplebbitAddress: params?.subplebbitAddress });
const descriptionReplyCount = location?.pathname.startsWith('/p/all/') ? 0 : subplebbit?.rules?.length > 0 ? 1 : 0;
2024-07-07 14:36:14 +02:00
const { closed, pinned, replyCount } = comment || {};
const linkCount = useCountLinksInReplies(comment);
2024-09-18 12:37:39 +02:00
const displayReplyCount = replyCount !== undefined ? replyCount.toString() : isInDescriptionView ? descriptionReplyCount : '?';
2024-09-15 21:27:32 +02:00
const replyCountTooltip = replyCount !== undefined || isInDescriptionView ? _.capitalize(t('replies')) : t('loading');
return (
<span>
2024-09-15 21:27:32 +02:00
{(pinned || isInDescriptionView) && `${_.capitalize(t('sticky'))} / `}
{(closed || isInDescriptionView) && `${_.capitalize(t('closed'))} / `}
<Tooltip children={displayReplyCount} content={replyCountTooltip} /> / <Tooltip children={linkCount?.toString()} content={_.capitalize(t('links'))} />
</span>
);
};
export const DesktopBoardButtons = () => {
2024-09-10 19:19:39 +02:00
const { t } = useTranslation();
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;
const isInCatalogView = isCatalogView(location.pathname, params);
2024-10-29 17:40:09 +01: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);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, useParams());
2024-04-12 17:47:30 +02:00
const { filteredCount, resetFilteredCount } = useCatalogFiltersStore();
useEffect(() => {
if (subplebbitAddress) {
resetFilteredCount();
}
}, [subplebbitAddress, resetFilteredCount]);
return (
2024-06-01 18:13:41 +02:00
<>
<hr />
2024-06-01 18:13:41 +02:00
<div className={styles.desktopBoardButtons}>
{isInPostView || isInPendingPostPage ? (
<>
[
2024-05-31 18:18:42 +02:00
<ReturnButton address={subplebbitAddress} isInAllView={isInAllView} isInSubscriptionsView={isInSubscriptionsView} />
2024-06-01 18:13:41 +02:00
] [
<CatalogButton address={subplebbitAddress} isInAllView={isInAllView} isInSubscriptionsView={isInSubscriptionsView} />] [
<UpdateButton />
] [
<AutoButton />]
<span className={styles.rightSideButtons}>
<PostPageStats />
</span>
2024-06-01 18:13:41 +02:00
</>
) : (
<>
{isInCatalogView ? (
<>
[
<ReturnButton address={subplebbitAddress} isInAllView={isInAllView} isInSubscriptionsView={isInSubscriptionsView} />]{' '}
</>
) : (
<>
[
<CatalogButton address={subplebbitAddress} isInAllView={isInAllView} isInSubscriptionsView={isInSubscriptionsView} />]{' '}
2024-05-31 18:18:42 +02:00
</>
)}
[<RefreshButton />]
{isInCatalogView && filteredCount > 0 && (
<span className={styles.filteredThreadsCount}>
{' '}
2024-09-10 19:19:39 +02:00
{t('filtered_threads')}: <strong>{filteredCount}</strong>
</span>
)}
2024-06-01 18:13:41 +02:00
<span className={styles.rightSideButtons}>
{isInCatalogView && (
<>
<SortOptions />
<ImageSizeOptions />
<ShowOPCommentOption />
</>
)}
{(isInAllView || isInSubscriptionsView) && (
<TimeFilter isInAllView={isInAllView} isInCatalogView={isInCatalogView} isInSubscriptionsView={isInSubscriptionsView} />
)}
2024-09-13 12:01:56 +02:00
{isInCatalogView && (
2024-06-15 17:09:28 +02:00
<>
[
<CatalogFilters />]
</>
)}{' '}
2024-06-01 18:13:41 +02:00
{!(isInAllView || isInSubscriptionsView) && (
<>
[
<SubscribeButton address={subplebbitAddress} />]
</>
)}
</span>
</>
)}
</div>
</>
);
};