fix: Incorrect navigation when creating new message with @ (#10930)

* fix(message): Incorrect navigation when creating new message with @

Update variable name from newAssistantStub to newAssistantMessageStub for clarity
Add dispatch calls to update message folding state
Remove unused message length tracking effect in MessageGroup

Fixes #10928

* refactor(MessageGroup): remove unused prevMessageLengthRef variable
This commit is contained in:
Phantom 2025-11-14 11:45:10 +08:00 committed by GitHub
parent b08aecb22b
commit 35aa9d7355
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 28 deletions

View File

@ -11,7 +11,7 @@ import type { Topic } from '@renderer/types'
import type { Message } from '@renderer/types/newMessage' import type { Message } from '@renderer/types/newMessage'
import { classNames } from '@renderer/utils' import { classNames } from '@renderer/utils'
import { Popover } from 'antd' import { Popover } from 'antd'
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react' import { ComponentProps, memo, useCallback, useEffect, useMemo, useState } from 'react'
import styled from 'styled-components' import styled from 'styled-components'
import { useChatMaxWidth } from '../Chat' import { useChatMaxWidth } from '../Chat'
@ -43,9 +43,6 @@ const MessageGroup = ({ messages, topic, registerMessageElement }: Props) => {
) )
const [selectedIndex, setSelectedIndex] = useState(messageLength - 1) const [selectedIndex, setSelectedIndex] = useState(messageLength - 1)
// Refs
const prevMessageLengthRef = useRef(messageLength)
// 对于单模型消息,采用简单的样式,避免 overflow 影响内部的 sticky 效果 // 对于单模型消息,采用简单的样式,避免 overflow 影响内部的 sticky 效果
const multiModelMessageStyle = useMemo( const multiModelMessageStyle = useMemo(
() => (messageLength < 2 ? 'fold' : _multiModelMessageStyle), () => (messageLength < 2 ? 'fold' : _multiModelMessageStyle),
@ -83,24 +80,6 @@ const MessageGroup = ({ messages, topic, registerMessageElement }: Props) => {
}, },
[editMessage, selectedMessageId, setTimeoutTimer] [editMessage, selectedMessageId, setTimeoutTimer]
) )
useEffect(() => {
if (messageLength > prevMessageLengthRef.current) {
setSelectedIndex(messageLength - 1)
const lastMessage = messages[messageLength - 1]
if (lastMessage) {
setSelectedMessage(lastMessage)
}
} else {
const newIndex = messages.findIndex((msg) => msg.id === selectedMessageId)
if (newIndex !== -1) {
setSelectedIndex(newIndex)
}
}
prevMessageLengthRef.current = messageLength
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [messageLength])
// 添加对流程图节点点击事件的监听 // 添加对流程图节点点击事件的监听
useEffect(() => { useEffect(() => {
// 只在组件挂载和消息数组变化时添加监听器 // 只在组件挂载和消息数组变化时添加监听器
@ -223,7 +202,7 @@ const MessageGroup = ({ messages, topic, registerMessageElement }: Props) => {
message, message,
topic, topic,
index: message.index index: message.index
} } satisfies ComponentProps<typeof MessageItem>
const messageContent = ( const messageContent = (
<MessageWrapper <MessageWrapper
@ -277,7 +256,7 @@ const MessageGroup = ({ messages, topic, registerMessageElement }: Props) => {
isGrouped, isGrouped,
topic, topic,
multiModelMessageStyle, multiModelMessageStyle,
messages.length, messages,
selectedMessageId, selectedMessageId,
onUpdateUseful, onUpdateUseful,
groupContextMessageId, groupContextMessageId,

View File

@ -1446,7 +1446,7 @@ export const appendAssistantResponseThunk =
} }
// 2. Create the new assistant message stub // 2. Create the new assistant message stub
const newAssistantStub = createAssistantMessage(assistant.id, topicId, { const newAssistantMessageStub = createAssistantMessage(assistant.id, topicId, {
askId: askId, // Crucial: Use the original askId askId: askId, // Crucial: Use the original askId
model: newModel, model: newModel,
modelId: newModel.id, modelId: newModel.id,
@ -1459,9 +1459,14 @@ export const appendAssistantResponseThunk =
const insertAtIndex = existingMessageIndex !== -1 ? existingMessageIndex + 1 : currentTopicMessageIds.length const insertAtIndex = existingMessageIndex !== -1 ? existingMessageIndex + 1 : currentTopicMessageIds.length
// 4. Update Database (Save the stub to the topic's message list) // 4. Update Database (Save the stub to the topic's message list)
await saveMessageAndBlocksToDB(newAssistantStub, [], insertAtIndex) await saveMessageAndBlocksToDB(newAssistantMessageStub, [], insertAtIndex)
dispatch(newMessagesActions.insertMessageAtIndex({ topicId, message: newAssistantStub, index: insertAtIndex })) dispatch(
newMessagesActions.insertMessageAtIndex({ topicId, message: newAssistantMessageStub, index: insertAtIndex })
)
dispatch(updateMessageAndBlocksThunk(topicId, { id: existingAssistantMessageId, foldSelected: false }, []))
dispatch(updateMessageAndBlocksThunk(topicId, { id: newAssistantMessageStub.id, foldSelected: true }, []))
// 5. Prepare and queue the processing task // 5. Prepare and queue the processing task
const assistantConfigForThisCall = { const assistantConfigForThisCall = {
@ -1475,7 +1480,7 @@ export const appendAssistantResponseThunk =
getState, getState,
topicId, topicId,
assistantConfigForThisCall, assistantConfigForThisCall,
newAssistantStub // Pass the newly created stub newAssistantMessageStub // Pass the newly created stub
) )
}) })
} catch (error) { } catch (error) {