Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ class ChatViewModel @Inject constructor(
(older + current).distinctBy { it.id }.sortedBy { it.createdAt }
}
}
}.onFailure { e ->
_error.value = "Failed to load older messages: ${e.message}"
Comment on lines +348 to +349
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There is a potential race condition here. If the user switches to a different chat while loadMoreMessages is in progress, the error message from the previous chat could be shown in the new chat's UI. You should verify that the chatId for which the request was made still matches the currentChatId before updating the state.

Note: This same check should also be applied to the onSuccess block (lines 340-347) to prevent mixing messages from different chats, although those lines are not part of this specific diff hunk.

            }.onFailure { e ->
                if (chatId == currentChatId) {
                    _error.value = "Failed to load older messages: ${e.message}"
                }

}
_isLoadingMore.value = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal fun ChatMessageList(
}
}
LaunchedEffect(shouldLoadMore.value) {
if (shouldLoadMore.value) onLoadMore()
if (shouldLoadMore.value && !isLoadingMore) onLoadMore()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Adding the !isLoadingMore check prevents redundant calls while a request is in flight. However, since the LaunchedEffect is only keyed on shouldLoadMore.value, if a request fails and isLoadingMore returns to false, the effect will not re-trigger even if the user remains at the top of the list. The user would have to scroll down and back up to try again. Consider adding isLoadingMore to the LaunchedEffect keys or providing a manual retry mechanism in the UI when an error occurs.

}

LazyColumn(
Expand All @@ -84,13 +84,6 @@ internal fun ChatMessageList(
),
reverseLayout = true
) {
if (isLoadingMore) {
item(key = "loading_more") {
Box(modifier = Modifier.fillMaxWidth().padding(Spacing.Small), contentAlignment = Alignment.Center) {
CircularProgressIndicator()
}
}
}
val reversedItems = chatItems.reversed()
itemsIndexed(reversedItems, key = { index, item ->
when (item) {
Expand Down Expand Up @@ -163,5 +156,13 @@ internal fun ChatMessageList(
)
}
}

if (isLoadingMore) {
item(key = "loading_more") {
Box(modifier = Modifier.fillMaxWidth().padding(Spacing.Small), contentAlignment = Alignment.Center) {
CircularProgressIndicator()
}
}
}
}
}
Loading