Skip to content

Commit 21ab928

Browse files
committed
refactor: Simplify path construction in MessageTree by enhancing sibling message navigation logic
1 parent 97024cb commit 21ab928

1 file changed

Lines changed: 14 additions & 32 deletions

File tree

src/renderer/src/components/pages/chat/messageTree.ts

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -340,42 +340,24 @@ export class MessageTree {
340340
}
341341

342342
const targetSibling = siblings[siblingBranchIndex]
343-
const messageIndex = this.currentPath.indexOf(messageId)
344-
345-
// 检查是否是根消息的兄弟分支切换
346343
const sourceMessage = this.messageMap.get(messageId)
347-
if (sourceMessage && !sourceMessage.parentId) {
348-
// 根消息的兄弟分支切换:直接从目标兄弟消息开始构建新路径
349-
const newPath = [targetSibling.id]
350-
351-
// 继续沿着这个分支向下,选择最新的子分支
352-
let traverseMessage = targetSibling
353-
while (traverseMessage.children && traverseMessage.children.length > 0) {
354-
const childId = traverseMessage.children.reduce((latest, childId) => {
355-
const child = this.messageMap.get(childId)
356-
const latestChild = this.messageMap.get(latest)
357-
return child && latestChild && child.timestamp > latestChild.timestamp ? childId : latest
358-
})
359-
360-
const childMessage = this.messageMap.get(childId)
361-
if (childMessage) {
362-
newPath.push(childMessage.id)
363-
traverseMessage = childMessage
364-
} else {
365-
break
366-
}
367-
}
368-
369-
this.currentPath = newPath
370-
return newPath
371-
}
372-
373-
if (messageIndex === -1) {
344+
345+
if (!sourceMessage) {
374346
return this.currentPath
375347
}
376348

377-
// 构建新路径:保留到目标兄弟节点之前的路径,然后替换当前消息为目标兄弟消息
378-
const newPath = [...this.currentPath.slice(0, messageIndex), targetSibling.id]
349+
// 构建从根到目标兄弟消息的路径
350+
const pathToParent: string[] = []
351+
let currentMsg: ChatMessage | undefined = sourceMessage.parentId ? this.messageMap.get(sourceMessage.parentId) : undefined
352+
353+
// 从父消息回溯到根
354+
while (currentMsg) {
355+
pathToParent.unshift(currentMsg.id)
356+
currentMsg = currentMsg.parentId ? this.messageMap.get(currentMsg.parentId) : undefined
357+
}
358+
359+
// 新路径 = 到父节点的路径 + 目标兄弟节点
360+
const newPath = [...pathToParent, targetSibling.id]
379361

380362
// 继续沿着这个分支向下,选择最新的子分支
381363
let currentMessage = targetSibling

0 commit comments

Comments
 (0)