@@ -106,6 +106,14 @@ export function useGetMessagesProvider() {
106106 } )
107107
108108 const conversationLastMessageId = computed < number > ( ( ) => {
109+ if ( threadId ) {
110+ const threadInfo = chatExtrasStore . threads [ currentToken . value ] ?. [ threadId ]
111+ // If threadId is set, we should compare with last message from the thread
112+ if ( threadInfo ) {
113+ return threadInfo . last ?. id ?? threadId
114+ }
115+ }
116+
109117 if ( conversation . value ?. lastMessage && 'id' in conversation . value . lastMessage ) {
110118 return conversation . value . lastMessage . id
111119 }
@@ -123,14 +131,6 @@ export function useGetMessagesProvider() {
123131
124132 const lastKnownMessageId = chatStore . getLastKnownId ( currentToken . value , { messageId : contextMessageId . value , threadId : contextThreadId . value } )
125133
126- if ( contextThreadId . value ) {
127- const threadInfo = chatExtrasStore . threads [ currentToken . value ] ?. [ contextThreadId . value ]
128- // If threadId is set, we should compare with last message from the thread
129- if ( threadInfo ?. last ) {
130- return lastKnownMessageId >= threadInfo . last . id
131- }
132- }
133-
134134 return lastKnownMessageId >= conversationLastMessageId . value
135135 } )
136136
@@ -226,12 +226,14 @@ export function useGetMessagesProvider() {
226226 if ( from . hash !== to . hash && focusMessageId !== null ) {
227227 // the hash changed, need to focus/highlight another message
228228 contextMessageId . value = focusMessageId
229- } else if ( conversation . value ?. lastReadMessage && conversation . value . lastReadMessage > contextMessageId . value ) {
230- // focus last read message first
231- contextMessageId . value = conversation . value . lastReadMessage
232229 } else {
233- // last known message in the most recent block store
234- contextMessageId . value = conversationLastMessageId . value
230+ // try to focus last read message first, otherwise scroll to last known message in the most recent block store
231+ const hasLastReadMessageInContextBelow = conversation . value ?. lastReadMessage && conversation . value . lastReadMessage > contextMessageId . value
232+ && ( ! contextThreadId . value || chatStore . hasMessage ( to . params . token , { messageId : conversation . value . lastReadMessage , threadId : contextThreadId . value } ) )
233+
234+ contextMessageId . value = hasLastReadMessageInContextBelow
235+ ? conversation . value . lastReadMessage
236+ : contextMessageId . value = conversationLastMessageId . value
235237 }
236238
237239 await checkContextAndFocusMessage ( to . params . token , contextMessageId . value , contextThreadId . value )
0 commit comments