Skip to content

Commit

Permalink
refactor(comments): Performance opt: Don't re-render replies every time.
Browse files Browse the repository at this point in the history
  • Loading branch information
skogsmaskin committed Oct 25, 2023
1 parent 85f23a9 commit fe0fe53
Showing 1 changed file with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,38 @@ export const CommentsListItem = React.memo(function CommentsListItem(props: Comm
}
}, [replies])

const renderedReplies = useMemo(
() =>
splicedReplies.map((reply) => (
<Stack as="li" key={reply._id} data-comment-id={reply._id}>
<CommentsListItemLayout
canDelete={reply.authorId === currentUser.id}
canEdit={reply.authorId === currentUser.id}
comment={reply}
currentUser={currentUser}
hasError={reply._state?.type === 'createError'}
isRetrying={reply._state?.type === 'createRetrying'}
mentionOptions={mentionOptions}
onCopyLink={onCopyLink}
onCreateRetry={onCreateRetry}
onDelete={onDelete}
onEdit={onEdit}
readOnly={readOnly}
/>
</Stack>
)),
[
currentUser,
mentionOptions,
onCopyLink,
onCreateRetry,
onDelete,
onEdit,
readOnly,
splicedReplies,
],
)

return (
<Stack space={2} ref={rootRef}>
<StyledThreadCard
Expand Down Expand Up @@ -236,24 +268,7 @@ export const CommentsListItem = React.memo(function CommentsListItem(props: Comm
</Flex>
)}

{splicedReplies.map((reply) => (
<Stack as="li" key={reply._id} data-comment-id={reply._id}>
<CommentsListItemLayout
canDelete={reply.authorId === currentUser.id}
canEdit={reply.authorId === currentUser.id}
comment={reply}
currentUser={currentUser}
hasError={reply._state?.type === 'createError'}
isRetrying={reply._state?.type === 'createRetrying'}
mentionOptions={mentionOptions}
onCopyLink={onCopyLink}
onCreateRetry={onCreateRetry}
onDelete={onDelete}
onEdit={onEdit}
readOnly={readOnly}
/>
</Stack>
))}
{renderedReplies}

{canReply && (
<CommentInput
Expand Down

0 comments on commit fe0fe53

Please sign in to comment.