Commit cc4a3caf by Liu

fix: reset chat tool id state

parent cf37e68e
......@@ -12,7 +12,7 @@ import { ChatEditor } from '@/components/ChatEditor'
import type { ChatRecord } from '@/types/chat'
import { fetchUserQaRecordPage } from '@/api/conversation'
import { fetchCheckTokenApi, fetchStreamResponse } from '@/api/chat'
import { fetchToolList } from '@/api/home'
import { fetchEfficiencyQuestionList, fetchToolList } from '@/api/home'
// import { mockFetchToolList } from '@/api/mock/home'
import { clearCurrentToolId, clearShouldSendQuestion, fetchConversations, setCurrentToolId } from '@/store/conversationSlice'
import { getUserRolesForApi } from '@/lib/utils'
......@@ -25,9 +25,11 @@ import SdreamLoading from '@/components/SdreamLoading'
export const Chat: React.FC = () => {
const { id } = useParams<{ id: string }>()
const location = useLocation()
const [searchParams] = useSearchParams()
const [searchParams, setSearchParams] = useSearchParams()
// 优先从 URL 查询参数读取 toolId(刷新后仍能保留),其次从 location.state 读取
const toolIdFromUrl = searchParams.get('toolId')
const rawToolIdFromUrl = searchParams.get('toolId')
const shouldForceClearUrlToolId = Boolean(rawToolIdFromUrl)
const toolIdFromUrl = shouldForceClearUrlToolId ? null : rawToolIdFromUrl
// 添加调试日志,查看 location.state 的实际值
// eslint-disable-next-line no-console
console.log('[Chat] location.state:', location.state)
......@@ -52,6 +54,72 @@ export const Chat: React.FC = () => {
const [currentToolName, setCurrentToolName] = useState<string | undefined>(undefined)
// 使用 ref 保存从 location.state 传递的 toolId,避免被异步操作覆盖
const toolIdFromStateRef = useRef<string | null | undefined>(undefined)
// 若链接中带有 toolId,进入页面后强制清空(避免沿用历史链接参数)
useEffect(() => {
if (!shouldForceClearUrlToolId)
return
// 1. 清除 Redux 中的 currentToolId
dispatch(clearCurrentToolId())
// 2. 清除 sessionStorage 中的 currentToolId
sessionStorage.removeItem('currentToolId')
// 3. 清除 URL 中的 toolId 参数(如果存在)
const currentUrl = new URL(window.location.href)
if (currentUrl.searchParams.has('toolId')) {
currentUrl.searchParams.delete('toolId')
window.history.replaceState({}, '', currentUrl.toString())
}
// 4. 触发自定义事件,通知 ChatEditor 强制重置为通用模式
window.dispatchEvent(new CustomEvent('forceResetToGeneralMode'))
// 同步 react-router 的 searchParams 状态
const newParams = new URLSearchParams(searchParams)
newParams.delete('toolId')
setSearchParams(newParams, { replace: true })
}, [searchParams, setSearchParams, shouldForceClearUrlToolId, dispatch])
// 调试用:展示当前页面链接与缓存中的 toolId
const [debugInfo, setDebugInfo] = useState<{ href: string, sessionToolId: string | null }>({
href: '',
sessionToolId: null,
})
// 首次进入 /chat/:id 时,拉取一次通用模式下的常见问题(toolId: ''),后续仍按现有逻辑走
useEffect(() => {
;(async () => {
try {
// 仅在本标签页首次进入时调用一次
if (sessionStorage.getItem('__INITIAL_FAQ_LOADED__'))
return
sessionStorage.setItem('__INITIAL_FAQ_LOADED__', 'true')
await fetchEfficiencyQuestionList({ toolId: '' })
}
catch (error) {
console.error('初始化通用模式常见问题失败:', error)
}
})()
}, [])
// 进入聊天页时,同步当前链接和缓存中的 toolId 到页面上展示
useEffect(() => {
try {
const href = window.location.href
const sessionToolId = sessionStorage.getItem('currentToolId')
setDebugInfo({
href,
sessionToolId,
})
}
catch (error) {
console.error('同步页面链接与缓存信息失败:', error)
}
}, [])
useEffect(() => {
if (!debugInfo.href && !debugInfo.sessionToolId)
return
// eslint-disable-next-line no-console
console.debug('[Chat] 当前链接 / 缓存 toolId:', debugInfo)
}, [debugInfo])
// 历史记录点击时将 toolId 通过路由 state 传入,优先使用该值快速同步高亮
useEffect(() => {
......
......@@ -155,7 +155,7 @@ const QuestionListBase: React.FC<QuestionListProps & WithAuthProps> = ({
)
: null}
</h3>
{isLoaded && questions && questions.length === 0
{((isLoaded && questions && questions.length === 0) || (isToolBtn && (!questions || questions.length === 0)))
? (
<div className="mt-[34px] flex flex-col items-center justify-center h-[200px]">
<div className="flex flex-col items-center">
......
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