2025-03-06 00:07:08 +01:00
|
|
|
import { useEffect, useMemo, useRef, useState, useCallback } from 'react';
|
2025-12-08 13:03:25 +01:00
|
|
|
import { Link, useLocation, useNavigationType, useParams } from 'react-router-dom';
|
2024-10-12 15:35:27 +02:00
|
|
|
import { Trans, useTranslation } from 'react-i18next';
|
2026-02-19 15:30:55 +08:00
|
|
|
import { Comment, useAccount, useFeed, useSubplebbit, 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-08-03 13:21:09 +02:00
|
|
|
import useCatalogFeedRows from '../../hooks/use-catalog-feed-rows';
|
2026-01-28 16:51:27 +08:00
|
|
|
import { useDirectories } from '../../hooks/use-directories';
|
|
|
|
|
import { useFilteredDirectoryAddresses } from '../../hooks/use-filtered-directory-addresses';
|
2025-11-07 12:50:58 +01:00
|
|
|
import { useResolvedSubplebbitAddress, useBoardPath } from '../../hooks/use-resolved-subplebbit-address';
|
2025-02-28 23:36:33 +01:00
|
|
|
import { useFeedStateString } from '../../hooks/use-state-string';
|
2025-12-08 23:32:42 +01:00
|
|
|
import useTimeFilter, { timeFilterNameToSeconds } 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-06-15 16:03:59 +02:00
|
|
|
import useSortingStore from '../../stores/use-sorting-store';
|
2025-03-06 00:07:08 +01:00
|
|
|
import useCatalogFiltersStore from '../../stores/use-catalog-filters-store';
|
2025-12-28 23:13:25 +01:00
|
|
|
import { getSubplebbitAddress, isDirectoryBoard } from '../../lib/utils/route-utils';
|
2024-04-12 14:28:06 +02:00
|
|
|
import CatalogRow from '../../components/catalog-row';
|
|
|
|
|
import LoadingEllipsis from '../../components/loading-ellipsis';
|
2025-12-31 16:00:58 +01:00
|
|
|
import ErrorDisplay from '../../components/error-display/error-display';
|
2024-04-12 14:28:06 +02:00
|
|
|
import styles from './catalog.module.css';
|
2025-03-06 23:48:17 +01:00
|
|
|
import { commentMatchesPattern } from '../../lib/utils/pattern-utils';
|
2024-04-12 14:28:06 +02:00
|
|
|
|
|
|
|
|
const lastVirtuosoStates: { [key: string]: StateSnapshot } = {};
|
|
|
|
|
|
2026-01-13 17:21:30 +01:00
|
|
|
interface CatalogFooterProps {
|
|
|
|
|
subplebbitAddresses: string[];
|
|
|
|
|
hasMore: boolean;
|
|
|
|
|
feedLength: number;
|
|
|
|
|
combinedFeedLength: number;
|
|
|
|
|
subplebbitAddressesWithNewerPosts: string[];
|
|
|
|
|
onNewerPostsClick: () => void;
|
|
|
|
|
isInAllView: boolean;
|
|
|
|
|
isInSubscriptionsView: boolean;
|
|
|
|
|
showMorePostsSuggestion: boolean;
|
|
|
|
|
weeklyFeedLength: number;
|
|
|
|
|
monthlyFeedLength: number;
|
|
|
|
|
yearlyFeedLength: number;
|
|
|
|
|
boardPath: string | undefined;
|
|
|
|
|
currentTimeFilterName: string | undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Defined outside Catalog to preserve component identity across renders (Virtuoso optimization)
|
|
|
|
|
// The useFeedStateString hook is called here instead of in Catalog to isolate re-renders
|
|
|
|
|
// caused by backend IPFS state changes to just this footer component
|
|
|
|
|
const CatalogFooter = ({
|
|
|
|
|
subplebbitAddresses,
|
|
|
|
|
hasMore,
|
|
|
|
|
feedLength,
|
|
|
|
|
combinedFeedLength,
|
|
|
|
|
subplebbitAddressesWithNewerPosts,
|
|
|
|
|
onNewerPostsClick,
|
|
|
|
|
isInAllView,
|
|
|
|
|
isInSubscriptionsView,
|
|
|
|
|
showMorePostsSuggestion,
|
|
|
|
|
weeklyFeedLength,
|
|
|
|
|
monthlyFeedLength,
|
|
|
|
|
yearlyFeedLength,
|
|
|
|
|
boardPath,
|
|
|
|
|
currentTimeFilterName,
|
|
|
|
|
}: CatalogFooterProps) => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
|
|
const loadingStateString =
|
|
|
|
|
useFeedStateString(subplebbitAddresses) ||
|
|
|
|
|
(feedLength === 0 && !(weeklyFeedLength > feedLength || monthlyFeedLength > feedLength || yearlyFeedLength > monthlyFeedLength))
|
|
|
|
|
? t('loading_feed')
|
|
|
|
|
: t('looking_for_more_posts');
|
|
|
|
|
|
|
|
|
|
let footerContent;
|
|
|
|
|
if (feedLength === 0) {
|
|
|
|
|
if (combinedFeedLength === 0) {
|
|
|
|
|
footerContent = t('no_threads');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (hasMore || (subplebbitAddresses && subplebbitAddresses.length === 0)) {
|
|
|
|
|
footerContent = (
|
|
|
|
|
<>
|
|
|
|
|
{subplebbitAddressesWithNewerPosts.length > 0 ? (
|
|
|
|
|
<div className={styles.stateString}>
|
|
|
|
|
<Trans
|
|
|
|
|
i18nKey='newer_threads_available'
|
|
|
|
|
components={{
|
|
|
|
|
1: <span className={styles.newerPostsButton} onClick={onNewerPostsClick} />,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
(isInAllView || isInSubscriptionsView) &&
|
|
|
|
|
showMorePostsSuggestion &&
|
|
|
|
|
(monthlyFeedLength > feedLength || yearlyFeedLength > monthlyFeedLength) &&
|
|
|
|
|
(weeklyFeedLength > feedLength ? (
|
|
|
|
|
<div className={styles.stateString}>
|
|
|
|
|
<Trans
|
|
|
|
|
i18nKey='more_threads_last_week'
|
|
|
|
|
values={{ currentTimeFilterName, count: feedLength }}
|
|
|
|
|
components={{
|
|
|
|
|
1: <Link to={(isInAllView ? '/all/catalog' : isInSubscriptionsView ? '/subs/catalog' : `/${boardPath}/catalog`) + '/1w'} />,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
) : monthlyFeedLength > feedLength ? (
|
|
|
|
|
<div className={styles.stateString}>
|
|
|
|
|
<Trans
|
|
|
|
|
i18nKey='more_threads_last_month'
|
|
|
|
|
values={{ currentTimeFilterName, count: feedLength }}
|
|
|
|
|
components={{
|
|
|
|
|
1: <Link to={(isInAllView ? '/all/catalog' : isInSubscriptionsView ? '/subs/catalog' : `/${boardPath}/catalog`) + '/1m'} />,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className={styles.stateString}>
|
|
|
|
|
<Trans
|
|
|
|
|
i18nKey='more_threads_last_year'
|
|
|
|
|
values={{ currentTimeFilterName, count: feedLength }}
|
|
|
|
|
components={{
|
|
|
|
|
1: <Link to={(isInAllView ? '/all/catalog' : isInSubscriptionsView ? '/subs/catalog' : `/${boardPath}/catalog`) + '/1y'} />,
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
))
|
|
|
|
|
)}
|
|
|
|
|
<div className={styles.stateString}>
|
|
|
|
|
<LoadingEllipsis string={loadingStateString} />
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return <div className={styles.footer}>{footerContent}</div>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Separate component for the loading state when there's no feed
|
|
|
|
|
// This also calls useFeedStateString internally to isolate re-renders
|
|
|
|
|
interface CatalogLoadingProps {
|
|
|
|
|
subplebbitAddresses: string[];
|
|
|
|
|
hasMore: boolean;
|
|
|
|
|
feedLength: number;
|
|
|
|
|
weeklyFeedLength: number;
|
|
|
|
|
monthlyFeedLength: number;
|
|
|
|
|
yearlyFeedLength: number;
|
|
|
|
|
state: string | undefined;
|
|
|
|
|
subscriptionsLength: number;
|
|
|
|
|
combinedFeedLength: number;
|
|
|
|
|
error: Error | undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const CatalogLoading = ({
|
|
|
|
|
subplebbitAddresses,
|
|
|
|
|
hasMore,
|
|
|
|
|
feedLength,
|
|
|
|
|
weeklyFeedLength,
|
|
|
|
|
monthlyFeedLength,
|
|
|
|
|
yearlyFeedLength,
|
|
|
|
|
state,
|
|
|
|
|
subscriptionsLength,
|
|
|
|
|
combinedFeedLength,
|
|
|
|
|
error,
|
|
|
|
|
}: CatalogLoadingProps) => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
|
|
const rawFeedStateString = useFeedStateString(subplebbitAddresses);
|
|
|
|
|
const loadingStateString =
|
|
|
|
|
rawFeedStateString || (feedLength === 0 && !(weeklyFeedLength > feedLength || monthlyFeedLength > feedLength || yearlyFeedLength > monthlyFeedLength))
|
|
|
|
|
? t('loading_feed')
|
|
|
|
|
: t('looking_for_more_posts');
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={styles.stateString}>
|
|
|
|
|
{state === 'failed' ? (
|
|
|
|
|
<span className='red'>{state}</span>
|
|
|
|
|
) : subscriptionsLength === 0 ? (
|
|
|
|
|
<span className='red'>{t('not_subscribed_to_any_board')}</span>
|
|
|
|
|
) : !hasMore && combinedFeedLength === 0 ? (
|
|
|
|
|
t('no_threads')
|
|
|
|
|
) : (
|
|
|
|
|
hasMore && <LoadingEllipsis string={loadingStateString} />
|
|
|
|
|
)}
|
|
|
|
|
<ErrorDisplay error={error} />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-06 00:07:08 +01:00
|
|
|
const createContentFilter = (
|
2025-03-07 17:43:09 +01:00
|
|
|
filterItems: { text: string; enabled: boolean; count: number; filteredCids: Set<string>; hide: boolean; top: boolean; color?: string }[],
|
2025-03-06 15:44:26 +01:00
|
|
|
subplebbitAddress: string,
|
|
|
|
|
onFilterMatch?: (filterIndex: number, cid: string, subplebbitAddress: string) => void,
|
2025-03-06 00:07:08 +01:00
|
|
|
) => {
|
|
|
|
|
// Create a unique key based on the enabled filter items
|
|
|
|
|
const enabledFilters = filterItems.filter((item) => item.enabled && item.text.trim() !== '');
|
|
|
|
|
const filterKey =
|
|
|
|
|
enabledFilters.length > 0
|
|
|
|
|
? `content-filter-${enabledFilters.map((item) => `${item.text}-${item.hide ? 'hide' : ''}-${item.top ? 'top' : ''}`).join('-')}`
|
|
|
|
|
: 'no-content-filter';
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
filter: (comment: Comment) => {
|
|
|
|
|
if (!comment?.cid) return true;
|
|
|
|
|
|
|
|
|
|
if (enabledFilters.length === 0) return true;
|
|
|
|
|
|
|
|
|
|
// Check if any enabled filter matches the content
|
|
|
|
|
for (let i = 0; i < enabledFilters.length; i++) {
|
|
|
|
|
const item = enabledFilters[i];
|
2025-03-06 23:48:17 +01:00
|
|
|
const pattern = item.text;
|
2025-03-06 00:07:08 +01:00
|
|
|
|
2025-03-06 23:48:17 +01:00
|
|
|
if (commentMatchesPattern(comment, pattern)) {
|
2025-03-06 00:07:08 +01:00
|
|
|
// Find the original filter index to increment count
|
|
|
|
|
const filterIndex = filterItems.findIndex((f) => f.text === item.text && f.enabled);
|
|
|
|
|
if (filterIndex !== -1) {
|
|
|
|
|
if (onFilterMatch) {
|
2025-03-06 15:44:26 +01:00
|
|
|
onFilterMatch(filterIndex, comment.cid, subplebbitAddress);
|
2025-03-06 00:07:08 +01:00
|
|
|
} else {
|
|
|
|
|
// Fallback to the store method if no callback provided
|
2025-03-06 15:44:26 +01:00
|
|
|
useCatalogFiltersStore.getState().incrementFilterCount(filterIndex, comment.cid, subplebbitAddress);
|
2025-03-06 00:07:08 +01:00
|
|
|
}
|
2025-03-07 17:43:09 +01:00
|
|
|
|
|
|
|
|
// If the filter has a color, track it in the matchedFilters map
|
|
|
|
|
if (item.color) {
|
|
|
|
|
useCatalogFiltersStore.getState().setMatchedFilter(comment.cid, item.color);
|
|
|
|
|
}
|
2025-03-06 00:07:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If this filter is set to hide, filter out the comment
|
|
|
|
|
if (item.hide) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If this filter is set to top, we'll handle it separately in the component
|
|
|
|
|
// (we don't filter it out here)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
},
|
|
|
|
|
key: filterKey,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const createImageFilter = (showTextOnlyThreads: boolean) => {
|
|
|
|
|
return {
|
|
|
|
|
filter: (comment: Comment) => {
|
|
|
|
|
if (showTextOnlyThreads) return true;
|
|
|
|
|
|
|
|
|
|
const { link, linkHeight, linkWidth, thumbnailUrl } = comment || {};
|
|
|
|
|
const hasThumbnail = getHasThumbnail(getCommentMediaInfo(link, thumbnailUrl, linkWidth, linkHeight), link);
|
|
|
|
|
|
|
|
|
|
return hasThumbnail;
|
|
|
|
|
},
|
|
|
|
|
key: showTextOnlyThreads ? 'no-image-filter' : 'threads-with-images-only',
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const createCombinedFilter = (
|
|
|
|
|
showTextOnlyThreads: boolean,
|
2025-03-07 17:43:09 +01:00
|
|
|
filterItems: { text: string; enabled: boolean; count: number; filteredCids: Set<string>; hide: boolean; top: boolean; color?: string }[],
|
2025-03-06 16:27:09 +01:00
|
|
|
searchText: string,
|
2025-03-06 15:44:26 +01:00
|
|
|
subplebbitAddress: string,
|
|
|
|
|
onFilterMatch?: (filterIndex: number, cid: string, subplebbitAddress: string) => void,
|
2025-03-06 00:07:08 +01:00
|
|
|
) => {
|
|
|
|
|
const imageFilter = createImageFilter(showTextOnlyThreads);
|
2025-03-06 15:44:26 +01:00
|
|
|
const contentFilter = createContentFilter(filterItems, subplebbitAddress, onFilterMatch);
|
2025-03-06 00:07:08 +01:00
|
|
|
|
2025-03-06 16:27:09 +01:00
|
|
|
const searchFilter = {
|
|
|
|
|
filter: (comment: Comment) => {
|
|
|
|
|
if (!searchText.trim()) return true;
|
2025-03-06 23:48:17 +01:00
|
|
|
return commentMatchesPattern(comment, searchText);
|
2025-03-06 16:27:09 +01:00
|
|
|
},
|
|
|
|
|
key: searchText ? `search-filter-${searchText}` : 'no-search-filter',
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-06 00:07:08 +01:00
|
|
|
return {
|
|
|
|
|
filter: (comment: Comment) => {
|
|
|
|
|
if (!imageFilter.filter(comment)) return false;
|
2025-03-06 16:27:09 +01:00
|
|
|
if (!contentFilter.filter(comment)) return false;
|
|
|
|
|
if (!searchFilter.filter(comment)) return false;
|
2025-03-06 00:07:08 +01:00
|
|
|
|
2025-03-06 16:27:09 +01:00
|
|
|
return true;
|
2025-03-06 00:07:08 +01:00
|
|
|
},
|
2025-03-06 16:27:09 +01:00
|
|
|
key: `${imageFilter.key}-${contentFilter.key}-${searchFilter.key}`,
|
2025-03-06 00:07:08 +01:00
|
|
|
};
|
|
|
|
|
};
|
2024-10-12 15:35:27 +02:00
|
|
|
|
2025-12-08 22:40:16 +01:00
|
|
|
export interface CatalogProps {
|
|
|
|
|
feedCacheKey?: string;
|
|
|
|
|
viewType?: 'all' | 'subs' | 'mod' | 'board';
|
|
|
|
|
boardIdentifier?: string;
|
|
|
|
|
timeFilterNameFromCache?: string;
|
|
|
|
|
isVisible?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Catalog = ({ feedCacheKey, viewType, boardIdentifier: boardIdentifierProp, timeFilterNameFromCache, isVisible = true }: CatalogProps) => {
|
2024-04-12 14:28:06 +02:00
|
|
|
const { t } = useTranslation();
|
2024-05-09 16:04:24 +02:00
|
|
|
const location = useLocation();
|
2025-12-08 22:40:16 +01:00
|
|
|
const params = useParams();
|
2024-05-17 16:39:02 +02:00
|
|
|
|
2025-12-08 22:40:16 +01:00
|
|
|
const isInAllView = viewType ? viewType === 'all' : false;
|
|
|
|
|
const isInSubscriptionsView = viewType ? viewType === 'subs' : false;
|
|
|
|
|
|
2026-01-28 16:51:27 +08:00
|
|
|
const directories = useDirectories();
|
2025-12-08 22:40:16 +01:00
|
|
|
const resolvedAddressFromUrl = useResolvedSubplebbitAddress();
|
|
|
|
|
const subplebbitAddress = useMemo(() => {
|
|
|
|
|
if (boardIdentifierProp) {
|
2026-01-28 16:51:27 +08:00
|
|
|
return getSubplebbitAddress(boardIdentifierProp, directories);
|
2025-12-08 22:40:16 +01:00
|
|
|
}
|
|
|
|
|
return resolvedAddressFromUrl;
|
2026-01-28 16:51:27 +08:00
|
|
|
}, [boardIdentifierProp, directories, resolvedAddressFromUrl]);
|
2025-12-08 22:40:16 +01:00
|
|
|
|
|
|
|
|
const boardPath = useBoardPath(subplebbitAddress);
|
2025-03-07 17:43:09 +01:00
|
|
|
const { showTextOnlyThreads, filterItems, searchText, clearMatchedFilters } = useCatalogFiltersStore();
|
2024-05-17 16:39:02 +02:00
|
|
|
|
|
|
|
|
const account = useAccount();
|
|
|
|
|
const subscriptions = account?.subscriptions;
|
2026-01-28 16:51:27 +08:00
|
|
|
const filteredDirectoryAddresses = useFilteredDirectoryAddresses();
|
2024-05-17 16:39:02 +02:00
|
|
|
|
|
|
|
|
const subplebbitAddresses = useMemo(() => {
|
|
|
|
|
if (isInAllView) {
|
2026-01-28 16:51:27 +08:00
|
|
|
return filteredDirectoryAddresses;
|
2024-05-17 16:39:02 +02:00
|
|
|
}
|
|
|
|
|
if (isInSubscriptionsView) {
|
2025-06-04 16:37:17 +02:00
|
|
|
return (subscriptions || []).filter(Boolean); // Filter out any undefined/null values
|
2024-05-17 16:39:02 +02:00
|
|
|
}
|
2025-06-04 16:37:17 +02:00
|
|
|
// Only include subplebbitAddress if it's defined
|
|
|
|
|
return subplebbitAddress ? [subplebbitAddress] : [];
|
2026-01-28 16:51:27 +08:00
|
|
|
}, [isInAllView, isInSubscriptionsView, subplebbitAddress, filteredDirectoryAddresses, subscriptions]);
|
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
|
|
|
|
2025-12-08 23:32:42 +01:00
|
|
|
const { timeFilterSeconds: timeFilterSecondsFromHook, timeFilterName: timeFilterNameFromHook } = useTimeFilter();
|
2024-06-15 16:03:59 +02:00
|
|
|
const { sortType } = useSortingStore();
|
2025-12-08 22:40:16 +01:00
|
|
|
const timeFilterName = timeFilterNameFromCache || timeFilterNameFromHook;
|
2025-12-08 23:32:42 +01:00
|
|
|
const timeFilterSeconds = timeFilterNameFromCache ? timeFilterNameToSeconds(timeFilterNameFromCache) : timeFilterSecondsFromHook;
|
2024-06-09 18:16:01 +02:00
|
|
|
|
2025-03-06 15:44:26 +01:00
|
|
|
// Create a stable callback for filter matching
|
|
|
|
|
const handleFilterMatch = useCallback((filterIndex: number, cid: string, subplebbitAddress: string) => {
|
|
|
|
|
useCatalogFiltersStore.getState().incrementFilterCount(filterIndex, cid, subplebbitAddress);
|
2025-03-06 00:07:08 +01:00
|
|
|
}, []);
|
|
|
|
|
|
2025-03-06 15:44:26 +01:00
|
|
|
// Set the current subplebbit address
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
useCatalogFiltersStore.getState().setCurrentSubplebbitAddress(subplebbitAddress || null);
|
|
|
|
|
return () => {
|
|
|
|
|
useCatalogFiltersStore.getState().setCurrentSubplebbitAddress(null);
|
|
|
|
|
};
|
|
|
|
|
}, [subplebbitAddress]);
|
|
|
|
|
|
2024-09-10 18:20:24 +02:00
|
|
|
const feedOptions = useMemo(() => {
|
|
|
|
|
const options: any = {
|
|
|
|
|
subplebbitAddresses,
|
|
|
|
|
sortType,
|
|
|
|
|
postsPerPage: isInAllView || isInSubscriptionsView ? 10 : postsPerPage,
|
2025-03-06 16:27:09 +01:00
|
|
|
filter: createCombinedFilter(showTextOnlyThreads, filterItems, searchText, subplebbitAddress || 'all', handleFilterMatch),
|
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;
|
2025-03-06 15:44:26 +01:00
|
|
|
}, [
|
|
|
|
|
subplebbitAddresses,
|
|
|
|
|
sortType,
|
|
|
|
|
isInAllView,
|
|
|
|
|
isInSubscriptionsView,
|
|
|
|
|
postsPerPage,
|
|
|
|
|
timeFilterSeconds,
|
|
|
|
|
showTextOnlyThreads,
|
|
|
|
|
filterItems,
|
2025-03-06 16:27:09 +01:00
|
|
|
searchText,
|
2025-03-06 15:44:26 +01:00
|
|
|
subplebbitAddress,
|
|
|
|
|
handleFilterMatch,
|
|
|
|
|
]);
|
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) => {
|
2025-03-03 23:47:33 +01:00
|
|
|
const { cid, deleted, link, linkHeight, linkWidth, postCid, removed, state, thumbnailUrl, timestamp } = comment || {};
|
2025-03-06 16:27:09 +01:00
|
|
|
|
|
|
|
|
// Basic filtering conditions
|
|
|
|
|
const basicConditions =
|
2025-02-23 16:28:30 +01:00
|
|
|
!deleted &&
|
|
|
|
|
!removed &&
|
|
|
|
|
timestamp > Date.now() / 1000 - 60 * 60 &&
|
|
|
|
|
state === 'succeeded' &&
|
|
|
|
|
cid &&
|
2025-03-06 00:07:08 +01:00
|
|
|
(showTextOnlyThreads ? getHasThumbnail(getCommentMediaInfo(link, thumbnailUrl, linkWidth, linkHeight), comment?.link) : true) &&
|
2025-02-23 16:28:30 +01:00
|
|
|
cid === postCid &&
|
|
|
|
|
comment?.subplebbitAddress === subplebbitAddress &&
|
2025-03-06 16:27:09 +01:00
|
|
|
!feed.some((post) => post.cid === cid);
|
|
|
|
|
|
|
|
|
|
// If search is active, also check search conditions
|
|
|
|
|
if (basicConditions && searchText.trim()) {
|
|
|
|
|
const titleLower = comment?.title?.toLowerCase() || '';
|
|
|
|
|
const contentLower = comment?.content?.toLowerCase() || '';
|
|
|
|
|
const searchPattern = searchText.toLowerCase();
|
|
|
|
|
|
|
|
|
|
return titleLower.includes(searchPattern) || contentLower.includes(searchPattern);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return basicConditions;
|
2025-02-23 16:28:30 +01:00
|
|
|
}),
|
2025-03-06 16:27:09 +01:00
|
|
|
[accountComments, subplebbitAddress, feed, showTextOnlyThreads, searchText],
|
2025-02-23 16:28:30 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 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,
|
2025-03-06 16:27:09 +01:00
|
|
|
filter: createCombinedFilter(showTextOnlyThreads, filterItems, searchText, subplebbitAddress || 'all', handleFilterMatch),
|
2024-10-12 15:35:27 +02:00
|
|
|
});
|
2025-03-06 00:07:08 +01:00
|
|
|
|
2024-10-12 15:35:27 +02:00
|
|
|
const { feed: monthlyFeed } = useFeed({
|
|
|
|
|
subplebbitAddresses,
|
|
|
|
|
sortType,
|
|
|
|
|
newerThan: 60 * 60 * 24 * 30,
|
2025-03-06 16:27:09 +01:00
|
|
|
filter: createCombinedFilter(showTextOnlyThreads, filterItems, searchText, subplebbitAddress || 'all', handleFilterMatch),
|
2024-10-12 15:35:27 +02:00
|
|
|
});
|
|
|
|
|
|
2025-11-08 15:56:32 +01:00
|
|
|
const { feed: yearlyFeed } = useFeed({
|
|
|
|
|
subplebbitAddresses,
|
|
|
|
|
sortType,
|
|
|
|
|
newerThan: 60 * 60 * 24 * 365,
|
|
|
|
|
filter: createCombinedFilter(showTextOnlyThreads, filterItems, searchText, subplebbitAddress || 'all', handleFilterMatch),
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-12 15:35:27 +02:00
|
|
|
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(() => {
|
2025-12-08 22:40:16 +01:00
|
|
|
if (isVisible) {
|
|
|
|
|
setResetFunction(reset);
|
|
|
|
|
}
|
|
|
|
|
}, [reset, setResetFunction, isVisible]);
|
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 || {};
|
2025-02-20 13:54:55 +01:00
|
|
|
const feedLength = feed.length;
|
|
|
|
|
const weeklyFeedLength = weeklyFeed.length;
|
|
|
|
|
const monthlyFeedLength = monthlyFeed.length;
|
2025-11-08 15:56:32 +01:00
|
|
|
const yearlyFeedLength = yearlyFeed.length;
|
2024-04-12 14:28:06 +02:00
|
|
|
|
2025-12-08 23:52:13 +01:00
|
|
|
const currentTimeFilterName = timeFilterName || params?.timeFilterName;
|
2024-06-27 14:18:10 +02:00
|
|
|
|
2026-01-13 17:21:30 +01:00
|
|
|
// Memoize footer component to preserve identity across renders (Virtuoso optimization)
|
|
|
|
|
// Note: useFeedStateString is called inside CatalogFooter to isolate re-renders from backend state changes
|
|
|
|
|
const footerComponents = useMemo(
|
|
|
|
|
() => ({
|
|
|
|
|
Footer: () => (
|
|
|
|
|
<CatalogFooter
|
|
|
|
|
subplebbitAddresses={subplebbitAddresses}
|
|
|
|
|
hasMore={hasMore}
|
|
|
|
|
feedLength={feedLength}
|
|
|
|
|
combinedFeedLength={combinedFeed.length}
|
|
|
|
|
subplebbitAddressesWithNewerPosts={subplebbitAddressesWithNewerPosts}
|
|
|
|
|
onNewerPostsClick={handleNewerPostsButtonClick}
|
|
|
|
|
isInAllView={isInAllView}
|
|
|
|
|
isInSubscriptionsView={isInSubscriptionsView}
|
|
|
|
|
showMorePostsSuggestion={showMorePostsSuggestion}
|
|
|
|
|
weeklyFeedLength={weeklyFeedLength}
|
|
|
|
|
monthlyFeedLength={monthlyFeedLength}
|
|
|
|
|
yearlyFeedLength={yearlyFeedLength}
|
|
|
|
|
boardPath={boardPath}
|
|
|
|
|
currentTimeFilterName={currentTimeFilterName}
|
|
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
}),
|
|
|
|
|
[
|
|
|
|
|
subplebbitAddresses,
|
|
|
|
|
hasMore,
|
|
|
|
|
feedLength,
|
|
|
|
|
combinedFeed.length,
|
|
|
|
|
subplebbitAddressesWithNewerPosts,
|
|
|
|
|
handleNewerPostsButtonClick,
|
|
|
|
|
isInAllView,
|
|
|
|
|
isInSubscriptionsView,
|
|
|
|
|
showMorePostsSuggestion,
|
|
|
|
|
weeklyFeedLength,
|
|
|
|
|
monthlyFeedLength,
|
|
|
|
|
yearlyFeedLength,
|
|
|
|
|
boardPath,
|
|
|
|
|
currentTimeFilterName,
|
|
|
|
|
],
|
|
|
|
|
);
|
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-03-06 00:07:08 +01:00
|
|
|
// Process the feed to move "top" posts to the top
|
|
|
|
|
const processedFeed = useMemo(() => {
|
|
|
|
|
if (!combinedFeed || combinedFeed.length === 0) return combinedFeed;
|
|
|
|
|
|
|
|
|
|
const enabledTopFilters = filterItems.filter((item) => item.enabled && item.text.trim() !== '' && item.top);
|
|
|
|
|
if (enabledTopFilters.length === 0) return combinedFeed;
|
|
|
|
|
|
|
|
|
|
// Separate posts that match "top" filters
|
|
|
|
|
const topPosts: Comment[] = [];
|
|
|
|
|
const regularPosts: Comment[] = [];
|
|
|
|
|
|
|
|
|
|
combinedFeed.forEach((comment) => {
|
|
|
|
|
if (!comment) return;
|
|
|
|
|
|
|
|
|
|
let isTop = false;
|
|
|
|
|
for (const filter of enabledTopFilters) {
|
2025-03-06 23:48:17 +01:00
|
|
|
if (commentMatchesPattern(comment, filter.text)) {
|
2025-03-06 00:07:08 +01:00
|
|
|
isTop = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isTop) {
|
|
|
|
|
topPosts.push(comment);
|
|
|
|
|
} else {
|
|
|
|
|
regularPosts.push(comment);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Return top posts followed by regular posts
|
|
|
|
|
return [...topPosts, ...regularPosts];
|
|
|
|
|
}, [combinedFeed, filterItems]);
|
|
|
|
|
|
|
|
|
|
const rows = useCatalogFeedRows(columnCount, processedFeed, isFeedLoaded, subplebbit);
|
2024-04-12 14:28:06 +02:00
|
|
|
|
|
|
|
|
const virtuosoRef = useRef<VirtuosoHandle | null>(null);
|
2025-12-08 22:40:16 +01:00
|
|
|
const virtuosoStateKey = feedCacheKey ? `${feedCacheKey}-${sortType}-${timeFilterSeconds}` : `${location.pathname}-${sortType}-${timeFilterSeconds}-catalog`;
|
2025-12-08 13:03:25 +01:00
|
|
|
const navigationType = useNavigationType();
|
|
|
|
|
|
2025-12-08 22:40:16 +01:00
|
|
|
const hasBeenVisibleRef = useRef(false);
|
2024-04-12 14:28:06 +02:00
|
|
|
useEffect(() => {
|
2025-12-08 22:40:16 +01:00
|
|
|
if (isVisible && !hasBeenVisibleRef.current) {
|
|
|
|
|
hasBeenVisibleRef.current = true;
|
|
|
|
|
if (navigationType !== 'POP') {
|
|
|
|
|
window.scrollTo({ top: 0, left: 0, behavior: 'auto' });
|
|
|
|
|
}
|
2025-12-08 13:03:25 +01:00
|
|
|
}
|
2025-12-08 22:40:16 +01:00
|
|
|
}, [isVisible, navigationType]);
|
2025-12-08 13:03:25 +01:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-12-08 22:40:16 +01:00
|
|
|
if (!isVisible) return;
|
|
|
|
|
|
2025-12-08 13:03:25 +01:00
|
|
|
const currentKey = virtuosoStateKey;
|
2024-04-12 14:28:06 +02:00
|
|
|
const setLastVirtuosoState = () =>
|
|
|
|
|
virtuosoRef.current?.getState((snapshot: StateSnapshot) => {
|
|
|
|
|
if (snapshot?.ranges?.length) {
|
2025-12-08 13:03:25 +01:00
|
|
|
lastVirtuosoStates[currentKey] = snapshot;
|
2024-04-12 14:28:06 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
window.addEventListener('scroll', setLastVirtuosoState);
|
|
|
|
|
return () => window.removeEventListener('scroll', setLastVirtuosoState);
|
2025-12-08 22:40:16 +01:00
|
|
|
}, [virtuosoStateKey, isVisible]);
|
2024-04-12 14:28:06 +02:00
|
|
|
|
2025-12-08 13:03:25 +01:00
|
|
|
const lastVirtuosoState = navigationType === 'POP' ? lastVirtuosoStates?.[virtuosoStateKey] : undefined;
|
2024-04-12 14:28:06 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-12-08 22:40:16 +01:00
|
|
|
if (!isVisible) return;
|
2025-12-28 23:13:25 +01:00
|
|
|
const boardIdentifier = params.boardIdentifier || boardIdentifierProp;
|
2026-01-28 16:51:27 +08:00
|
|
|
const isDirectory = boardIdentifier ? isDirectoryBoard(boardIdentifier, directories) : false;
|
2025-12-28 23:13:25 +01:00
|
|
|
|
|
|
|
|
let documentTitle: string;
|
|
|
|
|
if (isInAllView) {
|
|
|
|
|
documentTitle = t('all');
|
|
|
|
|
} else if (isInSubscriptionsView) {
|
|
|
|
|
documentTitle = t('subscriptions');
|
|
|
|
|
} else if (isDirectory) {
|
|
|
|
|
documentTitle = `/${boardIdentifier}/`;
|
|
|
|
|
} else {
|
|
|
|
|
documentTitle = title ? title : shortAddress || subplebbitAddress || '';
|
|
|
|
|
}
|
2025-10-17 23:30:35 +02:00
|
|
|
document.title = documentTitle + ` - ${t('catalog')} - 5chan`;
|
2026-01-28 16:51:27 +08:00
|
|
|
}, [title, shortAddress, subplebbitAddress, isInAllView, isInSubscriptionsView, t, isVisible, params.boardIdentifier, boardIdentifierProp, directories]);
|
2024-04-12 14:28:06 +02:00
|
|
|
|
2025-03-07 17:43:09 +01:00
|
|
|
// Clear matched filters when component mounts or when subplebbit changes
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
clearMatchedFilters();
|
|
|
|
|
return () => {
|
|
|
|
|
clearMatchedFilters();
|
|
|
|
|
};
|
|
|
|
|
}, [clearMatchedFilters, subplebbitAddress]);
|
|
|
|
|
|
2025-10-16 23:16:38 +02:00
|
|
|
// Memoize filter color application to avoid redundant iterations
|
|
|
|
|
useMemo(() => {
|
2025-03-07 17:43:09 +01:00
|
|
|
if (combinedFeed.length > 0 && filterItems.length > 0) {
|
|
|
|
|
// Clear existing matched filters
|
|
|
|
|
clearMatchedFilters();
|
|
|
|
|
|
|
|
|
|
// Apply colors to posts that match filters
|
|
|
|
|
combinedFeed.forEach((comment) => {
|
|
|
|
|
if (!comment?.cid) return;
|
|
|
|
|
|
|
|
|
|
// Check each filter
|
|
|
|
|
for (const item of filterItems) {
|
|
|
|
|
if (item.enabled && item.text.trim() !== '' && item.color) {
|
|
|
|
|
if (commentMatchesPattern(comment, item.text)) {
|
|
|
|
|
useCatalogFiltersStore.getState().setMatchedFilter(comment.cid, item.color);
|
|
|
|
|
break; // Use the first matching filter's color
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [combinedFeed, filterItems, clearMatchedFilters]);
|
|
|
|
|
|
2024-04-12 14:28:06 +02:00
|
|
|
return (
|
2024-04-12 17:47:30 +02:00
|
|
|
<div className={styles.content}>
|
|
|
|
|
<hr />
|
|
|
|
|
<div className={styles.catalog}>
|
2025-03-06 18:07:44 +01:00
|
|
|
{processedFeed?.length !== 0 ? (
|
2024-10-12 15:35:27 +02:00
|
|
|
<>
|
2026-01-13 17:01:51 +01:00
|
|
|
{/* Use Virtuoso for infinite scroll only when there's more content to paginate */}
|
|
|
|
|
{hasMore ? (
|
|
|
|
|
<Virtuoso
|
|
|
|
|
increaseViewportBy={{ bottom: 1200, top: 1200 }}
|
|
|
|
|
totalCount={rows?.length || 0}
|
|
|
|
|
data={rows}
|
|
|
|
|
itemContent={(index, row) => <CatalogRow index={index} row={row} />}
|
|
|
|
|
useWindowScroll={true}
|
2026-01-13 17:21:30 +01:00
|
|
|
components={footerComponents}
|
2026-01-13 17:01:51 +01:00
|
|
|
endReached={loadMore}
|
|
|
|
|
ref={virtuosoRef}
|
|
|
|
|
restoreStateFrom={lastVirtuosoState}
|
|
|
|
|
initialScrollTop={lastVirtuosoState?.scrollTop}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
{rows.map((row, index) => (
|
|
|
|
|
<CatalogRow key={index} index={index} row={row} />
|
|
|
|
|
))}
|
2026-01-13 17:21:30 +01:00
|
|
|
<CatalogFooter
|
|
|
|
|
subplebbitAddresses={subplebbitAddresses}
|
|
|
|
|
hasMore={hasMore}
|
|
|
|
|
feedLength={feedLength}
|
|
|
|
|
combinedFeedLength={combinedFeed.length}
|
|
|
|
|
subplebbitAddressesWithNewerPosts={subplebbitAddressesWithNewerPosts}
|
|
|
|
|
onNewerPostsClick={handleNewerPostsButtonClick}
|
|
|
|
|
isInAllView={isInAllView}
|
|
|
|
|
isInSubscriptionsView={isInSubscriptionsView}
|
|
|
|
|
showMorePostsSuggestion={showMorePostsSuggestion}
|
|
|
|
|
weeklyFeedLength={weeklyFeedLength}
|
|
|
|
|
monthlyFeedLength={monthlyFeedLength}
|
|
|
|
|
yearlyFeedLength={yearlyFeedLength}
|
|
|
|
|
boardPath={boardPath}
|
|
|
|
|
currentTimeFilterName={currentTimeFilterName}
|
|
|
|
|
/>
|
2026-01-13 17:01:51 +01:00
|
|
|
</>
|
|
|
|
|
)}
|
2024-10-12 15:35:27 +02:00
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<div className={styles.footer}>
|
2026-01-13 17:21:30 +01:00
|
|
|
<CatalogLoading
|
|
|
|
|
subplebbitAddresses={subplebbitAddresses}
|
|
|
|
|
hasMore={hasMore}
|
|
|
|
|
feedLength={feedLength}
|
|
|
|
|
weeklyFeedLength={weeklyFeedLength}
|
|
|
|
|
monthlyFeedLength={monthlyFeedLength}
|
|
|
|
|
yearlyFeedLength={yearlyFeedLength}
|
|
|
|
|
state={state}
|
|
|
|
|
subscriptionsLength={isInSubscriptionsView ? subscriptions?.length || 0 : 1}
|
|
|
|
|
combinedFeedLength={combinedFeed.length}
|
|
|
|
|
error={error}
|
|
|
|
|
/>
|
2024-10-12 15:35:27 +02:00
|
|
|
</div>
|
|
|
|
|
)}
|
2024-04-12 17:47:30 +02:00
|
|
|
</div>
|
2024-04-12 14:28:06 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Catalog;
|