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

142 lines
5.4 KiB
TypeScript
Raw Normal View History

import { useEffect, useMemo, useRef } from 'react';
2024-05-09 16:04:24 +02:00
import { useLocation, useParams } from 'react-router-dom';
2024-05-17 14:54:41 +02:00
import { useAccount, 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-05-17 14:54:41 +02:00
import { isAllView, isSubscriptionsView } from '../../lib/utils/view-utils';
import { useDefaultSubplebbitAddresses } from '../../hooks/use-default-subplebbits';
2024-03-22 15:31:02 +01:00
import useFeedStateString from '../../hooks/use-feed-state-string';
import useReplyModal from '../../hooks/use-reply-modal';
import useFeedResetStore from '../../stores/use-feed-reset-store';
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';
2024-05-09 16:04:24 +02:00
import SettingsModal from '../../components/settings-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();
2024-05-09 16:04:24 +02:00
const location = useLocation();
2024-03-22 15:31:02 +01:00
const { subplebbitAddress } = useParams<{ subplebbitAddress: string }>();
2024-05-17 14:54:41 +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-03-22 15:31:02 +01:00
const sortType = 'active';
const { feed, hasMore, loadMore, reset } = useFeed({ subplebbitAddresses, sortType });
const setResetFunction = useFeedResetStore((state) => state.setResetFunction);
useEffect(() => {
setResetFunction(reset);
}, [reset, setResetFunction]);
2024-04-01 14:24:10 +02:00
const subplebbit = useSubplebbit({ subplebbitAddress });
2024-05-17 14:54:41 +02:00
const { createdAt, description, rules, shortAddress, state, suggested } = subplebbit || {};
const title = isInAllView ? t('all') : isInSubscriptionsView ? t('subscriptions') : subplebbit?.title;
2024-04-08 12:33:59 +02:00
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
) : isInSubscriptionsView && subscriptions?.length === 0 ? (
2024-05-31 17:29:19 +02:00
t('not_subscribed_to_any_board')
) : (
<LoadingEllipsis string={loadingStateString} />
)}
</div>
);
2024-03-22 15:31:02 +01:00
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) {
2024-05-17 17:25:25 +02:00
lastVirtuosoStates[location.pathname] = snapshot;
2024-03-22 15:31:02 +01:00
}
});
};
window.addEventListener('scroll', setLastVirtuosoState);
return () => window.removeEventListener('scroll', setLastVirtuosoState);
2024-05-17 17:25:25 +02:00
}, [location.pathname]);
2024-03-22 15:31:02 +01:00
2024-05-17 17:25:25 +02:00
const lastVirtuosoState = lastVirtuosoStates?.[location.pathname];
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-05-17 16:28:09 +02:00
{location.pathname.endsWith('/settings') && <SettingsModal />}
{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-05-17 16:12:50 +02:00
{((description && description.length > 0) || isInAllView) && (
<SubplebbitDescription
avatarUrl={suggested?.avatarUrl}
subplebbitAddress={subplebbitAddress}
createdAt={createdAt}
description={description}
shortAddress={shortAddress}
title={title}
/>
)}
2024-04-08 12:33:59 +02:00
</>
)}
<Virtuoso
increaseViewportBy={{ bottom: 1200, top: 1200 }}
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} />;
}}
useWindowScroll={true}
components={{ Footer }}
endReached={loadMore}
ref={virtuosoRef}
restoreStateFrom={lastVirtuosoState}
initialScrollTop={lastVirtuosoState?.scrollTop}
/>
2024-03-22 15:31:02 +01:00
</div>
);
2024-03-17 21:47:16 +01:00
};
export default Board;