2024-03-22 15:31:02 +01:00
|
|
|
import { useEffect, useMemo, useRef } from 'react';
|
|
|
|
|
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-03-17 21:47:16 +01:00
|
|
|
import styles from './subplebbit.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-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-08 17:32:12 +02:00
|
|
|
interface DescriptionPostProps {
|
|
|
|
|
subplebbitAddress: string | undefined;
|
|
|
|
|
createdAt: number;
|
|
|
|
|
description: string;
|
|
|
|
|
avatarUrl?: string;
|
|
|
|
|
title: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const DescriptionPost = ({ subplebbitAddress, createdAt, description, avatarUrl, title }: DescriptionPostProps) => {
|
|
|
|
|
const post = {
|
|
|
|
|
isDescription: true,
|
|
|
|
|
subplebbitAddress,
|
|
|
|
|
timestamp: createdAt,
|
|
|
|
|
author: { displayName: '## Board Mods' },
|
|
|
|
|
content: description,
|
|
|
|
|
link: avatarUrl,
|
|
|
|
|
title: 'Welcome to ' + title,
|
|
|
|
|
pinned: true,
|
|
|
|
|
locked: true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return <Post post={post} />;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
interface RulesPostProps {
|
|
|
|
|
subplebbitAddress: string | undefined;
|
|
|
|
|
createdAt: number;
|
|
|
|
|
rules: string[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const RulesPost = ({ subplebbitAddress, createdAt, rules }: RulesPostProps) => {
|
|
|
|
|
const content = rules.map((rule, index) => `${index + 1}. ${rule}`).join('\n');
|
|
|
|
|
const post = {
|
|
|
|
|
isRules: true,
|
|
|
|
|
subplebbitAddress,
|
|
|
|
|
timestamp: createdAt,
|
|
|
|
|
author: { displayName: '## Board Mods' },
|
|
|
|
|
content,
|
|
|
|
|
title: 'Rules',
|
|
|
|
|
pinned: true,
|
|
|
|
|
locked: true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return <Post post={post} />;
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-07 12:21:53 +02:00
|
|
|
const Subplebbit = () => {
|
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
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
document.title = title ? title : shortAddress;
|
|
|
|
|
}, [title, shortAddress]);
|
|
|
|
|
|
2024-03-22 15:31:02 +01:00
|
|
|
return (
|
|
|
|
|
<div className={styles.content}>
|
2024-04-08 12:33:59 +02:00
|
|
|
{createdAt && (
|
|
|
|
|
<>
|
2024-04-08 17:32:12 +02:00
|
|
|
{rules && rules.length > 0 && <RulesPost subplebbitAddress={subplebbitAddress} createdAt={createdAt} rules={rules} />}
|
|
|
|
|
{description && description.length > 0 && (
|
|
|
|
|
<DescriptionPost avatarUrl={suggested?.avatarUrl} subplebbitAddress={subplebbitAddress} createdAt={createdAt} description={description} title={title} />
|
|
|
|
|
)}
|
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) => <Post index={index} post={post} />}
|
|
|
|
|
useWindowScroll={true}
|
|
|
|
|
components={{ Footer }}
|
|
|
|
|
endReached={loadMore}
|
|
|
|
|
ref={virtuosoRef}
|
|
|
|
|
restoreStateFrom={lastVirtuosoState}
|
|
|
|
|
initialScrollTop={lastVirtuosoState?.scrollTop}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2024-03-17 21:47:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Subplebbit;
|