diff --git a/packages/app/src/app/overmind/namespaces/comments/state.ts b/packages/app/src/app/overmind/namespaces/comments/state.ts index 023e029dc8f..0e49bbd69c8 100644 --- a/packages/app/src/app/overmind/namespaces/comments/state.ts +++ b/packages/app/src/app/overmind/namespaces/comments/state.ts @@ -84,11 +84,33 @@ export const state: State = { return null; } + function sortByInsertedAt( + commentA: CommentFragment | null, + commentB: CommentFragment | null + ) { + if (!commentA || !commentB) { + return 0; + } + + const aDate = new Date(commentA.insertedAt); + const bDate = new Date(commentB.insertedAt); + + if (aDate > bDate) { + return 1; + } + + if (bDate < aDate) { + return -1; + } + + return 0; + } + return { ...comments[currentSandbox.id][currentCommentId], - comments: comments[currentSandbox.id][currentCommentId].comments.map( - commentId => comments[currentSandbox.id][commentId.id] || null - ), + comments: comments[currentSandbox.id][currentCommentId].comments + .map(commentId => comments[currentSandbox.id][commentId.id] || null) + .sort(sortByInsertedAt), }; }, selectedCommentsFilter: CommentsFilterOption.OPEN,