From 6c012f93ce8ba9ee941c2e5ab82d5ae25d8d49ef Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Wed, 9 Oct 2024 11:46:01 +0530 Subject: [PATCH] fix(common): restore support for collapsing child comments Fixes: 86400c352baa ("feat(comments): remember comment collapsed state") --- .../claw/common/comments/CommentsHandler.kt | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsHandler.kt b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsHandler.kt index eef9a0b6..e4f52433 100644 --- a/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsHandler.kt +++ b/common/src/main/kotlin/dev/msfjarvis/claw/common/comments/CommentsHandler.kt @@ -47,14 +47,22 @@ internal class CommentsHandler { } fun updateListNode(shortId: String, isExpanded: Boolean) { + fun updateNode(node: CommentNode): CommentNode { + if (node.comment.shortId == shortId) { + return node.copy(isExpanded = isExpanded) + } + val updatedChildren = node.children.map { updateNode(it) }.toMutableList() + return node.copy(children = updatedChildren) + } + val listNode = _listItems.value.toMutableList() - val index = listNode.indexOfFirst { it.comment.shortId == shortId } - - if (index != -1) { - val commentNode = listNode[index].copy(isExpanded = isExpanded) - listNode[index] = commentNode - - _listItems.value = listNode.toList() + for (i in listNode.indices) { + val node = listNode[i] + if (node.comment.shortId == shortId || node.children.any { it.comment.shortId == shortId }) { + listNode[i] = updateNode(node) + _listItems.value = listNode.toList() + return + } } } }