feat(feed): show account comments instantly in the feed once published, instead of waiting for the feed to update

This commit is contained in:
Tom (plebeius.eth)
2024-08-03 13:21:09 +02:00
parent 2ab71978a7
commit 8598d1056e
4 changed files with 139 additions and 68 deletions
+16 -1
View File
@@ -1,6 +1,6 @@
import { useEffect, useMemo, useRef } from 'react';
import { useLocation, useParams } from 'react-router-dom';
import { useAccount, useBlock, useFeed, useSubplebbit } from '@plebbit/plebbit-react-hooks';
import { useAccount, useAccountComments, useBlock, useFeed, useSubplebbit } from '@plebbit/plebbit-react-hooks';
import { Virtuoso, VirtuosoHandle, StateSnapshot } from 'react-virtuoso';
import { useTranslation } from 'react-i18next';
import styles from './board.module.css';
@@ -20,6 +20,20 @@ import SubplebbitRules from '../../components/subplebbit-rules';
const lastVirtuosoStates: { [key: string]: StateSnapshot } = {};
// show account comments instantly in the feed instead of waiting for the next feed update, then hide them when they are in the feed
const AccountCommentsNotYetInFeed = ({ subplebbitAddress, feed }: { subplebbitAddress: string; feed: any }) => {
const { accountComments } = useAccountComments();
const _feed = [...feed];
const filteredComments = accountComments.filter((comment) => {
const { cid, deleted, postCid, removed } = comment || {};
return !deleted && !removed && cid && cid === postCid && comment?.subplebbitAddress === subplebbitAddress && !_feed.some((post) => post.cid === cid);
});
return filteredComments.map((comment) => <Post key={comment.cid} post={comment} />);
};
const Board = () => {
const { t } = useTranslation();
const location = useLocation();
@@ -161,6 +175,7 @@ const Board = () => {
title={title}
/>
)}
{subplebbitAddress && <AccountCommentsNotYetInFeed subplebbitAddress={subplebbitAddress} feed={feed} />}
</>
)}
<Virtuoso