Skip to content

Commit

Permalink
fixup! Fix(MessagesList): skip soft update when needed and make editi…
Browse files Browse the repository at this point in the history
…ng a message softer
  • Loading branch information
DorraJaouad committed Feb 23, 2024
1 parent 56027f4 commit 9d50176
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/components/MessagesList/MessagesList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand Down Expand Up @@ -365,7 +365,7 @@ export default {

methods: {
prepareMessagesGroups(messages) {
let prevGroupMap = {}
let prevGroupMap = null
const groupsByDate = {}
let lastMessage = null
let groupId = null
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
})

Expand All @@ -449,7 +447,7 @@ export default {
break
}
if (!newGroupsMap.has(id)) {
delete this.messagesGroupedByDateByAuthor[oldGroup.dateTimestamp][id]
delete this.messagesGroupedByDateByAuthor[dateTimestamp][id]
}
}
}
Expand Down Expand Up @@ -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
Expand All @@ -778,7 +782,6 @@ export default {
nextMessageId,
isSystemMessagesGroup: message.systemMessage.length !== 0,
}
this.messagesGroupedByDateByAuthor[dateGroupId][message.id].nextMessageId = newGroupId
}
},

Expand Down

0 comments on commit 9d50176

Please sign in to comment.