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

103 lines
4.0 KiB
TypeScript
Raw Normal View History

import { useEffect, useMemo, useRef } from 'react';
2024-03-22 15:31:02 +01:00
import { useParams } from 'react-router-dom';
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 useReplyModal from '../../hooks/use-reply-modal';
2024-03-22 15:31:02 +01:00
import LoadingEllipsis from '../../components/loading-ellipsis';
import Post from '../../components/post';
import ReplyModal from '../../components/reply-modal';
import SubplebbitDescription from '../../components/subplebbit-description';
import SubplebbitRules from '../../components/subplebbit-rules';
2024-03-22 15:31:02 +01:00
const lastVirtuosoStates: { [key: string]: StateSnapshot } = {};
2024-03-17 21:47:16 +01: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
const subplebbit = useSubplebbit({ subplebbitAddress });
2024-04-08 12:33:59 +02:00
const { createdAt, description, rules, shortAddress, state, suggested, title } = subplebbit || {};
const { activeCid, closeModal, openReplyModal, showReplyModal, scrollY } = useReplyModal();
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
useEffect(() => {
document.title = title ? title : shortAddress;
}, [title, shortAddress]);
2024-03-22 15:31:02 +01:00
return (
<div className={styles.content}>
{showReplyModal && activeCid && <ReplyModal closeModal={closeModal} parentCid={activeCid} scrollY={scrollY} />}
{feed.length > 0 && (
2024-04-08 12:33:59 +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 && (
<SubplebbitDescription
avatarUrl={suggested?.avatarUrl}
subplebbitAddress={subplebbitAddress}
createdAt={createdAt}
description={description}
shortAddress={shortAddress}
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}
itemContent={(index, post) => {
const { deleted, locked, removed } = post || {};
const isThreadClosed = deleted || locked || removed;
return <Post index={index} post={post} openReplyModal={isThreadClosed ? () => alert(t('thread_closed_alert')) : 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
};
export default Board;