Files
5chan/src/views/catalog/catalog.tsx
T

144 lines
5.3 KiB
TypeScript
Raw Normal View History

2024-04-12 14:28:06 +02:00
import { useEffect, useMemo, useRef } from 'react';
import { useParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
2024-04-14 16:45:59 +02:00
import { Subplebbit, useFeed, useSubplebbit } from '@plebbit/plebbit-react-hooks';
2024-04-12 14:28:06 +02:00
import { Virtuoso, VirtuosoHandle, StateSnapshot } from 'react-virtuoso';
import useFeedStateString from '../../hooks/use-feed-state-string';
import useWindowWidth from '../../hooks/use-window-width';
import CatalogRow from '../../components/catalog-row';
import LoadingEllipsis from '../../components/loading-ellipsis';
import styles from './catalog.module.css';
const lastVirtuosoStates: { [key: string]: StateSnapshot } = {};
2024-04-14 16:45:59 +02:00
const useFeedRows = (columnCount: number, feed: any, isFeedLoaded: boolean, subplebbit: Subplebbit) => {
const { t } = useTranslation();
const { address, createdAt, description, rules, shortAddress, suggested, title } = subplebbit || {};
const { avatarUrl } = suggested || {};
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
}
if (!description && !rules) {
2024-04-14 16:45:59 +02:00
return feed;
}
const _feed = [...feed];
if (description && description.length > 0) {
2024-04-14 16:45:59 +02:00
_feed.unshift({
isDescription: true,
subplebbitAddress: address,
timestamp: createdAt,
2024-04-14 16:45:59 +02:00
author: { displayName: '## Board Mods' },
content: description,
link: avatarUrl,
title: 'Welcome to ' + (title || `p/${shortAddress}`),
2024-04-14 16:45:59 +02:00
pinned: true,
locked: true,
});
}
if (rules && rules.length > 0) {
2024-04-14 16:45:59 +02:00
_feed.unshift({
isRules: true,
subplebbitAddress: address,
timestamp: createdAt,
2024-04-14 16:45:59 +02:00
author: { displayName: '## Board Mods' },
content: rules.map((rule: string, index: number) => `${index + 1}. ${rule}`).join('\n'),
2024-04-14 16:45:59 +02:00
title: 'Rules',
pinned: true,
locked: true,
});
}
return _feed;
}, [feed, description, rules, address, isFeedLoaded, createdAt, title, shortAddress, avatarUrl]);
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 = [];
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;
}, [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();
const { subplebbitAddress } = useParams<{ subplebbitAddress: string }>();
const subplebbitAddresses = useMemo(() => [subplebbitAddress], [subplebbitAddress]) as string[];
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), []);
const { feed, hasMore, loadMore } = useFeed({ subplebbitAddresses, sortType: 'active', postsPerPage });
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');
const loadingString = <div className={styles.stateString}>{state === 'failed' ? state : <LoadingEllipsis string={loadingStateString} />}</div>;
const Footer = () => {
let footerContent;
if (feed.length === 0) {
footerContent = t('no_posts');
}
if (hasMore || subplebbitAddresses.length === 0) {
footerContent = loadingString;
}
return <div className={styles.footer}>{footerContent}</div>;
};
2024-04-14 16:45:59 +02:00
const isFeedloaded = feed.length > 0 || state === 'failed';
2024-04-12 14:28:06 +02:00
// split feed into rows
2024-04-14 16:45:59 +02:00
const rows = useFeedRows(columnCount, feed, 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) {
lastVirtuosoStates[subplebbitAddress + 'catalog'] = snapshot;
}
});
window.addEventListener('scroll', setLastVirtuosoState);
return () => window.removeEventListener('scroll', setLastVirtuosoState);
2024-04-12 17:47:30 +02:00
}, [subplebbitAddress]);
2024-04-12 14:28:06 +02:00
const lastVirtuosoState = lastVirtuosoStates?.[subplebbitAddress + 'catalog'];
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}>
<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;