UNPKG

@replyke/core

Version:

Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.

60 lines 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addCommentsToTree = void 0; const handleError_1 = require("../utils/handleError"); function addSingleCommentToTree(entityCommentsTree, newComment, newlyAdded) { try { if (newComment.parentId) { // Previously, changing the comment sort order and triggering a fresh fetch caused issues: replies from previously loaded comments // would sometimes be re-added to the comment tree, likely due to a state change triggering re-insertion before the UI fully cleared them. // This led to partial items in the tree (replies without their parent comment), resulting in crashes. // // The condition below ensures we don't add replies if their parent comment isn't present. // TODO: This solution prevents errors and serves as a useful safeguard. However, it's worth investigating why replies are re-fetching and attempting to re-add, as this causes redundant server calls, even though it no longer leads to errors. if (!entityCommentsTree[newComment.parentId]) return entityCommentsTree; return { ...entityCommentsTree, [newComment.parentId]: { ...entityCommentsTree[newComment.parentId], replies: { ...(entityCommentsTree[newComment.parentId]?.replies || []), [newComment.id]: { ...newComment, new: !!newlyAdded }, }, }, [newComment.id]: { comment: newComment, replies: {}, new: !!newlyAdded, }, }; } else { return { ...entityCommentsTree, [newComment.id]: { comment: newComment, replies: {}, new: !!newlyAdded, }, }; } } catch (err) { (0, handleError_1.handleError)(err, "Failed to add a comment to the tree"); throw new Error(); } } const addCommentsToTree = (setEntityCommentsTree, newComments, newlyAdded) => { setEntityCommentsTree((prevCommentsTree) => { let newTree = prevCommentsTree; if (newComments) { for (const comment of newComments) { newTree = addSingleCommentToTree(newTree, comment, newlyAdded); } } return newTree; }); }; exports.addCommentsToTree = addCommentsToTree; //# sourceMappingURL=addCommentsToTree.js.map