recursion is not needed

This commit is contained in:
Tom (plebeius.eth)
2024-09-03 12:06:21 +02:00
parent c4a5fe37b0
commit 7f6a0b9794
+15 -25
View File
@@ -1,4 +1,4 @@
import { useMemo, useCallback } from 'react'; import { useMemo } from 'react';
import { Comment, useAccountComments } from '@plebbit/plebbit-react-hooks'; import { Comment, useAccountComments } from '@plebbit/plebbit-react-hooks';
import { flattenCommentsPages } from '@plebbit/plebbit-react-hooks/dist/lib/utils'; import { flattenCommentsPages } from '@plebbit/plebbit-react-hooks/dist/lib/utils';
@@ -11,34 +11,24 @@ const useReplies = (comment: Comment) => {
const { accountComments } = useAccountComments(); 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 filteredAccountComments = useMemo(() => {
const filterComments = (comments: Comment[]) => { const commentMap = new Map(accountComments.map((c) => [c.cid, c]));
return comments.filter((accountComment) => {
const parentCid = accountComment.parentCid; return accountComments.filter((accountComment) => {
if (parentCid === (comment?.cid || 'n/a') || replyCids.has(parentCid)) { let currentCid = accountComment.parentCid;
while (currentCid && currentCid !== comment?.cid) {
if (replyCids.has(currentCid)) {
return true; return true;
} }
// Changed this line to compare with comment?.parentCid instead of comment?.cid const parent = commentMap.get(currentCid);
return getPostCid(accountComment, comments) === comment?.parentCid; if (!parent) {
return false;
}
currentCid = parent.parentCid;
}
return currentCid === comment?.cid;
}); });
}; }, [accountComments, comment?.cid, replyCids]);
return filterComments(accountComments);
}, [accountComments, comment?.cid, comment?.parentCid, replyCids, getPostCid]);
// the account's replies have a delay before getting published, so get them locally from accountComments instead // the account's replies have a delay before getting published, so get them locally from accountComments instead
const accountRepliesNotYetPublished = useMemo(() => { const accountRepliesNotYetPublished = useMemo(() => {