From 7f6a0b979426f710fc0ef5d987c3b91fa13f4c4e Mon Sep 17 00:00:00 2001 From: "Tom (plebeius.eth)" Date: Tue, 3 Sep 2024 12:06:21 +0200 Subject: [PATCH] recursion is not needed --- src/hooks/use-replies.ts | 42 +++++++++++++++------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/src/hooks/use-replies.ts b/src/hooks/use-replies.ts index 598b3775..3da6f700 100644 --- a/src/hooks/use-replies.ts +++ b/src/hooks/use-replies.ts @@ -1,4 +1,4 @@ -import { useMemo, useCallback } from 'react'; +import { useMemo } from 'react'; import { Comment, useAccountComments } from '@plebbit/plebbit-react-hooks'; import { flattenCommentsPages } from '@plebbit/plebbit-react-hooks/dist/lib/utils'; @@ -11,34 +11,24 @@ const useReplies = (comment: Comment) => { const { accountComments } = useAccountComments(); - 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], - ); - const filteredAccountComments = useMemo(() => { - const filterComments = (comments: Comment[]) => { - return comments.filter((accountComment) => { - const parentCid = accountComment.parentCid; - if (parentCid === (comment?.cid || 'n/a') || replyCids.has(parentCid)) { + const commentMap = new Map(accountComments.map((c) => [c.cid, c])); + + return accountComments.filter((accountComment) => { + let currentCid = accountComment.parentCid; + while (currentCid && currentCid !== comment?.cid) { + if (replyCids.has(currentCid)) { return true; } - // Changed this line to compare with comment?.parentCid instead of comment?.cid - return getPostCid(accountComment, comments) === comment?.parentCid; - }); - }; - - return filterComments(accountComments); - }, [accountComments, comment?.cid, comment?.parentCid, replyCids, getPostCid]); + const parent = commentMap.get(currentCid); + if (!parent) { + return false; + } + currentCid = parent.parentCid; + } + return currentCid === comment?.cid; + }); + }, [accountComments, comment?.cid, replyCids]); // the account's replies have a delay before getting published, so get them locally from accountComments instead const accountRepliesNotYetPublished = useMemo(() => {