From 9d501765f91279547ec930b662ba8d63eeec904c Mon Sep 17 00:00:00 2001 From: DorraJaouad Date: Fri, 23 Feb 2024 18:30:36 +0100 Subject: [PATCH] fixup! Fix(MessagesList): skip soft update when needed and make editing a message softer --- src/components/MessagesList/MessagesList.vue | 27 +++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/components/MessagesList/MessagesList.vue b/src/components/MessagesList/MessagesList.vue index 5fdefefedcb..f0f1922a4a6 100644 --- a/src/components/MessagesList/MessagesList.vue +++ b/src/components/MessagesList/MessagesList.vue @@ -140,7 +140,7 @@ export default { data() { return { /** - * An array of messages grouped in nested arrays by same day and then by author. + * A list of messages grouped by same day and then by author and time. */ messagesGroupedByDateByAuthor: {}, @@ -365,7 +365,7 @@ export default { methods: { prepareMessagesGroups(messages) { - let prevGroupMap = {} + let prevGroupMap = null const groupsByDate = {} let lastMessage = null let groupId = null @@ -393,15 +393,13 @@ export default { messages: [message], token: this.token, dateTimestamp, - previousMessageId: Object.keys(prevGroupMap).length - ? groupsByDate[prevGroupMap.date][prevGroupMap.groupId].messages.at(-1).id - : 0, + previousMessageId: lastMessage?.id || 0, nextMessageId: 0, isSystemMessagesGroup: message.systemMessage.length !== 0, } // Update the previous group with the next message id - if (Object.keys(prevGroupMap).length) { + if (prevGroupMap) { groupsByDate[prevGroupMap.date][prevGroupMap.groupId].nextMessageId = message.id } @@ -437,7 +435,7 @@ export default { const oldGroupsMap = new Map(Object.entries(oldGroups)) Object.entries(newGroups).forEach(([id, newGroup]) => { if (!oldGroupsMap.has(id) || (oldGroupsMap.has(id) && !this.areGroupsIdentical(newGroup, oldGroupsMap.get(id)))) { - this.messagesGroupedByDateByAuthor[newGroup.dateTimestamp][id] = newGroup + this.messagesGroupedByDateByAuthor[dateTimestamp][id] = newGroup } }) @@ -449,7 +447,7 @@ export default { break } if (!newGroupsMap.has(id)) { - delete this.messagesGroupedByDateByAuthor[oldGroup.dateTimestamp][id] + delete this.messagesGroupedByDateByAuthor[dateTimestamp][id] } } } @@ -736,24 +734,30 @@ export default { return } // find the corresponding messages group in the list - const group = Object.values(groups).find(group => group.id <= message.id && (group.nextMessageId >= message.id || group.nextMessageId === 0)) + const group = Object.values(groups).find(group => group.id <= message.id && (group.nextMessageId > message.id || group.nextMessageId === 0)) if (!group) { // Messages were not loaded yet return } + + if (group.messages.length === 1 && group.id === message.id) { + // Nothing to split + this.messagesGroupedByDateByAuthor[dateGroupId][group.id].messages = [message] + return + } + // we split the group in 3 part, // 1. the messages before the edited message (if any) // 2. the edited message // 3. the messages after the edited message (if any) - let nextMessageId = 0 + const nextMessageId = group.nextMessageId const index = group.messages.findIndex(m => m.id === message.id) const before = group.messages.slice(0, index) const after = group.messages.slice(index + 1) // If there are before messages, we keep them in the same group if (before.length) { this.messagesGroupedByDateByAuthor[dateGroupId][group.id].messages = before - nextMessageId = group.nextMessageId this.messagesGroupedByDateByAuthor[dateGroupId][group.id].nextMessageId = message.id } // We create a new group for the edited message @@ -778,7 +782,6 @@ export default { nextMessageId, isSystemMessagesGroup: message.systemMessage.length !== 0, } - this.messagesGroupedByDateByAuthor[dateGroupId][message.id].nextMessageId = newGroupId } },