2024-10-09 22:30:31 +02:00
|
|
|
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
|
|
|
import { Link, useLocation, useParams } from 'react-router-dom';
|
2024-09-12 21:22:22 +02:00
|
|
|
import { Comment, useAccount, useAccountComments, useBlock, useFeed, useSubplebbit } from '@plebbit/plebbit-react-hooks';
|
2024-03-22 15:31:02 +01:00
|
|
|
import { Virtuoso, VirtuosoHandle, StateSnapshot } from 'react-virtuoso';
|
2024-10-09 22:30:31 +02:00
|
|
|
import { Trans, 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';
|
2024-04-29 12:35:37 +02:00
|
|
|
import useReplyModal from '../../hooks/use-reply-modal';
|
2024-06-09 16:40:32 +02:00
|
|
|
import useTimeFilter from '../../hooks/use-time-filter';
|
2024-05-31 15:16:21 +02:00
|
|
|
import useFeedResetStore from '../../stores/use-feed-reset-store';
|
2024-06-15 16:03:59 +02:00
|
|
|
import useSortingStore from '../../stores/use-sorting-store';
|
2024-03-22 15:31:02 +01:00
|
|
|
import LoadingEllipsis from '../../components/loading-ellipsis';
|
2024-06-11 15:49:26 +02:00
|
|
|
import { Post } from '../post';
|
2024-04-29 12:05:52 +02:00
|
|
|
import ReplyModal from '../../components/reply-modal';
|
2024-05-09 16:04:24 +02:00
|
|
|
import SettingsModal from '../../components/settings-modal';
|
2024-04-09 15:03:26 +02:00
|
|
|
import SubplebbitDescription from '../../components/subplebbit-description';
|
|
|
|
|
import SubplebbitRules from '../../components/subplebbit-rules';
|
2024-09-12 21:22:22 +02:00
|
|
|
import useInterfaceSettingsStore from '../../stores/use-interface-settings-store';
|
|
|
|
|
import { getCommentMediaInfo, getHasThumbnail } from '../../lib/utils/media-utils';
|
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-09-12 21:22:22 +02:00
|
|
|
const threadsWithoutImagesFilter = (comment: Comment) => {
|
|
|
|
|
if (!getHasThumbnail(getCommentMediaInfo(comment), comment?.link)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-16 10:58:20 +02: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-09-12 21:22:22 +02:00
|
|
|
const { hideThreadsWithoutImages } = useInterfaceSettingsStore();
|
2024-05-17 14:54:41 +02:00
|
|
|
|
2024-10-29 17:40:09 +01:00
|
|
|
const isInAllView = isAllView(location.pathname);
|
2024-05-17 14:54:41 +02:00
|
|
|
const defaultSubplebbitAddresses = useDefaultSubplebbitAddresses();
|
|
|
|
|
|
|
|
|
|
const account = useAccount();
|
|
|
|
|
const subscriptions = account?.subscriptions;
|
2024-06-17 12:17:36 +02:00
|
|
|
const isInSubscriptionsView = isSubscriptionsView(location.pathname, useParams());
|
2024-05-17 14:54:41 +02:00
|
|
|
|
|
|
|
|
const subplebbitAddresses = useMemo(() => {
|
|
|
|
|
if (isInAllView) {
|
|
|
|
|
return defaultSubplebbitAddresses;
|
|
|
|
|
}
|
|
|
|
|
if (isInSubscriptionsView) {
|
|
|
|
|
return subscriptions || [];
|
|
|
|
|
}
|
|
|
|
|
return [subplebbitAddress];
|
|
|
|
|
}, [isInAllView, isInSubscriptionsView, subplebbitAddress, defaultSubplebbitAddresses, subscriptions]);
|
|
|
|
|
|
2024-06-15 16:03:59 +02:00
|
|
|
const { sortType } = useSortingStore();
|
2024-10-09 22:30:31 +02:00
|
|
|
const { timeFilterSeconds, timeFilterName } = useTimeFilter();
|
2024-06-09 18:16:01 +02:00
|
|
|
|
2024-08-04 15:40:09 +02:00
|
|
|
const feedOptions: any = useMemo(
|
|
|
|
|
() => ({
|
|
|
|
|
subplebbitAddresses,
|
|
|
|
|
sortType,
|
|
|
|
|
postsPerPage: isInAllView || isInSubscriptionsView ? 5 : 25,
|
|
|
|
|
...(isInAllView || isInSubscriptionsView ? { newerThan: timeFilterSeconds } : {}),
|
2024-09-12 21:22:22 +02:00
|
|
|
filter: hideThreadsWithoutImages ? threadsWithoutImagesFilter : undefined,
|
2024-08-04 15:40:09 +02:00
|
|
|
}),
|
2024-09-12 21:22:22 +02:00
|
|
|
[subplebbitAddresses, sortType, timeFilterSeconds, isInAllView, isInSubscriptionsView, hideThreadsWithoutImages],
|
2024-08-04 15:40:09 +02:00
|
|
|
);
|
2024-06-09 18:16:01 +02:00
|
|
|
|
2024-10-09 22:30:31 +02:00
|
|
|
const { feed, hasMore, loadMore, reset, subplebbitAddressesWithNewerPosts } = useFeed(feedOptions);
|
2024-08-04 15:40:09 +02:00
|
|
|
const { accountComments } = useAccountComments();
|
|
|
|
|
|
|
|
|
|
const resetTriggeredRef = useRef(false);
|
2024-05-31 15:16:21 +02:00
|
|
|
|
|
|
|
|
const setResetFunction = useFeedResetStore((state) => state.setResetFunction);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setResetFunction(reset);
|
2024-08-04 15:40:09 +02:00
|
|
|
}, [reset, setResetFunction, feed]);
|
|
|
|
|
|
|
|
|
|
// show account comments instantly in the feed once published (cid defined), instead of waiting for the feed to update
|
|
|
|
|
const filteredComments = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
accountComments.filter((comment) => {
|
2024-10-15 12:11:56 +02:00
|
|
|
const { cid, deleted, postCid, removed, state, timestamp } = comment || {};
|
2024-08-04 15:40:09 +02:00
|
|
|
return (
|
|
|
|
|
!deleted &&
|
|
|
|
|
!removed &&
|
2024-10-15 12:11:56 +02:00
|
|
|
timestamp > Date.now() - 60 * 60 * 1000 &&
|
2024-08-04 15:40:09 +02:00
|
|
|
state === 'succeeded' &&
|
|
|
|
|
cid &&
|
2024-09-13 15:06:10 +02:00
|
|
|
(hideThreadsWithoutImages ? getHasThumbnail(getCommentMediaInfo(comment), comment?.link) : true) &&
|
2024-08-04 15:40:09 +02:00
|
|
|
cid === postCid &&
|
|
|
|
|
comment?.subplebbitAddress === subplebbitAddress &&
|
|
|
|
|
!feed.some((post) => post.cid === cid)
|
|
|
|
|
);
|
|
|
|
|
}),
|
2024-09-15 21:38:16 +02:00
|
|
|
[accountComments, subplebbitAddress, feed, hideThreadsWithoutImages],
|
2024-08-04 15:40:09 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// show newest account comment at the top of the feed but after pinned posts
|
|
|
|
|
const combinedFeed = useMemo(() => {
|
|
|
|
|
const newFeed = [...feed];
|
|
|
|
|
const lastPinnedIndex = newFeed.map((post) => post.pinned).lastIndexOf(true);
|
|
|
|
|
if (filteredComments.length > 0) {
|
|
|
|
|
newFeed.splice(lastPinnedIndex + 1, 0, ...filteredComments);
|
|
|
|
|
}
|
|
|
|
|
return newFeed;
|
|
|
|
|
}, [feed, filteredComments]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (filteredComments.length > 0 && !resetTriggeredRef.current) {
|
|
|
|
|
reset();
|
|
|
|
|
resetTriggeredRef.current = true;
|
|
|
|
|
}
|
|
|
|
|
}, [filteredComments, reset]);
|
2024-04-01 14:24:10 +02:00
|
|
|
|
2024-06-27 14:12:47 +02:00
|
|
|
const subplebbit = useSubplebbit({ subplebbitAddress });
|
|
|
|
|
const { createdAt, description, error, rules, shortAddress, state, suggested } = subplebbit || {};
|
2024-05-17 14:54:41 +02:00
|
|
|
const title = isInAllView ? t('all') : isInSubscriptionsView ? t('subscriptions') : subplebbit?.title;
|
2024-04-08 12:33:59 +02:00
|
|
|
|
2024-08-14 10:23:47 +02:00
|
|
|
const { activeCid, threadCid, closeModal, openReplyModal, showReplyModal, scrollY, subplebbitAddress: postSubplebbitAddress } = useReplyModal();
|
2024-04-29 12:35:37 +02:00
|
|
|
|
2024-08-04 15:40:09 +02:00
|
|
|
const { blocked, unblock } = useBlock({ address: subplebbitAddress });
|
2024-03-22 15:31:02 +01:00
|
|
|
const loadingStateString = useFeedStateString(subplebbitAddresses) || t('loading');
|
2024-05-17 16:48:28 +02:00
|
|
|
const loadingString = (
|
|
|
|
|
<div className={styles.stateString}>
|
|
|
|
|
{state === 'failed' ? (
|
2024-07-07 20:02:58 +02:00
|
|
|
<span className='red'>{state}</span>
|
2024-05-17 16:48:28 +02:00
|
|
|
) : isInSubscriptionsView && subscriptions?.length === 0 ? (
|
2024-05-31 17:29:19 +02:00
|
|
|
t('not_subscribed_to_any_board')
|
2024-08-04 15:40:09 +02:00
|
|
|
) : blocked ? (
|
|
|
|
|
'you have blocked this board'
|
|
|
|
|
) : !hasMore && feed.length === 0 ? (
|
|
|
|
|
t('no_posts')
|
2024-05-17 16:48:28 +02:00
|
|
|
) : (
|
2024-08-07 21:14:21 +02:00
|
|
|
hasMore && <LoadingEllipsis string={loadingStateString} />
|
2024-05-17 16:48:28 +02:00
|
|
|
)}
|
2024-06-03 11:04:53 +02:00
|
|
|
{error && (
|
2024-07-07 20:02:58 +02:00
|
|
|
<div className='red'>
|
2024-06-03 11:04:53 +02:00
|
|
|
<br />
|
|
|
|
|
{error.message}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2024-05-17 16:48:28 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
2024-03-22 15:31:02 +01:00
|
|
|
|
2024-10-09 22:30:31 +02:00
|
|
|
const handleNewerPostsButtonClick = () => {
|
2024-10-12 15:35:27 +02:00
|
|
|
window.scrollTo({ top: 0, left: 0 });
|
2024-10-09 22:30:31 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
|
reset();
|
|
|
|
|
}, 300);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// suggest the user to change time filter if there aren't enough posts
|
|
|
|
|
const { feed: weeklyFeed } = useFeed({
|
|
|
|
|
subplebbitAddresses,
|
|
|
|
|
sortType,
|
|
|
|
|
newerThan: 60 * 60 * 24 * 7,
|
|
|
|
|
filter: hideThreadsWithoutImages ? threadsWithoutImagesFilter : undefined,
|
|
|
|
|
});
|
|
|
|
|
const { feed: monthlyFeed } = useFeed({
|
|
|
|
|
subplebbitAddresses,
|
|
|
|
|
sortType,
|
|
|
|
|
newerThan: 60 * 60 * 24 * 30,
|
|
|
|
|
filter: hideThreadsWithoutImages ? threadsWithoutImagesFilter : undefined,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const [showMorePostsSuggestion, setShowMorePostsSuggestion] = useState(false);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const timer = setTimeout(() => {
|
|
|
|
|
setShowMorePostsSuggestion(true);
|
|
|
|
|
}, 5000);
|
|
|
|
|
|
|
|
|
|
return () => clearTimeout(timer);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const params = useParams();
|
|
|
|
|
const currentTimeFilterName = params?.timeFilterName || timeFilterName;
|
|
|
|
|
|
2024-03-22 15:31:02 +01:00
|
|
|
const Footer = () => {
|
2024-10-09 22:30:31 +02:00
|
|
|
let footerContent;
|
|
|
|
|
if (feed.length === 0) {
|
|
|
|
|
footerContent = t('no_posts');
|
|
|
|
|
}
|
|
|
|
|
if (hasMore || (subplebbitAddresses && subplebbitAddresses.length === 0)) {
|
|
|
|
|
footerContent = (
|
|
|
|
|
<>
|
|
|
|
|
{subplebbitAddressesWithNewerPosts.length > 0 ? (
|
|
|
|
|
<div className={styles.stateString}>
|
|
|
|
|
<Trans
|
|
|
|
|
i18nKey='newer_posts_available'
|
|
|
|
|
components={{
|
2024-10-15 13:10:47 +02:00
|
|
|
1: <span className={styles.newerPostsButton} onClick={handleNewerPostsButtonClick} />,
|
2024-10-09 22:30:31 +02:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
(isInAllView || isInSubscriptionsView) &&
|
|
|
|
|
showMorePostsSuggestion &&
|
|
|
|
|
monthlyFeed.length > feed.length &&
|
|
|
|
|
(weeklyFeed.length > feed.length ? (
|
|
|
|
|
<div className={styles.stateString}>
|
|
|
|
|
<Trans
|
|
|
|
|
i18nKey='more_posts_last_week'
|
|
|
|
|
values={{ currentTimeFilterName }}
|
|
|
|
|
components={{
|
2024-10-12 15:35:27 +02:00
|
|
|
1: <Link to={(isInAllView ? '/p/all' : isInSubscriptionsView ? '/p/subscriptions' : `/p/${subplebbitAddress}`) + '/1w'} />,
|
2024-10-09 22:30:31 +02:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className={styles.stateString}>
|
|
|
|
|
<Trans
|
|
|
|
|
i18nKey='more_posts_last_month'
|
|
|
|
|
values={{ currentTimeFilterName }}
|
|
|
|
|
components={{
|
2024-10-12 15:35:27 +02:00
|
|
|
1: <Link to={(isInAllView ? '/p/all' : isInSubscriptionsView ? '/p/subscriptions' : `/p/${subplebbitAddress}`) + '/1m'} />,
|
2024-10-09 22:30:31 +02:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
))
|
|
|
|
|
)}
|
|
|
|
|
<div className={styles.stateString}>
|
|
|
|
|
<LoadingEllipsis string={loadingStateString} />
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return <div className={styles.footer}>{footerContent}</div>;
|
2024-03-22 15:31:02 +01: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) {
|
2024-06-09 16:40:32 +02:00
|
|
|
lastVirtuosoStates[sortType + timeFilterSeconds] = snapshot;
|
2024-03-22 15:31:02 +01:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
window.addEventListener('scroll', setLastVirtuosoState);
|
|
|
|
|
return () => window.removeEventListener('scroll', setLastVirtuosoState);
|
2024-06-09 16:40:32 +02:00
|
|
|
}, [sortType, timeFilterSeconds]);
|
2024-03-22 15:31:02 +01:00
|
|
|
|
2024-06-09 16:40:32 +02:00
|
|
|
const lastVirtuosoState = lastVirtuosoStates?.[sortType + timeFilterSeconds];
|
2024-04-01 14:24:10 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-06-28 17:14:57 +02:00
|
|
|
const boardTitle = title ? title : shortAddress || subplebbitAddress;
|
|
|
|
|
document.title = boardTitle + ' - plebchan';
|
2024-06-03 13:53:12 +02:00
|
|
|
}, [title, shortAddress, subplebbitAddress]);
|
2024-04-01 14:24:10 +02:00
|
|
|
|
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 />}
|
2024-08-14 10:23:47 +02:00
|
|
|
{activeCid && threadCid && postSubplebbitAddress && (
|
|
|
|
|
<ReplyModal
|
|
|
|
|
closeModal={closeModal}
|
|
|
|
|
parentCid={activeCid}
|
|
|
|
|
postCid={threadCid}
|
|
|
|
|
scrollY={scrollY}
|
|
|
|
|
showReplyModal={showReplyModal}
|
|
|
|
|
subplebbitAddress={postSubplebbitAddress}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2024-09-07 18:11:38 +02:00
|
|
|
{((description && description.length > 0) || isInAllView) && (
|
|
|
|
|
<SubplebbitDescription
|
|
|
|
|
avatarUrl={suggested?.avatarUrl}
|
|
|
|
|
subplebbitAddress={subplebbitAddress}
|
|
|
|
|
createdAt={createdAt}
|
|
|
|
|
description={description}
|
2024-09-18 12:37:39 +02:00
|
|
|
replyCount={isInAllView ? 0 : rules?.length > 0 ? 1 : 0}
|
2024-09-07 18:11:38 +02:00
|
|
|
shortAddress={shortAddress}
|
|
|
|
|
title={title}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2024-08-04 15:40:09 +02:00
|
|
|
{feed.length !== 0 ? (
|
2024-04-08 12:33:59 +02:00
|
|
|
<>
|
2024-09-15 21:27:32 +02:00
|
|
|
{rules && !description && rules.length > 0 && <SubplebbitRules subplebbitAddress={subplebbitAddress} createdAt={createdAt} rules={rules} />}
|
2024-08-04 15:40:09 +02:00
|
|
|
<Virtuoso
|
|
|
|
|
increaseViewportBy={{ bottom: 1200, top: 1200 }}
|
|
|
|
|
totalCount={combinedFeed.length}
|
|
|
|
|
data={combinedFeed}
|
|
|
|
|
itemContent={(index, post) => {
|
|
|
|
|
const { deleted, locked, removed } = post || {};
|
|
|
|
|
const isThreadClosed = deleted || locked || removed;
|
2024-04-29 12:17:48 +02:00
|
|
|
|
2024-08-04 15:40:09 +02:00
|
|
|
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}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
) : (
|
|
|
|
|
<div className={styles.footer}>
|
|
|
|
|
{loadingString}
|
|
|
|
|
{blocked && (
|
|
|
|
|
<>
|
|
|
|
|
[
|
|
|
|
|
<span
|
|
|
|
|
className={styles.button}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
unblock();
|
|
|
|
|
reset();
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
Unblock
|
|
|
|
|
</span>
|
|
|
|
|
]
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2024-03-22 15:31:02 +01:00
|
|
|
</div>
|
|
|
|
|
);
|
2024-03-17 21:47:16 +01:00
|
|
|
};
|
|
|
|
|
|
2024-04-16 10:58:20 +02:00
|
|
|
export default Board;
|