From b0a2ce0d6710df2085ebabf315d531eba807f6ac Mon Sep 17 00:00:00 2001 From: Khushal Agarwal Date: Tue, 21 May 2024 12:47:38 +0530 Subject: [PATCH] fix: add undefined check for message when checking for isMessageUnread (#2518) * fix: add undefined check for message when checking for isMessageUnread * fix: add undefined check for message when checking for isMessageUnread * fix: simplify case --- package/src/components/MessageList/MessageList.tsx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/package/src/components/MessageList/MessageList.tsx b/package/src/components/MessageList/MessageList.tsx index 9b9731fee7..a07b677b96 100644 --- a/package/src/components/MessageList/MessageList.tsx +++ b/package/src/components/MessageList/MessageList.tsx @@ -612,14 +612,17 @@ const MessageListWithContext = < const isLatestMessageSetShown = !!channel.state.messageSets.find( (set) => set.isCurrent && set.isLatest, ); - const msg = processedMessageList?.[messageArrayIndex]; + if (!isLatestMessageSetShown) { + const msg = processedMessageList?.[messageArrayIndex]; if ( channel.state.latestMessages.length !== 0 && unreadCount > channel.state.latestMessages.length ) { return messageArrayIndex <= unreadCount - channel.state.latestMessages.length - 1; - } else if (lastRead && msg.created_at) { + } + // The `msg` can be undefined here, since `messageArrayIndex` can be out of bounds hence we add a check for `msg`. + else if (lastRead && msg?.created_at) { return lastRead < msg.created_at; } return false;