From c7838952d764a874af788348e486fabeee12afe7 Mon Sep 17 00:00:00 2001
From: Audric Ackermann <audric@loki.network>
Date: Tue, 31 Oct 2023 15:54:15 +1100
Subject: [PATCH] fix: update redux mostRecentMessageid anytime a message
 changes/added

---
 ts/state/ducks/conversations.ts | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/ts/state/ducks/conversations.ts b/ts/state/ducks/conversations.ts
index 8fd12de43..606de9f7d 100644
--- a/ts/state/ducks/conversations.ts
+++ b/ts/state/ducks/conversations.ts
@@ -525,6 +525,7 @@ function handleMessageChangedOrAdded(
   );
   if (messageInStoreIndex >= 0) {
     state.messages[messageInStoreIndex] = changedOrAddedMessageProps;
+    state.mostRecentMessageId = updateMostRecentMessageId(state);
 
     return state;
   }
@@ -535,12 +536,30 @@ function handleMessageChangedOrAdded(
   if (state.showScrollButton) {
     return state;
   }
+
   // sorting happens in the selector
 
-  state.messages.push(changedOrAddedMessageProps);
+  state.mostRecentMessageId = updateMostRecentMessageId(state);
   return state;
 }
 
+function updateMostRecentMessageId(state: ConversationsStateType) {
+  // update the most recent message id as this is the one used to display the last MessageStatus
+  let foundSoFarMaxId = '';
+  let foundSoFarMaxTimestamp = 0;
+
+  state.messages.forEach(m => {
+    if (
+      (m.propsForMessage.serverTimestamp || m.propsForMessage.timestamp || 0) >
+      foundSoFarMaxTimestamp
+    ) {
+      foundSoFarMaxId = m.propsForMessage.id;
+      foundSoFarMaxTimestamp = m.propsForMessage.serverTimestamp || m.propsForMessage.timestamp;
+    }
+  });
+  return foundSoFarMaxId;
+}
+
 function handleMessagesChangedOrAdded(
   state: ConversationsStateType,
   payload: Array<MessageModelPropsWithoutConvoProps>