Skip to content

Commit

Permalink
fix(MessagesList): reinitialize chat if focused messages is out of range
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed Jan 22, 2025
1 parent f307f72 commit 92972aa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/components/MessagesList/MessagesList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,11 @@ export default {

let newTop
if (options?.force) {
if (this.$route?.hash?.startsWith('#message_')) {
// drop the hash, route change will handle scrolling
this.$router.replace({ ...this.$route, hash: '' })
return
}
newTop = this.$refs.scroller.scrollHeight
this.setChatScrolledToBottom(true)
} else if (!this.isSticky) {
Expand Down Expand Up @@ -1290,8 +1295,20 @@ export default {
&& from.token === to.token
&& from.hash !== to.hash) {

// the hash changed, need to focus/highlight another message
if (to.hash && to.hash.startsWith('#message_')) {
if (from.hash?.startsWith('#message_') && !to.hash) {
// the hash is cleared
const lastMessageId = this.conversation.lastMessage.id
if (this.messagesList.find(m => m.id === lastMessageId)) {
this.scrollToBottom({ smooth: false })
} else {
this.$store.dispatch('cancelLookForNewMessages', { requestId: this.chatIdentifier })
this.$store.dispatch('purgeMessagesStore', this.token)
this.$nextTick(async () => {
await this.handleStartGettingMessagesPreconditions(this.token)
})
}
} else if (to.hash?.startsWith('#message_')) {
// the hash changed, need to focus/highlight another message
const focusedId = this.getMessageIdFromHash(to.hash)
if (this.messagesList.find(m => m.id === focusedId)) {
// need some delay (next tick is too short) to be able to run
Expand All @@ -1302,17 +1319,13 @@ export default {
this.focusMessage(focusedId, true)
}, 2)
} else {
// Update environment around context to fill the gaps
this.$store.dispatch('setFirstKnownMessageId', {
token: this.token,
id: focusedId,
})
this.$store.dispatch('setLastKnownMessageId', {
token: this.token,
id: focusedId,
this.isFocusingMessage = true
this.$store.dispatch('cancelLookForNewMessages', { requestId: this.chatIdentifier })
this.$store.dispatch('purgeMessagesStore', this.token)
this.$nextTick(async () => {
await this.handleStartGettingMessagesPreconditions(this.token)
this.setChatScrolledToBottom(false, { auto: true })
})
await this.getMessageContext(this.token, focusedId)
this.focusMessage(focusedId, true)
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/store/messagesStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ const mutations = {
if (state.messages[token]) {
Vue.delete(state.messages, token)
}
if (state.loadedMessages[token]) {
Vue.delete(state.loadedMessages, token)
}
},

/**
Expand Down

0 comments on commit 92972aa

Please sign in to comment.