2024-04-29 12:05:52 +02:00
|
|
|
import { useEffect, useMemo, useRef, useState } from 'react';
|
2024-03-22 15:31:02 +01:00
|
|
|
import { useParams } from 'react-router-dom';
|
2024-04-07 12:21:53 +02:00
|
|
|
import { useFeed, useSubplebbit } from '@plebbit/plebbit-react-hooks';
|
2024-03-22 15:31:02 +01:00
|
|
|
import { Virtuoso, VirtuosoHandle, StateSnapshot } from 'react-virtuoso';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-04-13 20:44:54 +02:00
|
|
|
import styles from './board.module.css';
|
2024-03-22 15:31:02 +01:00
|
|
|
import useFeedStateString from '../../hooks/use-feed-state-string';
|
|
|
|
|
import LoadingEllipsis from '../../components/loading-ellipsis';
|
|
|
|
|
import Post from '../../components/post';
|
2024-04-29 12:05:52 +02:00
|
|
|
import ReplyModal from '../../components/reply-modal';
|
2024-04-09 15:03:26 +02:00
|
|
|
import SubplebbitDescription from '../../components/subplebbit-description';
|
|
|
|
|
import SubplebbitRules from '../../components/subplebbit-rules';
|
2024-04-07 12:21:53 +02:00
|
|
|
|
2024-03-22 15:31:02 +01:00
|
|
|
const lastVirtuosoStates: { [key: string]: StateSnapshot } = {};
|
2024-03-17 21:47:16 +01:00
|
|
|
|
2024-04-16 10:58:20 +02:00
|
|
|
const Board = () => {
|
2024-03-22 15:31:02 +01:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const { subplebbitAddress } = useParams<{ subplebbitAddress: string }>();
|
|
|
|
|
const subplebbitAddresses = useMemo(() => [subplebbitAddress], [subplebbitAddress]) as string[];
|
|
|
|
|
const sortType = 'active';
|
|
|
|
|
const { feed, hasMore, loadMore } = useFeed({ subplebbitAddresses, sortType });
|
2024-04-01 14:24:10 +02:00
|
|
|
|
2024-04-07 12:21:53 +02:00
|
|
|
const subplebbit = useSubplebbit({ subplebbitAddress });
|
2024-04-08 12:33:59 +02:00
|
|
|
const { createdAt, description, rules, shortAddress, state, suggested, title } = subplebbit || {};
|
|
|
|
|
|
2024-03-22 15:31:02 +01: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>;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 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 + sortType] = snapshot;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
window.addEventListener('scroll', setLastVirtuosoState);
|
|
|
|
|
return () => window.removeEventListener('scroll', setLastVirtuosoState);
|
|
|
|
|
}, [subplebbitAddress, sortType]);
|
|
|
|
|
|
|
|
|
|
const lastVirtuosoState = lastVirtuosoStates?.[subplebbitAddress + sortType];
|
2024-04-01 14:24:10 +02:00
|
|
|
|
2024-04-29 12:05:52 +02:00
|
|
|
const [showReplyModal, setShowReplyModal] = useState(false);
|
|
|
|
|
const [activeCid, setActiveCid] = useState<string | null>(null);
|
|
|
|
|
|
|
|
|
|
const openReplyModal = (cid: string) => {
|
|
|
|
|
if (activeCid && activeCid !== cid) {
|
|
|
|
|
closeModal();
|
|
|
|
|
setActiveCid(cid);
|
|
|
|
|
setShowReplyModal(true);
|
|
|
|
|
} else if (!activeCid) {
|
|
|
|
|
setActiveCid(cid);
|
|
|
|
|
setShowReplyModal(true);
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const closeModal = () => {
|
|
|
|
|
setActiveCid(null);
|
|
|
|
|
setShowReplyModal(false);
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-01 14:24:10 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
document.title = title ? title : shortAddress;
|
|
|
|
|
}, [title, shortAddress]);
|
|
|
|
|
|
2024-03-22 15:31:02 +01:00
|
|
|
return (
|
|
|
|
|
<div className={styles.content}>
|
2024-04-29 12:05:52 +02:00
|
|
|
{showReplyModal && activeCid && <ReplyModal closeModal={closeModal} parentCid={activeCid} />}
|
2024-04-16 10:58:20 +02:00
|
|
|
{feed.length > 0 && (
|
2024-04-08 12:33:59 +02:00
|
|
|
<>
|
2024-04-09 15:03:26 +02:00
|
|
|
{rules && rules.length > 0 && <SubplebbitRules subplebbitAddress={subplebbitAddress} createdAt={createdAt} rules={rules} />}
|
2024-04-08 17:32:12 +02:00
|
|
|
{description && description.length > 0 && (
|
2024-04-09 15:03:26 +02:00
|
|
|
<SubplebbitDescription avatarUrl={suggested?.avatarUrl} subplebbitAddress={subplebbitAddress} createdAt={createdAt} description={description} title={title} />
|
2024-04-08 17:32:12 +02:00
|
|
|
)}
|
2024-04-08 12:33:59 +02:00
|
|
|
</>
|
|
|
|
|
)}
|
2024-03-22 15:31:02 +01:00
|
|
|
<Virtuoso
|
2024-03-29 20:43:58 +01:00
|
|
|
increaseViewportBy={{ bottom: 1200, top: 1200 }}
|
2024-03-22 15:31:02 +01:00
|
|
|
totalCount={feed?.length || 0}
|
|
|
|
|
data={feed}
|
2024-04-29 12:05:52 +02:00
|
|
|
itemContent={(index, post) => <Post index={index} post={post} openReplyModal={openReplyModal} />}
|
2024-03-22 15:31:02 +01:00
|
|
|
useWindowScroll={true}
|
|
|
|
|
components={{ Footer }}
|
|
|
|
|
endReached={loadMore}
|
|
|
|
|
ref={virtuosoRef}
|
|
|
|
|
restoreStateFrom={lastVirtuosoState}
|
|
|
|
|
initialScrollTop={lastVirtuosoState?.scrollTop}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2024-03-17 21:47:16 +01:00
|
|
|
};
|
|
|
|
|
|
2024-04-16 10:58:20 +02:00
|
|
|
export default Board;
|