2024-10-12 15:35:27 +02:00
|
|
|
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
|
|
|
import { Link, useLocation, useParams } from 'react-router-dom';
|
|
|
|
|
import { Trans, useTranslation } from 'react-i18next';
|
2025-02-23 16:28:30 +01:00
|
|
|
import { Comment, useAccount, useFeed, useSubplebbit, useBlock, useAccountComments } from '@plebbit/plebbit-react-hooks';
|
2024-04-12 14:28:06 +02:00
|
|
|
import { Virtuoso, VirtuosoHandle, StateSnapshot } from 'react-virtuoso';
|
2024-10-12 15:35:27 +02:00
|
|
|
import { getCommentMediaInfo, getHasThumbnail } from '../../lib/utils/media-utils';
|
2024-05-17 16:39:02 +02:00
|
|
|
import { isAllView, isSubscriptionsView } from '../../lib/utils/view-utils';
|
2024-08-03 13:21:09 +02:00
|
|
|
import useCatalogFeedRows from '../../hooks/use-catalog-feed-rows';
|
2025-02-20 15:15:19 +01:00
|
|
|
import { useDefaultSubplebbits } from '../../hooks/use-default-subplebbits';
|
2025-02-20 15:34:30 +01:00
|
|
|
import useFeedStateString from '../../hooks/use-feed-state-string';
|
2024-06-09 16:40:32 +02:00
|
|
|
import useTimeFilter from '../../hooks/use-time-filter';
|
2024-04-12 14:28:06 +02:00
|
|
|
import useWindowWidth from '../../hooks/use-window-width';
|
2024-08-03 13:21:09 +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-10-12 15:35:27 +02:00
|
|
|
import useInterfaceSettingsStore from '../../stores/use-interface-settings-store';
|
2024-06-15 16:03:59 +02:00
|
|
|
import useSortingStore from '../../stores/use-sorting-store';
|
2024-04-12 14:28:06 +02:00
|
|
|
import CatalogRow from '../../components/catalog-row';
|
|
|
|
|
import LoadingEllipsis from '../../components/loading-ellipsis';
|
2024-05-09 16:04:24 +02:00
|
|
|
import SettingsModal from '../../components/settings-modal';
|
2024-04-12 14:28:06 +02:00
|
|
|
import styles from './catalog.module.css';
|
|
|
|
|
|
|
|
|
|
const lastVirtuosoStates: { [key: string]: StateSnapshot } = {};
|
|
|
|
|
|
2024-10-12 15:35:27 +02:00
|
|
|
const threadsWithoutImagesFilter = (comment: Comment) => {
|
|
|
|
|
if (!getHasThumbnail(getCommentMediaInfo(comment), comment?.link)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-12 14:28:06 +02:00
|
|
|
const Catalog = () => {
|
|
|
|
|
const { t } = useTranslation();
|
2024-05-09 16:04:24 +02:00
|
|
|
const location = useLocation();
|
2024-04-12 14:28:06 +02:00
|
|
|
const { subplebbitAddress } = useParams<{ subplebbitAddress: string }>();
|
2024-05-17 16:39:02 +02:00
|
|
|
|
2024-10-29 17:40:09 +01:00
|
|
|
const isInAllView = isAllView(location.pathname);
|
2024-06-15 16:42:30 +02:00
|
|
|
const defaultSubplebbits = useDefaultSubplebbits();
|
2024-09-12 21:22:22 +02:00
|
|
|
const { hideAdultBoards, hideGoreBoards } = useInterfaceSettingsStore();
|
2024-10-12 15:35:27 +02:00
|
|
|
const { hideThreadsWithoutImages } = useInterfaceSettingsStore();
|
2024-05-17 16:39:02 +02:00
|
|
|
|
|
|
|
|
const account = useAccount();
|
|
|
|
|
const subscriptions = account?.subscriptions;
|
2024-06-17 12:17:36 +02:00
|
|
|
const isInSubscriptionsView = isSubscriptionsView(location.pathname, useParams());
|
2024-05-17 16:39:02 +02:00
|
|
|
|
|
|
|
|
const subplebbitAddresses = useMemo(() => {
|
2024-06-15 16:42:30 +02:00
|
|
|
const filteredDefaultSubplebbits = defaultSubplebbits
|
|
|
|
|
.filter((subplebbit) => {
|
|
|
|
|
const { tags } = subplebbit;
|
|
|
|
|
const hasGoreTag = tags?.includes('gore');
|
|
|
|
|
const hasAdultTag = tags?.includes('adult');
|
|
|
|
|
|
2024-09-12 21:22:22 +02:00
|
|
|
if ((hasGoreTag && hideGoreBoards) || (hasAdultTag && hideAdultBoards)) {
|
2024-06-15 16:42:30 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
})
|
|
|
|
|
.map((subplebbit) => subplebbit.address);
|
|
|
|
|
|
2024-05-17 16:39:02 +02:00
|
|
|
if (isInAllView) {
|
2024-06-15 16:42:30 +02:00
|
|
|
return filteredDefaultSubplebbits;
|
2024-05-17 16:39:02 +02:00
|
|
|
}
|
|
|
|
|
if (isInSubscriptionsView) {
|
|
|
|
|
return subscriptions || [];
|
|
|
|
|
}
|
|
|
|
|
return [subplebbitAddress];
|
2024-09-12 21:22:22 +02:00
|
|
|
}, [isInAllView, isInSubscriptionsView, subplebbitAddress, defaultSubplebbits, subscriptions, hideAdultBoards, hideGoreBoards]);
|
2024-06-15 16:42:30 +02:00
|
|
|
|
2024-06-17 21:16:31 +02:00
|
|
|
const { imageSize } = useCatalogStyleStore();
|
|
|
|
|
const columnWidth = imageSize === 'Large' ? 270 : 180;
|
|
|
|
|
|
2024-04-12 14:28:06 +02:00
|
|
|
const columnCount = Math.floor(useWindowWidth() / columnWidth);
|
2024-09-08 14:47:52 +02:00
|
|
|
const postsPerPage = columnCount <= 2 ? 10 : columnCount === 3 ? 15 : columnCount === 4 ? 20 : 25;
|
2024-04-12 14:28:06 +02:00
|
|
|
|
2024-10-12 15:35:27 +02:00
|
|
|
const { timeFilterSeconds, timeFilterName } = useTimeFilter();
|
2024-06-15 16:03:59 +02:00
|
|
|
const { sortType } = useSortingStore();
|
2024-06-09 18:16:01 +02:00
|
|
|
|
2024-09-10 18:20:24 +02:00
|
|
|
const feedOptions = useMemo(() => {
|
|
|
|
|
const options: any = {
|
|
|
|
|
subplebbitAddresses,
|
|
|
|
|
sortType,
|
|
|
|
|
postsPerPage: isInAllView || isInSubscriptionsView ? 10 : postsPerPage,
|
2024-10-12 15:35:27 +02:00
|
|
|
filter: hideThreadsWithoutImages ? threadsWithoutImagesFilter : undefined,
|
2024-09-10 18:20:24 +02:00
|
|
|
};
|
2024-06-09 18:16:01 +02:00
|
|
|
|
2024-09-10 18:20:24 +02:00
|
|
|
if (isInAllView || isInSubscriptionsView) {
|
|
|
|
|
options.newerThan = timeFilterSeconds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return options;
|
2024-10-12 15:35:27 +02:00
|
|
|
}, [subplebbitAddresses, sortType, isInAllView, isInSubscriptionsView, postsPerPage, timeFilterSeconds, hideThreadsWithoutImages]);
|
2024-06-09 18:16:01 +02:00
|
|
|
|
2024-10-12 15:35:27 +02:00
|
|
|
const { feed, hasMore, loadMore, reset, subplebbitAddressesWithNewerPosts } = useFeed(feedOptions);
|
2025-02-23 16:28:30 +01:00
|
|
|
const { accountComments } = useAccountComments();
|
|
|
|
|
|
|
|
|
|
const resetTriggeredRef = useRef(false);
|
|
|
|
|
|
|
|
|
|
// show account comments instantly in the feed once published (cid defined), instead of waiting for the feed to update
|
|
|
|
|
const filteredComments = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
accountComments.filter((comment) => {
|
|
|
|
|
const { cid, deleted, postCid, removed, state, timestamp } = comment || {};
|
|
|
|
|
return (
|
|
|
|
|
!deleted &&
|
|
|
|
|
!removed &&
|
|
|
|
|
timestamp > Date.now() / 1000 - 60 * 60 &&
|
|
|
|
|
state === 'succeeded' &&
|
|
|
|
|
cid &&
|
|
|
|
|
(hideThreadsWithoutImages ? getHasThumbnail(getCommentMediaInfo(comment), comment?.link) : true) &&
|
|
|
|
|
cid === postCid &&
|
|
|
|
|
comment?.subplebbitAddress === subplebbitAddress &&
|
|
|
|
|
!feed.some((post) => post.cid === cid)
|
|
|
|
|
);
|
|
|
|
|
}),
|
|
|
|
|
[accountComments, subplebbitAddress, feed, hideThreadsWithoutImages],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// show newest account comment at the top of the feed but after pinned posts
|
|
|
|
|
const combinedFeed = useMemo(() => {
|
|
|
|
|
const newFeed = [...feed];
|
|
|
|
|
const lastPinnedIndex = newFeed.map((post) => post.pinned).lastIndexOf(true);
|
|
|
|
|
if (filteredComments.length > 0) {
|
|
|
|
|
newFeed.splice(lastPinnedIndex + 1, 0, ...filteredComments);
|
|
|
|
|
}
|
|
|
|
|
return newFeed;
|
|
|
|
|
}, [feed, filteredComments]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (filteredComments.length > 0 && !resetTriggeredRef.current) {
|
|
|
|
|
reset();
|
|
|
|
|
resetTriggeredRef.current = true;
|
|
|
|
|
}
|
|
|
|
|
}, [filteredComments, reset]);
|
2024-10-12 15:35:27 +02:00
|
|
|
|
|
|
|
|
const handleNewerPostsButtonClick = () => {
|
|
|
|
|
window.scrollTo({ top: 0, left: 0 });
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
reset();
|
|
|
|
|
}, 300);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// suggest the user to change time filter if there aren't enough posts
|
|
|
|
|
const { feed: weeklyFeed } = useFeed({
|
|
|
|
|
subplebbitAddresses,
|
|
|
|
|
sortType,
|
|
|
|
|
newerThan: 60 * 60 * 24 * 7,
|
|
|
|
|
filter: hideThreadsWithoutImages ? threadsWithoutImagesFilter : undefined,
|
|
|
|
|
});
|
|
|
|
|
const { feed: monthlyFeed } = useFeed({
|
|
|
|
|
subplebbitAddresses,
|
|
|
|
|
sortType,
|
|
|
|
|
newerThan: 60 * 60 * 24 * 30,
|
|
|
|
|
filter: hideThreadsWithoutImages ? threadsWithoutImagesFilter : undefined,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const [showMorePostsSuggestion, setShowMorePostsSuggestion] = useState(false);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const timer = setTimeout(() => {
|
|
|
|
|
setShowMorePostsSuggestion(true);
|
|
|
|
|
}, 5000);
|
|
|
|
|
|
|
|
|
|
return () => clearTimeout(timer);
|
|
|
|
|
}, []);
|
2024-05-31 10:41:44 +02:00
|
|
|
|
|
|
|
|
const setResetFunction = useFeedResetStore((state) => state.setResetFunction);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setResetFunction(reset);
|
|
|
|
|
}, [reset, setResetFunction]);
|
2024-04-12 14:28:06 +02:00
|
|
|
|
|
|
|
|
const subplebbit = useSubplebbit({ subplebbitAddress });
|
2024-06-27 14:13:46 +02:00
|
|
|
const { error, shortAddress, state, title } = subplebbit || {};
|
2024-10-12 15:35:27 +02:00
|
|
|
const { blocked, unblock } = useBlock({ address: subplebbitAddress });
|
2025-02-20 13:54:55 +01:00
|
|
|
|
|
|
|
|
const feedLength = feed.length;
|
|
|
|
|
const weeklyFeedLength = weeklyFeed.length;
|
|
|
|
|
const monthlyFeedLength = monthlyFeed.length;
|
|
|
|
|
const hasFeedLoaded = !!feed;
|
|
|
|
|
const loadingStateString =
|
2025-02-20 15:34:30 +01:00
|
|
|
useFeedStateString(subplebbitAddresses) || !hasFeedLoaded || (feedLength === 0 && !(weeklyFeedLength > feedLength || monthlyFeedLength > feedLength))
|
|
|
|
|
? t('loading_feed')
|
|
|
|
|
: t('looking_for_more_posts');
|
2024-10-12 15:35:27 +02:00
|
|
|
|
2024-05-17 17:25:25 +02:00
|
|
|
const loadingString = (
|
|
|
|
|
<div className={styles.stateString}>
|
|
|
|
|
{state === 'failed' ? (
|
2024-10-12 15:35:27 +02:00
|
|
|
<span className='red'>{state}</span>
|
2024-05-17 17:25:25 +02:00
|
|
|
) : isInSubscriptionsView && subscriptions?.length === 0 ? (
|
2024-05-31 17:29:19 +02:00
|
|
|
t('not_subscribed_to_any_board')
|
2024-10-12 15:35:27 +02:00
|
|
|
) : blocked ? (
|
2025-02-11 12:27:03 +01:00
|
|
|
t('you_have_blocked_this_board')
|
2025-02-23 16:30:13 +01:00
|
|
|
) : !hasMore && combinedFeed.length === 0 ? (
|
2024-12-21 00:55:18 +01:00
|
|
|
t('no_threads')
|
2024-05-17 17:25:25 +02:00
|
|
|
) : (
|
2024-10-12 15:35:27 +02:00
|
|
|
hasMore && <LoadingEllipsis string={loadingStateString} />
|
2024-05-17 17:25:25 +02:00
|
|
|
)}
|
2024-06-27 14:13:46 +02:00
|
|
|
{error && (
|
2024-10-12 15:35:27 +02:00
|
|
|
<div className='red'>
|
2024-06-27 14:13:46 +02:00
|
|
|
<br />
|
|
|
|
|
{error.message}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2024-05-17 17:25:25 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
2024-04-12 14:28:06 +02:00
|
|
|
|
2024-10-29 17:40:09 +01:00
|
|
|
const params = useParams<{ sortType?: string; timeFilterName?: string }>();
|
2024-10-12 15:35:27 +02:00
|
|
|
const currentTimeFilterName = params?.timeFilterName || timeFilterName;
|
2024-06-27 14:18:10 +02:00
|
|
|
|
2024-04-12 14:28:06 +02:00
|
|
|
const Footer = () => {
|
|
|
|
|
let footerContent;
|
2024-05-31 09:38:59 +02:00
|
|
|
if (feed.length === 0) {
|
2024-06-27 14:18:10 +02:00
|
|
|
if (blocked) {
|
2025-02-11 12:27:03 +01:00
|
|
|
footerContent = t('you_have_blocked_this_board');
|
2025-02-23 16:30:13 +01:00
|
|
|
} else if (combinedFeed.length === 0) {
|
2024-12-21 00:55:18 +01:00
|
|
|
footerContent = t('no_threads');
|
2024-06-27 14:18:10 +02:00
|
|
|
}
|
2024-04-12 14:28:06 +02:00
|
|
|
}
|
2024-10-12 15:35:27 +02:00
|
|
|
if (hasMore || (subplebbitAddresses && subplebbitAddresses.length === 0)) {
|
|
|
|
|
footerContent = (
|
|
|
|
|
<>
|
|
|
|
|
{subplebbitAddressesWithNewerPosts.length > 0 ? (
|
|
|
|
|
<div className={styles.stateString}>
|
|
|
|
|
<Trans
|
2024-12-21 00:55:18 +01:00
|
|
|
i18nKey='newer_threads_available'
|
2024-10-12 15:35:27 +02:00
|
|
|
components={{
|
2024-10-15 13:10:47 +02:00
|
|
|
1: <span className={styles.newerPostsButton} onClick={handleNewerPostsButtonClick} />,
|
2024-10-12 15:35:27 +02:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
(isInAllView || isInSubscriptionsView) &&
|
|
|
|
|
showMorePostsSuggestion &&
|
|
|
|
|
monthlyFeed.length > feed.length &&
|
|
|
|
|
(weeklyFeed.length > feed.length ? (
|
|
|
|
|
<div className={styles.stateString}>
|
|
|
|
|
<Trans
|
2024-12-21 00:55:18 +01:00
|
|
|
i18nKey='more_threads_last_week'
|
2025-02-20 13:54:55 +01:00
|
|
|
values={{ currentTimeFilterName, count: feed.length }}
|
2024-10-12 15:35:27 +02:00
|
|
|
components={{
|
|
|
|
|
1: <Link to={(isInAllView ? '/p/all/catalog' : isInSubscriptionsView ? '/p/subscriptions/catalog' : `/p/${subplebbitAddress}/catalog`) + '/1w'} />,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className={styles.stateString}>
|
|
|
|
|
<Trans
|
2024-12-21 00:55:18 +01:00
|
|
|
i18nKey='more_threads_last_month'
|
2025-02-20 13:54:55 +01:00
|
|
|
values={{ currentTimeFilterName, count: feed.length }}
|
2024-10-12 15:35:27 +02:00
|
|
|
components={{
|
|
|
|
|
1: <Link to={(isInAllView ? '/p/all/catalog' : isInSubscriptionsView ? '/p/subscriptions/catalog' : `/p/${subplebbitAddress}/catalog`) + '/1m'} />,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
))
|
|
|
|
|
)}
|
|
|
|
|
<div className={styles.stateString}>
|
|
|
|
|
<LoadingEllipsis string={loadingStateString} />
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
);
|
2024-04-12 14:28:06 +02:00
|
|
|
}
|
2024-10-12 15:35:27 +02:00
|
|
|
return <div className={styles.footer}>{footerContent}</div>;
|
2024-04-12 14:28:06 +02:00
|
|
|
};
|
|
|
|
|
|
2024-05-31 09:38:59 +02:00
|
|
|
const isFeedLoaded = feed.length > 0 || state === 'failed';
|
2024-04-14 16:45:59 +02:00
|
|
|
|
2025-02-23 16:28:30 +01:00
|
|
|
const rows = useCatalogFeedRows(columnCount, combinedFeed, isFeedLoaded, subplebbit);
|
2024-04-12 14:28:06 +02:00
|
|
|
|
|
|
|
|
// save the last Virtuoso state to restore it when navigating back
|
|
|
|
|
const virtuosoRef = useRef<VirtuosoHandle | null>(null);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const setLastVirtuosoState = () =>
|
|
|
|
|
virtuosoRef.current?.getState((snapshot: StateSnapshot) => {
|
|
|
|
|
if (snapshot?.ranges?.length) {
|
2024-06-09 16:40:32 +02:00
|
|
|
lastVirtuosoStates[sortType + timeFilterSeconds + 'catalog'] = snapshot;
|
2024-04-12 14:28:06 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
window.addEventListener('scroll', setLastVirtuosoState);
|
|
|
|
|
return () => window.removeEventListener('scroll', setLastVirtuosoState);
|
2024-06-09 16:40:32 +02:00
|
|
|
}, [sortType, timeFilterSeconds]);
|
2024-04-12 14:28:06 +02:00
|
|
|
|
2024-06-09 16:40:32 +02:00
|
|
|
const lastVirtuosoState = lastVirtuosoStates?.[sortType + timeFilterSeconds + 'catalog'];
|
2024-04-12 14:28:06 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let documentTitle = title ? title : shortAddress;
|
2024-05-31 17:33:49 +02:00
|
|
|
if (isInAllView) documentTitle = t('all');
|
|
|
|
|
else if (isInSubscriptionsView) documentTitle = t('subscriptions');
|
2024-06-28 17:14:57 +02:00
|
|
|
document.title = documentTitle + ` - ${t('catalog')} - plebchan`;
|
2024-05-31 20:14:32 +02:00
|
|
|
}, [title, shortAddress, isInAllView, isInSubscriptionsView, t]);
|
2024-04-12 14:28:06 +02:00
|
|
|
|
|
|
|
|
return (
|
2024-04-12 17:47:30 +02:00
|
|
|
<div className={styles.content}>
|
2024-05-17 16:28:09 +02:00
|
|
|
{location.pathname.endsWith('/settings') && <SettingsModal />}
|
2024-04-12 17:47:30 +02:00
|
|
|
<hr />
|
|
|
|
|
<div className={styles.catalog}>
|
2025-02-23 16:28:30 +01:00
|
|
|
{combinedFeed.length !== 0 ? (
|
2024-10-12 15:35:27 +02:00
|
|
|
<>
|
|
|
|
|
<Virtuoso
|
|
|
|
|
increaseViewportBy={{ bottom: 1200, top: 1200 }}
|
|
|
|
|
totalCount={rows?.length || 0}
|
|
|
|
|
data={rows}
|
|
|
|
|
itemContent={(index, row) => <CatalogRow index={index} row={row} />}
|
|
|
|
|
useWindowScroll={true}
|
|
|
|
|
components={{ Footer }}
|
|
|
|
|
endReached={loadMore}
|
|
|
|
|
ref={virtuosoRef}
|
|
|
|
|
restoreStateFrom={lastVirtuosoState}
|
|
|
|
|
initialScrollTop={lastVirtuosoState?.scrollTop}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<div className={styles.footer}>
|
|
|
|
|
{loadingString}
|
|
|
|
|
{blocked && (
|
|
|
|
|
<>
|
|
|
|
|
[
|
|
|
|
|
<span
|
|
|
|
|
className={styles.button}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
unblock();
|
|
|
|
|
reset();
|
|
|
|
|
}}
|
|
|
|
|
>
|
2025-02-11 12:27:03 +01:00
|
|
|
{t('unblock')}
|
2024-10-12 15:35:27 +02:00
|
|
|
</span>
|
|
|
|
|
]
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2024-04-12 17:47:30 +02:00
|
|
|
</div>
|
2024-04-12 14:28:06 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Catalog;
|