From a698b222f8a22bf3a4e5850cd5328b81cebbbdfa Mon Sep 17 00:00:00 2001 From: "Tom (plebeius.eth)" Date: Fri, 30 Aug 2024 12:19:18 +0200 Subject: [PATCH] fix(use-replies): a reply to a newly-published reply wouldn't render until propagated --- src/hooks/use-replies.ts | 41 ++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/hooks/use-replies.ts b/src/hooks/use-replies.ts index c4f1ac43..0426f009 100644 --- a/src/hooks/use-replies.ts +++ b/src/hooks/use-replies.ts @@ -1,4 +1,4 @@ -import { useMemo, useCallback } from 'react'; +import { useMemo, useCallback, useState, useEffect } from 'react'; import { Comment, useAccountComments } from '@plebbit/plebbit-react-hooks'; import { flattenCommentsPages } from '@plebbit/plebbit-react-hooks/dist/lib/utils'; @@ -9,24 +9,45 @@ const useReplies = (comment: Comment) => { // generate a Set of CIDs from flattened replies for quick lookup const replyCids = useMemo(() => new Set(flattenedReplies.map((reply) => reply?.cid)), [flattenedReplies]); - // filter against the original comment's CID and all CIDs in flattened replies - const filter = useCallback( - (accountComment: Comment) => { - const parentCid = accountComment.parentCid; - return parentCid === (comment?.cid || 'n/a') || replyCids.has(parentCid); + const [filteredAccountComments, setFilteredAccountComments] = useState([]); + + const getPostCid = useCallback( + (accountComment: Comment, allComments: Comment[]): string | null => { + if (accountComment.parentCid === comment?.cid) { + return comment?.cid; + } + const parent = allComments.find((c) => c.cid === accountComment.parentCid); + if (!parent) { + return null; + } + return getPostCid(parent, allComments); }, - [comment?.cid, replyCids], + [comment?.cid], ); - const { accountComments } = useAccountComments({ filter }); + const { accountComments } = useAccountComments(); + + useEffect(() => { + const filterComments = (comments: Comment[]) => { + return comments.filter((accountComment) => { + const parentCid = accountComment.parentCid; + if (parentCid === (comment?.cid || 'n/a') || replyCids.has(parentCid)) { + return true; + } + return getPostCid(accountComment, comments) === comment?.cid; + }); + }; + + setFilteredAccountComments(filterComments(accountComments)); + }, [accountComments, comment?.cid, replyCids, getPostCid]); // the account's replies have a delay before getting published, so get them locally from accountComments instead const accountRepliesNotYetPublished = useMemo(() => { const replies = flattenedReplies || []; const replyCids = new Set(replies.map((reply: Comment) => reply?.cid)); // filter out the account comments already in comment.replies, so they don't appear twice - return accountComments.filter((accountReply) => !replyCids.has(accountReply?.cid)); - }, [flattenedReplies, accountComments]); + return filteredAccountComments.filter((accountReply) => !replyCids.has(accountReply?.cid)); + }, [flattenedReplies, filteredAccountComments]); const repliesAndNotYetPublishedReplies = useMemo(() => { const repliesSortedByPinnedAndTimestamp = [...accountRepliesNotYetPublished.reverse(), ...(flattenedReplies || [])];