From f80d21a4bdd3282a00c6338a86dc2301e9135528 Mon Sep 17 00:00:00 2001 From: Marcus Pasell <3690498+rickyrombo@users.noreply.github.com> Date: Tue, 2 May 2023 09:49:37 -0700 Subject: [PATCH] [PAY-1178] Fix new messages on new/unloaded chats (#3298) --- .../common/src/store/pages/chat/slice.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/apps/audius-client/packages/common/src/store/pages/chat/slice.ts b/apps/audius-client/packages/common/src/store/pages/chat/slice.ts index 9eb7355f5f2..26f4d8242ba 100644 --- a/apps/audius-client/packages/common/src/store/pages/chat/slice.ts +++ b/apps/audius-client/packages/common/src/store/pages/chat/slice.ts @@ -164,11 +164,13 @@ const slice = createSlice({ return { ...item, hasTail: hasTail(item, data[index - 1]) } }) // Recalculate hasTail for latest message of new batch - if (state.messages[chatId].ids.length > 0) { + if (state.messages[chatId] && state.messages[chatId].ids.length > 0) { const prevEarliestMessageId = state.messages[chatId].ids[state.messages[chatId].ids.length - 1] - const prevEarliestMessage = - state.messages[chatId].entities[prevEarliestMessageId] + const prevEarliestMessage = getMessage( + state.messages[chatId], + prevEarliestMessageId + ) const newLatestMessage = messagesWithTail[0] newLatestMessage.hasTail = hasTail( newLatestMessage, @@ -306,10 +308,12 @@ const slice = createSlice({ const { chatId, message, status } = action.payload // Recalculate hasTail of previous message - const prevLatestMessageId = state.messages[chatId].ids[0] - const prevLatestMessage = - state.messages[chatId].entities[prevLatestMessageId] - if (prevLatestMessage) { + if (state.messages[chatId] && state.messages[chatId].ids.length > 0) { + const prevLatestMessageId = state.messages[chatId].ids[0] + const prevLatestMessage = getMessage( + state.chats[chatId], + prevLatestMessageId + )! const prevMsgHasTail = hasTail(prevLatestMessage, message) chatMessagesAdapter.updateOne(state.messages[chatId], { id: prevLatestMessageId,