updated renderComments to shorten replies section

This commit is contained in:
plebbitor
2023-03-05 15:36:38 +01:00
parent 6ca67fc775
commit 66fdb62bc1
2 changed files with 31 additions and 16 deletions
+29
View File
@@ -0,0 +1,29 @@
function renderComments(comments) {
let displayedCount = 0;
let omittedCount = 0;
const renderComment = (comment) => {
const { replyCount, replies: { pages: { topAll: { comments: nestedComments } } } } = comment;
let renderedNestedComments = [];
if (replyCount > 0 && nestedComments.length > 0) {
const result = renderComments(nestedComments);
renderedNestedComments = result.renderedComments;
omittedCount += result.omittedCount;
}
const allComments = [comment, ...renderedNestedComments];
const displayedComments = allComments.slice(0, 5 - displayedCount);
displayedCount += displayedComments.length;
omittedCount += allComments.length - displayedComments.length;
return displayedComments;
};
const renderedComments = comments.flatMap(renderComment);
return { renderedComments, omittedCount };
}
export default renderComments;