fix(use-replies.ts): flatten and display replies of replies not yet published

This commit is contained in:
plebeius.eth
2024-04-29 21:49:00 +02:00
parent bb9d53f47c
commit 6e20b86b7b
+14 -3
View File
@@ -3,11 +3,22 @@ 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';
const useRepliesAndAccountReplies = (comment: Comment) => { const useRepliesAndAccountReplies = (comment: Comment) => {
// filter only the parent cid // flatten all replies including nested ones from the original comment
const filter = useCallback((accountComment: Comment) => accountComment.parentCid === (comment?.cid || 'n/a'), [comment?.cid]);
const { accountComments } = useAccountComments({ filter });
const flattenedReplies = useMemo(() => flattenCommentsPages(comment.replies), [comment.replies]); const flattenedReplies = useMemo(() => flattenCommentsPages(comment.replies), [comment.replies]);
// gemerate a Set of CIDs from flattened replies for quick lookup
const replyCids = useMemo(() => new Set(flattenedReplies.map((reply) => reply.cid)), [flattenedReplies]);
// filter against all CIDs in flattened replies
const filter = useCallback(
(accountComment: Comment) => {
return replyCids.has(accountComment.parentCid);
},
[replyCids],
);
const { accountComments } = useAccountComments({ filter });
// 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(() => {
const replies = flattenedReplies || []; const replies = flattenedReplies || [];