Commit 0a7d4e3c by Liu

fix:历史记录进入会话后切换工具ID未更新

parent a6ba29ca
......@@ -2,7 +2,7 @@ import React, { useEffect, useRef, useState } from 'react'
import { AnimatePresence, motion } from 'framer-motion'
import { Button, Tooltip } from '@heroui/react'
import { useLocalStorageState, useToggle } from 'ahooks'
import { useLocation, useSearchParams } from 'react-router-dom'
import { useLocation, useNavigate, useSearchParams } from 'react-router-dom'
import { LoginModal } from '../LoginModal'
import type { RootState } from '@/store'
import SendIcon from '@/assets/svg/send.svg?react'
......@@ -42,6 +42,7 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
const [sessionToolId, setSessionToolId] = useState<string | null>(null)
const [searchParams, setSearchParams] = useSearchParams()
const location = useLocation()
const navigate = useNavigate()
const toolIdFromUrl = searchParams.get('toolId')
const fromCollect = location.state?.fromCollect
......@@ -330,6 +331,13 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
const res = await fetchSessionConversationId(requestData)
if (res?.data?.conversationId) {
const conversationId = res.data.conversationId
// 更新路由到新的会话ID
navigate(`/chat/${conversationId}`, {
replace: true,
state: {
toolId: null,
},
})
// 使用获取到的会话ID调用历史会话
await waitForToken()
const qaRes = await fetchUserQaRecordPage(conversationId, '')
......@@ -380,6 +388,13 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
const res = await fetchSessionConversationId(requestData)
if (res?.data?.conversationId) {
const conversationId = res.data.conversationId
// 更新路由到新的会话ID,并携带 toolId 参数
navigate(`/chat/${conversationId}?toolId=${tool.toolId}`, {
replace: true,
state: {
toolId: tool.toolId,
},
})
// 使用获取到的会话ID调用历史会话
await waitForToken()
const qaRes = await fetchUserQaRecordPage(conversationId, tool.toolId)
......
......@@ -25,7 +25,7 @@ const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, c
const navigate = useNavigate()
const location = useLocation()
const { currentConversationId, shouldNavigateToNewConversation, currentToolId } = useAppSelector(state => state.conversation)
const { currentConversationId, shouldNavigateToNewConversation, currentToolId, shouldSendQuestion } = useAppSelector(state => state.conversation)
const { currentConversationId: _tacticsConversationId, shouldNavigateToNewConversation: _tacticsShouldNavigate } = useAppSelector(state => state.tactics)
const handleCreateConversation = () => {
......@@ -127,14 +127,16 @@ const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, c
? `/chat/${currentConversationId}?toolId=${currentToolId}`
: `/chat/${currentConversationId}`
// 通过 location.state 传递 toolId,避免在 Chat 页面被误判为"外链残留参数"而强制清空
// 如果有 shouldSendQuestion,传递 skipHistoryLoad 标记,跳过历史记录加载
navigate(url, {
state: {
toolId: currentToolId || null,
skipHistoryLoad: Boolean(shouldSendQuestion),
},
})
dispatch(clearNavigationFlag())
}
}, [shouldNavigateToNewConversation, currentConversationId, currentToolId, navigate, dispatch])
}, [shouldNavigateToNewConversation, currentConversationId, currentToolId, shouldSendQuestion, navigate, dispatch])
// keep latest conversation id in localStorage (persists across page closes) and sessionStorage (for cross-page returns)
useEffect(() => {
......
......@@ -539,6 +539,18 @@ export const Chat: React.FC = () => {
return
}
// 检查是否跳过历史记录加载(点击常见问题时)
const skipHistoryLoad = Boolean((location.state as { skipHistoryLoad?: boolean } | null)?.skipHistoryLoad)
if (skipHistoryLoad) {
console.log('[Chat] 检测到 skipHistoryLoad 标记,跳过历史记录加载,直接发送问题')
currentIdRef.current = id
// 更新 toolId 相关状态(如果有)
if (initialToolId !== undefined) {
toolIdFromStateRef.current = initialToolId
}
return
}
// 如果 id 没有变化,只是 location.state 变化,需要判断是否需要重新加载历史记录
if (currentIdRef.current === id) {
// 检查是否从收藏页返回(需要重新加载历史记录)
......
......@@ -773,17 +773,32 @@ export const TacticsChat: React.FC = () => {
}
}, [token, dispatch])
// 进入会话后自动触发一次提交,默认 question 为“策略分析”,busiType 02 / recordType A02
// 进入会话后自动触发一次提交,默认 question 为"策略分析",busiType 02 / recordType A02
const hasAutoSubmittedRef = useRef(false)
// 记录上次调用时的 searchParams,用于 orderMeta 场景检测参数变化
const lastSearchParamsRef = useRef<string>('')
useEffect(() => {
if (id) {
// 当 id 变化时,重置自动提交标志
hasAutoSubmittedRef.current = false
lastSearchParamsRef.current = ''
}
}, [id])
useEffect(() => {
// 仅在历史记录查询完成且为空时,才自动触发一次提交
if (currentIdRef.current && !isLoading && hasHistory === false && !hasAutoSubmittedRef.current) {
// 对于 orderMeta 场景:历史记录加载完成后自动触发(无论是否有历史记录),每次进入页面都调用
// 对于其他场景:仅在历史记录查询完成且为空时,才自动触发一次提交
const isOrderMetaScenario = !!orderMeta
const hasHistoryLoaded = hasHistory !== null
const currentSearchParams = location.search
// orderMeta 场景:如果 searchParams 变化了,重置标志以允许重新调用
if (isOrderMetaScenario && lastSearchParamsRef.current !== currentSearchParams) {
hasAutoSubmittedRef.current = false
lastSearchParamsRef.current = currentSearchParams
}
const shouldTriggerForOrderMeta = isOrderMetaScenario && hasHistoryLoaded && !isLoading
const shouldTriggerForNormal = !isOrderMetaScenario && hasHistory === false && !isLoading
if (currentIdRef.current && (shouldTriggerForOrderMeta || shouldTriggerForNormal) && !hasAutoSubmittedRef.current) {
// 如果当前会话是由「清空记录」创建的,则不自动调用提问接口
// 但仍然标记为已处理,避免后续重复触发
if (createdFromClearRef.current) {
......@@ -845,7 +860,7 @@ export const TacticsChat: React.FC = () => {
}
}
}
}, [isLoading, handleSubmitQuestion, id, orderMeta, userMeta, getNumberTypeWithUserMeta, hasHistory])
}, [isLoading, handleSubmitQuestion, id, orderMeta, userMeta, getNumberTypeWithUserMeta, hasHistory, location.search])
// 创建新会话成功后跳转到新会话页面
useEffect(() => {
......
......@@ -130,9 +130,11 @@ const QuestionListBase: React.FC<QuestionListProps & WithAuthProps> = ({
dispatch(setCurrentConversation(targetConversationId))
dispatch(setShouldSendQuestion(item))
// 通过 location.state 传递 toolId,避免 Chat 页面清空 toolId
// 传递 skipHistoryLoad 标记,跳过历史记录加载,直接发送问题
navigate(`/chat/${targetConversationId}`, {
state: {
toolId,
skipHistoryLoad: true,
},
})
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment