Files
5chan/src/utils/renderThreadComments.js
T

23 lines
861 B
JavaScript
Raw Normal View History

2023-04-16 18:07:20 +02:00
function renderThreadComments(comment, pendingComment = null) {
const nestedComments = comment?.replies?.pages?.topAll?.comments || {};
const commentKeys = Object.keys(nestedComments);
2023-03-08 09:49:42 +01:00
const renderedComments = commentKeys.map(key => {
2023-04-16 18:07:20 +02:00
const comment = nestedComments[key];
const { replies: { pages: { topAll: { comments: childNestedComments = [] } = {} } = {} } = {} } = comment;
if (comment.replyCount > 0 && childNestedComments) {
const renderedNestedComments = renderThreadComments(comment);
2023-03-08 09:49:42 +01:00
return [comment, ...renderedNestedComments];
}
return [comment];
}).flat();
2023-04-16 18:07:20 +02:00
if (pendingComment?.parentCid === comment?.cid) {
renderedComments.push(pendingComment);
}
2023-03-08 09:49:42 +01:00
const sortedComments = renderedComments.sort((a, b) => a.timestamp - b.timestamp);
return sortedComments;
}
export default renderThreadComments;