make useReplies page access dynamic to improve API compatibility

This commit is contained in:
Tom (plebeius.eth)
2025-05-07 12:18:54 +02:00
parent 974257841b
commit 32ddeddd48

View File

@@ -8,20 +8,25 @@ const useRepliesAndAccountReplies = (comment: Comment) => {
// the account's replies have a delay before getting published, so get them locally from accountComments instead
const accountRepliesNotYetPublished = useMemo(() => {
const replies = comment?.replies?.pages?.best?.comments || [];
const replyCids = new Set(replies.map((reply: Comment) => reply?.cid));
const pageValues = comment?.replies?.pages ? Object.values(comment.replies.pages) : [];
const firstPageObject = pageValues[0] as { comments?: Comment[] } | undefined;
const publishedComments = firstPageObject?.comments || [];
const replyCids = new Set(publishedComments.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));
}, [comment?.replies?.pages?.best?.comments, accountComments]);
}, [comment?.replies?.pages, accountComments]);
const repliesAndNotYetPublishedReplies = useMemo(() => {
const pageValues = comment?.replies?.pages ? Object.values(comment.replies.pages) : [];
const firstPageObject = pageValues[0] as { comments?: Comment[] } | undefined;
const publishedComments = firstPageObject?.comments || [];
return [
// put the author's unpublished replies at the top, latest first (reverse)
...accountRepliesNotYetPublished.reverse(),
// put the published replies after,
...(comment?.replies?.pages?.best?.comments || []),
...publishedComments,
];
}, [comment?.replies?.pages?.best?.comments, accountRepliesNotYetPublished]);
}, [comment?.replies?.pages, accountRepliesNotYetPublished]);
return repliesAndNotYetPublishedReplies;
};