Commit 424cc3c1 by Liu

fix:回滚路由逻辑

parent 94a7e78b
import type React from 'react' import type React from 'react'
import { motion } from 'framer-motion' import { motion } from 'framer-motion'
import { useEffect, useRef, useState } from 'react' import { useEffect, useRef, useState } from 'react'
import { useLocation, useNavigate } from 'react-router-dom' import { useNavigate } from 'react-router-dom'
import { useClickAway, useSessionStorageState } from 'ahooks' import { useClickAway, useSessionStorageState } from 'ahooks'
import styles from './Navbar.module.less' import styles from './Navbar.module.less'
import { NavBarItem } from './components/NavBarItem' import { NavBarItem } from './components/NavBarItem'
...@@ -21,7 +21,6 @@ interface NavbarProps { ...@@ -21,7 +21,6 @@ interface NavbarProps {
const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, checkAuth, onSetHistoryVisible }) => { const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, checkAuth, onSetHistoryVisible }) => {
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
const navigate = useNavigate() const navigate = useNavigate()
const location = useLocation()
const { currentConversationId, shouldNavigateToNewConversation, currentToolId } = useAppSelector(state => state.conversation) const { currentConversationId, shouldNavigateToNewConversation, currentToolId } = useAppSelector(state => state.conversation)
...@@ -104,29 +103,11 @@ const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, c ...@@ -104,29 +103,11 @@ const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, c
useEffect(() => { useEffect(() => {
if (shouldNavigateToNewConversation && currentConversationId) { if (shouldNavigateToNewConversation && currentConversationId) {
// 获取当前 URL 的查询参数,保留除 'from' 之外的所有参数 // 如果当前有选中的 toolId,在 URL 中拼接,以便刷新页面后保留工具选择
const currentSearchParams = new URLSearchParams(location.search) const url = currentToolId
const paramsToKeep = new URLSearchParams() ? `/chat/${currentConversationId}?toolId=${currentToolId}`
// 遍历当前查询参数,过滤掉 'from' 参数
currentSearchParams.forEach((value, key) => {
if (key !== 'from') {
paramsToKeep.set(key, value)
}
})
// 如果当前有选中的 toolId,添加到查询参数中
if (currentToolId) {
paramsToKeep.set('toolId', currentToolId)
}
// 构建跳转 URL
const queryString = paramsToKeep.toString()
const url = queryString
? `/chat/${currentConversationId}?${queryString}`
: `/chat/${currentConversationId}` : `/chat/${currentConversationId}`
// 通过 location.state 传递 toolId,避免在 Chat 页面被误判为“外链残留参数”而强制清空
// 通过 location.state 传递 toolId,避免在 Chat 页面被误判为"外链残留参数"而强制清空
navigate(url, { navigate(url, {
state: { state: {
toolId: currentToolId || null, toolId: currentToolId || null,
...@@ -134,7 +115,7 @@ const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, c ...@@ -134,7 +115,7 @@ const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, c
}) })
dispatch(clearNavigationFlag()) dispatch(clearNavigationFlag())
} }
}, [shouldNavigateToNewConversation, currentConversationId, currentToolId, navigate, dispatch, location.search]) }, [shouldNavigateToNewConversation, currentConversationId, currentToolId, navigate, dispatch])
// keep latest conversation id in sessionStorage for cross-page returns (e.g., from collect) // keep latest conversation id in sessionStorage for cross-page returns (e.g., from collect)
useEffect(() => { useEffect(() => {
......
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