2024-04-03 22:52:27 +02:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-06-09 16:40:32 +02:00
|
|
|
import { Link, useLocation, useNavigate, useParams } from 'react-router-dom';
|
2024-04-25 22:02:55 +02:00
|
|
|
import { useAccountComment, useSubscribe } from '@plebbit/plebbit-react-hooks';
|
2024-05-17 15:06:30 +02:00
|
|
|
import { isAllView, isCatalogView, isPendingPostView, isPostPageView, isSubscriptionsView } from '../../lib/utils/view-utils';
|
2024-06-15 16:42:30 +02:00
|
|
|
import useSortingStore from '../../stores/use-sorting-store';
|
2024-05-31 10:41:44 +02:00
|
|
|
import useFeedResetStore from '../../stores/use-feed-reset-store';
|
2024-06-09 16:40:32 +02:00
|
|
|
import useTimeFilter from '../../hooks/use-time-filter';
|
2024-06-15 16:42:30 +02:00
|
|
|
import CatalogFilters from '../../views/catalog/catalog-filters/';
|
2024-05-31 10:41:44 +02:00
|
|
|
import styles from './board-buttons.module.css';
|
2024-03-22 12:09:17 +01:00
|
|
|
|
|
|
|
|
interface BoardButtonsProps {
|
2024-05-30 18:01:37 +02:00
|
|
|
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;
|
2024-03-22 12:09:17 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-31 15:16:21 +02:00
|
|
|
const CatalogButton = ({ address, isInAllView, isInSubscriptionsView }: BoardButtonsProps) => {
|
2024-03-22 12:22:20 +01:00
|
|
|
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>
|
|
|
|
|
);
|
2024-03-22 12:09:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const SubscribeButton = ({ address }: BoardButtonsProps) => {
|
2024-03-22 12:22:20 +01:00
|
|
|
const { t } = useTranslation();
|
2024-03-22 12:09:17 +01:00
|
|
|
const { subscribed, subscribe, unsubscribe } = useSubscribe({ subplebbitAddress: address });
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<button className='button' onClick={subscribed ? unsubscribe : subscribe}>
|
2024-03-22 12:22:20 +01:00
|
|
|
{subscribed ? t('unsubscribe') : t('subscribe')}
|
2024-03-22 12:09:17 +01:00
|
|
|
</button>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-31 17:10:55 +02:00
|
|
|
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>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-31 15:16:21 +02:00
|
|
|
const RefreshButton = () => {
|
2024-05-31 17:09:20 +02:00
|
|
|
const { t } = useTranslation();
|
2024-05-31 15:16:21 +02:00
|
|
|
const reset = useFeedResetStore((state) => state.reset);
|
2024-04-03 22:52:27 +02:00
|
|
|
return (
|
2024-05-31 15:16:21 +02:00
|
|
|
<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>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-31 18:18:42 +02:00
|
|
|
const SortOptions = () => {
|
2024-06-09 16:40:32 +02:00
|
|
|
const { t } = useTranslation();
|
2024-06-15 16:42:30 +02:00
|
|
|
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-06-11 15:13:04 +02:00
|
|
|
<span className='capitalize'>{t('sort_by')}</span>:
|
2024-05-31 18:18:42 +02:00
|
|
|
<select value={sortType} onChange={handleSortChange}>
|
2024-06-09 16:40:32 +02:00
|
|
|
<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
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-09 16:50:51 +02:00
|
|
|
export const TimeFilter = ({ isInAllView, isInCatalogView, isInSubscriptionsView, isTopbar = false }: BoardButtonsProps) => {
|
2024-06-09 16:40:32 +02:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const params = useParams();
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const { timeFilterNames } = useTimeFilter();
|
2024-06-11 21:42:07 +02:00
|
|
|
const selectedTimeFilterName = params.timeFilterName;
|
2024-06-09 16:40:32 +02:00
|
|
|
|
|
|
|
|
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);
|
2024-06-09 16:40:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{!isTopbar ? (
|
|
|
|
|
<>
|
2024-06-11 10:29:01 +02:00
|
|
|
<span className='capitalize'>{t('newer_than')}</span>:
|
2024-06-09 16:40:32 +02:00
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<> </>
|
|
|
|
|
)}
|
|
|
|
|
<select onChange={changeTimeFilter} className={[styles.feedName, styles.menuItem].join(' ')} value={selectedTimeFilterName}>
|
|
|
|
|
{timeFilterNames.map((timeFilterName, i) => (
|
|
|
|
|
<option key={timeFilterName + i} value={timeFilterName}>
|
|
|
|
|
{timeFilterName}
|
|
|
|
|
</option>
|
|
|
|
|
))}
|
|
|
|
|
</select>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-08 17:13:02 +02:00
|
|
|
export const MobileBoardButtons = () => {
|
2024-04-25 22:02:55 +02:00
|
|
|
const params = useParams();
|
|
|
|
|
const location = useLocation();
|
2024-05-17 17:25:25 +02:00
|
|
|
const isInAllView = isAllView(location.pathname);
|
2024-06-09 16:40:32 +02:00
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
const accountComment = useAccountComment({ commentIndex: params?.accountCommentIndex as any });
|
|
|
|
|
const subplebbitAddress = params?.subplebbitAddress || accountComment?.subplebbitAddress;
|
2024-04-25 22:02:55 +02:00
|
|
|
|
2024-03-22 12:09:17 +01:00
|
|
|
return (
|
|
|
|
|
<div className={styles.mobileBoardButtons}>
|
2024-05-17 15:29:03 +02:00
|
|
|
{isInPostView || isInPendingPostPage ? (
|
2024-05-31 15:16:21 +02:00
|
|
|
<>
|
2024-06-03 20:48:44 +02:00
|
|
|
<ReturnButton address={subplebbitAddress} isInAllView={isInAllView} isInSubscriptionsView={isInSubscriptionsView} />
|
2024-05-31 15:16:21 +02:00
|
|
|
<CatalogButton address={subplebbitAddress} isInAllView={isInAllView} isInSubscriptionsView={isInSubscriptionsView} />
|
|
|
|
|
<SubscribeButton address={subplebbitAddress} />
|
|
|
|
|
</>
|
2024-04-03 22:52:27 +02:00
|
|
|
) : (
|
|
|
|
|
<>
|
2024-05-31 15:16:21 +02:00
|
|
|
{isInCatalogView ? (
|
2024-06-03 20:48:44 +02:00
|
|
|
<ReturnButton address={subplebbitAddress} isInAllView={isInAllView} isInSubscriptionsView={isInSubscriptionsView} />
|
2024-05-31 15:16:21 +02:00
|
|
|
) : (
|
|
|
|
|
<CatalogButton address={subplebbitAddress} isInAllView={isInAllView} isInSubscriptionsView={isInSubscriptionsView} />
|
|
|
|
|
)}
|
2024-06-03 20:48:44 +02:00
|
|
|
{!(isInAllView || isInSubscriptionsView) && <SubscribeButton address={subplebbitAddress} />}
|
2024-05-31 15:16:21 +02:00
|
|
|
<RefreshButton />
|
2024-05-31 18:18:42 +02:00
|
|
|
{isInCatalogView && (
|
|
|
|
|
<>
|
|
|
|
|
<hr />
|
|
|
|
|
<div className={styles.options}>
|
2024-06-15 17:09:28 +02:00
|
|
|
<SortOptions /> <CatalogFilters />
|
2024-05-31 18:18:42 +02:00
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
2024-04-03 22:52:27 +02:00
|
|
|
</>
|
|
|
|
|
)}
|
2024-03-22 12:09:17 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-08 17:13:02 +02:00
|
|
|
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-06-09 16:40:32 +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);
|
2024-05-17 15:29:03 +02:00
|
|
|
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
|
|
|
|
2024-03-22 12:09:17 +01:00
|
|
|
return (
|
2024-06-01 18:13:41 +02:00
|
|
|
<>
|
2024-03-22 13:03:04 +01: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
|
|
|
] [
|
2024-06-03 19:46:30 +02:00
|
|
|
<CatalogButton address={subplebbitAddress} isInAllView={isInAllView} isInSubscriptionsView={isInSubscriptionsView} />]
|
2024-06-01 18:13:41 +02:00
|
|
|
{isInPostView && (
|
|
|
|
|
<span className={styles.rightSideButtons}>
|
2024-05-31 18:18:42 +02:00
|
|
|
[
|
|
|
|
|
<SubscribeButton address={subplebbitAddress} />]
|
2024-06-01 18:13:41 +02:00
|
|
|
</span>
|
|
|
|
|
)}
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
{isInCatalogView ? (
|
|
|
|
|
<>
|
|
|
|
|
[
|
|
|
|
|
<ReturnButton address={subplebbitAddress} isInAllView={isInAllView} isInSubscriptionsView={isInSubscriptionsView} />]{' '}
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
[
|
|
|
|
|
<CatalogButton address={subplebbitAddress} isInAllView={isInAllView} isInSubscriptionsView={isInSubscriptionsView} />]{' '}
|
2024-05-31 18:18:42 +02:00
|
|
|
</>
|
|
|
|
|
)}
|
2024-06-03 19:46:30 +02:00
|
|
|
[<RefreshButton />]
|
2024-06-01 18:13:41 +02:00
|
|
|
<span className={styles.rightSideButtons}>
|
|
|
|
|
{isInCatalogView && <SortOptions />}
|
2024-06-14 21:56:23 +02:00
|
|
|
{(isInAllView || isInSubscriptionsView) && (
|
|
|
|
|
<TimeFilter isInAllView={isInAllView} isInCatalogView={isInCatalogView} isInSubscriptionsView={isInSubscriptionsView} />
|
|
|
|
|
)}
|
2024-06-15 17:09:28 +02:00
|
|
|
{(isInCatalogView || isInAllView || isInSubscriptionsView) && (
|
|
|
|
|
<>
|
|
|
|
|
[
|
|
|
|
|
<CatalogFilters />]
|
|
|
|
|
</>
|
|
|
|
|
)}{' '}
|
2024-06-01 18:13:41 +02:00
|
|
|
{!(isInAllView || isInSubscriptionsView) && (
|
|
|
|
|
<>
|
|
|
|
|
[
|
|
|
|
|
<SubscribeButton address={subplebbitAddress} />]
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</span>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
2024-03-22 12:09:17 +01:00
|
|
|
);
|
|
|
|
|
};
|