onCommentRemoved method

void onCommentRemoved(
  1. CommentData comment
)

Handles the removal of a comment by ID.

Implementation

void onCommentRemoved(CommentData comment) {
  final removeIndex = state.comments.indexWhere((it) => it.id == comment.id);

  // If found at the top level, remove it directly
  if (removeIndex >= 0) {
    final updatedComments = [...state.comments].apply(
      (it) => it.removeAt(removeIndex),
    );

    state = state.copyWith(comments: updatedComments);
    return;
  }

  // Otherwise, it might be a direct reply to a top-level comment
  final updatedComments = state.comments.updateNested(
    (it) => it.id == comment.parentId,
    children: (it) => it.replies ?? [],
    update: (found) => found.removeReply(comment),
    updateChildren: (parent, replies) => parent.copyWith(replies: replies),
  );

  state = state.copyWith(comments: updatedComments);
}