Skip to content

Commit

Permalink
Fix comment reply from comment context page clearing context
Browse files Browse the repository at this point in the history
  • Loading branch information
aeharding committed Jul 13, 2023
1 parent 4310b04 commit 43a4e54
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/features/comment/Comments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,25 @@ export default forwardRef<CommentsHandle, CommentsProps>(function Comments(
setPage(currentPage);
}

async function appendComments(comments: CommentView[]) {
setComments((existingComments) =>
uniqBy([...comments, ...existingComments], (c) => c.comment.id)
);
function appendComments(comments: CommentView[]) {
setComments((existingComments) => {
let commentsResult;

// We want to *unshift* comments, so that they appear as first child(ren) of a given node

// if `commentPath` exists, we call `buildCommentsTree` with `true`
if (commentPath) {
// The first comment is considered the root (see `buildCommentsTree(comments, true)`),
// so have to insert at root + 1 instead of at beginning
commentsResult = existingComments.slice(); // don't mutate existing
commentsResult.splice(1, 0, ...comments); // insert after root
} else {
// doesn't matter where inserted into array, put them first
commentsResult = [...comments, ...existingComments];
}

return uniqBy(commentsResult, (c) => c.comment.id);
});
}

async function handleRefresh(event: RefresherCustomEvent) {
Expand Down

0 comments on commit 43a4e54

Please sign in to comment.