Skip to content

Commit

Permalink
fixup! fixup! Fix(MessagesList): skip soft update when needed and mak…
Browse files Browse the repository at this point in the history
…e editing a message softer
  • Loading branch information
DorraJaouad committed Feb 22, 2024
1 parent be75afa commit b4058a2
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/components/MessagesList/MessagesList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,19 @@ export default {
token(newToken, oldToken) {
// Expire older messages when navigating to another conversation
this.$store.dispatch('easeMessageList', { token: oldToken })
this.messagesGroupedByDateByAuthor = this.prepareMessagesGroups(this.messagesList)
},

messagesList: {
immediate: true,
handler(newMessages, oldMessages) {
// token watcher will handle the conversations change
if (oldMessages?.length && newMessages.length && newMessages[0].token !== oldMessages?.at(0)?.token) {
return
}
const newGroups = this.prepareMessagesGroups(newMessages)
// Check if messages token has changed or if messages were just loaded
if (!oldMessages || (newMessages.length && newMessages[0].token !== oldMessages?.at(0)?.token)) {
// messages were just loaded
if (!oldMessages) {
this.messagesGroupedByDateByAuthor = newGroups
} else {
this.softUpdateByDateGroups(this.messagesGroupedByDateByAuthor, newGroups)
Expand Down Expand Up @@ -399,24 +404,37 @@ export default {
softUpdateByDateGroups(oldGroups, newGroups) {
const oldGroupsMap = new Map(Object.entries(oldGroups))
// Check if we have this group in the old list already and it is unchanged
return Object.keys(newGroups).forEach(dateTimestamp => {
Object.keys(newGroups).forEach(dateTimestamp => {
if (oldGroupsMap.has(dateTimestamp)) {
// the group by date has changed, we update its content (groups by author)
this.softUpdateAuthorGroups(oldGroupsMap.get(dateTimestamp), newGroups[dateTimestamp])
this.softUpdateAuthorGroups(oldGroupsMap.get(dateTimestamp), newGroups[dateTimestamp], dateTimestamp)
} else {
// the group is new
this.messagesGroupedByDateByAuthor[dateTimestamp] = newGroups[dateTimestamp]
}
})
},

softUpdateAuthorGroups(oldGroups, newGroups) {
softUpdateAuthorGroups(oldGroups, newGroups, dateTimestamp) {
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
}
})

// Remove temporary messages that are not in the new list
if (dateTimestamp == moment().startOf('day').unix()) {

Check failure on line 427 in src/components/MessagesList/MessagesList.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Expected '===' and instead saw '=='
const newGroupsMap = new Map(Object.entries(newGroups))
for (const [id, oldGroup] of Object.entries(oldGroups).reverse()) {
if (!id.toString().startsWith('temp-')) {
break
}
if (!newGroupsMap.has(id)) {
delete this.messagesGroupedByDateByAuthor[oldGroup.dateTimestamp][id]
}
}
}
},

areGroupsIdentical(group1, group2) {
Expand Down

0 comments on commit b4058a2

Please sign in to comment.