2024-04-12 14:28:06 +02:00
|
|
|
import { useEffect, useMemo, useRef } from 'react';
|
2024-05-09 16:04:24 +02:00
|
|
|
import { useLocation, useParams } from 'react-router-dom';
|
2024-04-12 14:28:06 +02:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-05-17 16:39:02 +02:00
|
|
|
import { useAccount, Subplebbit, useFeed, useSubplebbit } from '@plebbit/plebbit-react-hooks';
|
2024-04-12 14:28:06 +02:00
|
|
|
import { Virtuoso, VirtuosoHandle, StateSnapshot } from 'react-virtuoso';
|
2024-05-17 16:39:02 +02:00
|
|
|
import { isAllView, isSubscriptionsView } from '../../lib/utils/view-utils';
|
2024-05-30 18:01:37 +02:00
|
|
|
import useBlockedCommentsStore from '../../stores/useBlockedCommentsStore';
|
|
|
|
|
import useBlockedComments from '../../hooks/use-blocked-comments';
|
2024-05-17 16:39:02 +02:00
|
|
|
import { useDefaultSubplebbitAddresses } from '../../hooks/use-default-subplebbits';
|
2024-04-12 14:28:06 +02:00
|
|
|
import useFeedStateString from '../../hooks/use-feed-state-string';
|
2024-05-17 17:25:25 +02:00
|
|
|
import { useMultisubMetadata } from '../../hooks/use-default-subplebbits';
|
2024-04-12 14:28:06 +02:00
|
|
|
import useWindowWidth from '../../hooks/use-window-width';
|
|
|
|
|
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';
|
2024-04-29 14:53:46 +02:00
|
|
|
import _ from 'lodash';
|
2024-04-12 14:28:06 +02:00
|
|
|
|
|
|
|
|
const lastVirtuosoStates: { [key: string]: StateSnapshot } = {};
|
|
|
|
|
|
2024-05-30 18:01:37 +02:00
|
|
|
const useFeedRows = (columnCount: number, feed: any, isFeedLoaded: boolean, subplebbit: Subplebbit, includeDescriptionAndRules: boolean) => {
|
2024-04-16 10:58:49 +02:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const { address, createdAt, description, rules, shortAddress, suggested, title } = subplebbit || {};
|
|
|
|
|
const { avatarUrl } = suggested || {};
|
|
|
|
|
|
2024-05-17 17:25:25 +02:00
|
|
|
const location = useLocation();
|
|
|
|
|
const isInAllView = isAllView(location.pathname);
|
|
|
|
|
const multisub = useMultisubMetadata();
|
|
|
|
|
|
2024-04-16 10:58:49 +02:00
|
|
|
const feedWithDescriptionAndRules = useMemo(() => {
|
2024-04-14 16:45:59 +02:00
|
|
|
if (!isFeedLoaded) {
|
|
|
|
|
return []; // prevent rules and description from appearing while feed is loading
|
2024-04-12 14:28:06 +02:00
|
|
|
}
|
2024-05-30 18:01:37 +02:00
|
|
|
if (!includeDescriptionAndRules || (!description && !rules && !isInAllView)) {
|
2024-04-14 16:45:59 +02:00
|
|
|
return feed;
|
|
|
|
|
}
|
|
|
|
|
const _feed = [...feed];
|
2024-05-17 17:25:25 +02:00
|
|
|
if ((description && description.length > 0) || isInAllView) {
|
2024-04-14 16:45:59 +02:00
|
|
|
_feed.unshift({
|
|
|
|
|
isDescription: true,
|
2024-04-16 10:58:49 +02:00
|
|
|
subplebbitAddress: address,
|
|
|
|
|
timestamp: createdAt,
|
2024-04-14 16:45:59 +02:00
|
|
|
author: { displayName: '## Board Mods' },
|
2024-05-17 17:25:25 +02:00
|
|
|
content: isInAllView ? multisub?.description : description,
|
2024-04-16 10:58:49 +02:00
|
|
|
link: avatarUrl,
|
2024-05-17 17:25:25 +02:00
|
|
|
title: t('welcome_to_board', { board: isInAllView ? multisub?.title : title || `p/${shortAddress}` }),
|
2024-04-14 16:45:59 +02:00
|
|
|
pinned: true,
|
|
|
|
|
locked: true,
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-04-16 10:58:49 +02:00
|
|
|
if (rules && rules.length > 0) {
|
2024-04-14 16:45:59 +02:00
|
|
|
_feed.unshift({
|
|
|
|
|
isRules: true,
|
2024-04-16 10:58:49 +02:00
|
|
|
subplebbitAddress: address,
|
|
|
|
|
timestamp: createdAt,
|
2024-04-14 16:45:59 +02:00
|
|
|
author: { displayName: '## Board Mods' },
|
2024-04-16 10:58:49 +02:00
|
|
|
content: rules.map((rule: string, index: number) => `${index + 1}. ${rule}`).join('\n'),
|
2024-04-29 14:53:46 +02:00
|
|
|
title: _.capitalize(t('rules')),
|
2024-04-14 16:45:59 +02:00
|
|
|
pinned: true,
|
|
|
|
|
locked: true,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return _feed;
|
2024-05-30 18:01:37 +02:00
|
|
|
}, [feed, description, rules, address, isFeedLoaded, createdAt, title, shortAddress, avatarUrl, t, isInAllView, multisub, includeDescriptionAndRules]);
|
2024-04-12 14:28:06 +02:00
|
|
|
|
2024-04-14 16:45:59 +02:00
|
|
|
// Memoize rows calculation, ensuring it updates on changes to the modified feed or column count
|
|
|
|
|
const rows = useMemo(() => {
|
|
|
|
|
const rows = [];
|
2024-04-16 10:58:49 +02:00
|
|
|
for (let i = 0; i < feedWithDescriptionAndRules.length; i += columnCount) {
|
|
|
|
|
rows.push(feedWithDescriptionAndRules.slice(i, i + columnCount));
|
2024-04-14 16:45:59 +02:00
|
|
|
}
|
2024-04-12 14:28:06 +02:00
|
|
|
return rows;
|
2024-04-16 10:58:49 +02:00
|
|
|
}, [feedWithDescriptionAndRules, columnCount]);
|
2024-04-14 16:45:59 +02:00
|
|
|
|
|
|
|
|
return rows;
|
2024-04-12 14:28:06 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const columnWidth = 180;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
const isInAllView = isAllView(location.pathname);
|
|
|
|
|
const defaultSubplebbitAddresses = useDefaultSubplebbitAddresses();
|
|
|
|
|
|
|
|
|
|
const account = useAccount();
|
|
|
|
|
const subscriptions = account?.subscriptions;
|
|
|
|
|
const isInSubscriptionsView = isSubscriptionsView(location.pathname);
|
|
|
|
|
|
|
|
|
|
const subplebbitAddresses = useMemo(() => {
|
|
|
|
|
if (isInAllView) {
|
|
|
|
|
return defaultSubplebbitAddresses;
|
|
|
|
|
}
|
|
|
|
|
if (isInSubscriptionsView) {
|
|
|
|
|
return subscriptions || [];
|
|
|
|
|
}
|
|
|
|
|
return [subplebbitAddress];
|
|
|
|
|
}, [isInAllView, isInSubscriptionsView, subplebbitAddress, defaultSubplebbitAddresses, subscriptions]);
|
2024-04-12 14:28:06 +02:00
|
|
|
|
|
|
|
|
const columnCount = Math.floor(useWindowWidth() / columnWidth);
|
|
|
|
|
// postPerPage based on columnCount for optimized feed, dont change value after first render
|
|
|
|
|
// eslint-disable-next-line
|
|
|
|
|
const postsPerPage = useMemo(() => (columnCount <= 2 ? 10 : columnCount === 3 ? 15 : columnCount === 4 ? 20 : 25), []);
|
|
|
|
|
|
2024-05-30 18:01:37 +02:00
|
|
|
const blockedComments = useBlockedComments(subplebbitAddress);
|
|
|
|
|
const blockedCommentsAddresses = useMemo(() => blockedComments.map((comment) => comment.cid), [blockedComments]);
|
|
|
|
|
|
2024-04-12 14:28:06 +02:00
|
|
|
const { feed, hasMore, loadMore } = useFeed({ subplebbitAddresses, sortType: 'active', postsPerPage });
|
|
|
|
|
|
2024-05-30 18:01:37 +02:00
|
|
|
const filteredFeed = useMemo(() => feed.filter((comment) => !blockedCommentsAddresses.includes(comment.cid)), [feed, blockedCommentsAddresses]);
|
|
|
|
|
|
|
|
|
|
const { resetShowBlockedComments, showBlockedComments, setShowBlockedComments } = useBlockedCommentsStore();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
resetShowBlockedComments();
|
|
|
|
|
}, [subplebbitAddress, resetShowBlockedComments]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (blockedComments.length === 0) {
|
|
|
|
|
setShowBlockedComments(false);
|
|
|
|
|
}
|
|
|
|
|
}, [blockedComments, setShowBlockedComments]);
|
|
|
|
|
|
2024-04-12 14:28:06 +02:00
|
|
|
const subplebbit = useSubplebbit({ subplebbitAddress });
|
2024-04-14 16:45:59 +02:00
|
|
|
const { shortAddress, state, title } = subplebbit || {};
|
2024-04-12 14:28:06 +02:00
|
|
|
const loadingStateString = useFeedStateString(subplebbitAddresses) || t('loading');
|
2024-05-17 17:25:25 +02:00
|
|
|
const loadingString = (
|
|
|
|
|
<div className={styles.stateString}>
|
|
|
|
|
{state === 'failed' ? (
|
|
|
|
|
state
|
|
|
|
|
) : isInSubscriptionsView && subscriptions?.length === 0 ? (
|
|
|
|
|
`You haven't subscribed to any board yet.`
|
|
|
|
|
) : (
|
|
|
|
|
<LoadingEllipsis string={loadingStateString} />
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2024-04-12 14:28:06 +02:00
|
|
|
|
|
|
|
|
const Footer = () => {
|
|
|
|
|
let footerContent;
|
2024-05-30 18:01:37 +02:00
|
|
|
if ((showBlockedComments ? blockedComments : filteredFeed).length === 0) {
|
2024-04-12 14:28:06 +02:00
|
|
|
footerContent = t('no_posts');
|
|
|
|
|
}
|
|
|
|
|
if (hasMore || subplebbitAddresses.length === 0) {
|
|
|
|
|
footerContent = loadingString;
|
|
|
|
|
}
|
|
|
|
|
return <div className={styles.footer}>{footerContent}</div>;
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-30 18:01:37 +02:00
|
|
|
const isFeedLoaded = (showBlockedComments ? blockedComments : filteredFeed).length > 0 || state === 'failed';
|
2024-04-14 16:45:59 +02:00
|
|
|
|
2024-05-30 18:01:37 +02:00
|
|
|
const rows = useFeedRows(columnCount, showBlockedComments ? blockedComments : filteredFeed, isFeedLoaded, subplebbit, !showBlockedComments);
|
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-05-17 17:25:25 +02:00
|
|
|
lastVirtuosoStates[location.pathname] = snapshot;
|
2024-04-12 14:28:06 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
window.addEventListener('scroll', setLastVirtuosoState);
|
|
|
|
|
return () => window.removeEventListener('scroll', setLastVirtuosoState);
|
2024-05-17 17:25:25 +02:00
|
|
|
}, [location.pathname]);
|
2024-04-12 14:28:06 +02:00
|
|
|
|
2024-05-17 17:25:25 +02:00
|
|
|
const lastVirtuosoState = lastVirtuosoStates?.[location.pathname];
|
2024-04-12 14:28:06 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let documentTitle = title ? title : shortAddress;
|
|
|
|
|
document.title = documentTitle + ' - Catalog';
|
|
|
|
|
}, [title, shortAddress]);
|
|
|
|
|
|
|
|
|
|
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}>
|
|
|
|
|
<Virtuoso
|
|
|
|
|
increaseViewportBy={{ bottom: 1200, top: 1200 }}
|
|
|
|
|
totalCount={rows?.length || 0}
|
|
|
|
|
data={rows}
|
2024-04-14 16:45:59 +02:00
|
|
|
itemContent={(index, row) => <CatalogRow index={index} row={row} />}
|
2024-04-12 17:47:30 +02:00
|
|
|
useWindowScroll={true}
|
|
|
|
|
components={{ Footer }}
|
|
|
|
|
endReached={loadMore}
|
|
|
|
|
ref={virtuosoRef}
|
|
|
|
|
restoreStateFrom={lastVirtuosoState}
|
|
|
|
|
initialScrollTop={lastVirtuosoState?.scrollTop}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2024-04-12 14:28:06 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Catalog;
|