Commit ec975429 by Liu

fix:关闭后管标签时清空toolId

parent e015bccc
......@@ -155,10 +155,26 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
}
syncSessionToolId()
window.addEventListener('storage', syncSessionToolId)
// 监听强制重置为通用模式的事件(登录成功后触发)
const handleForceReset = () => {
// 强制同步 sessionStorage(此时应该已经被清除了)
syncSessionToolId()
// 确保 Redux 也被清除(如果还没有)
if (currentToolId) {
dispatch(clearCurrentToolId())
}
// 强制设置为通用模式
setSelectedToolId(null)
setIsToolBtnActive(true)
}
window.addEventListener('forceResetToGeneralMode', handleForceReset)
return () => {
window.removeEventListener('storage', syncSessionToolId)
window.removeEventListener('forceResetToGeneralMode', handleForceReset)
}
}, [])
}, [currentToolId, dispatch])
// 当路由变化时,同步更新 sessionToolId(因为 storage 事件不会在同标签页触发)
useEffect(() => {
......
......@@ -85,24 +85,19 @@ export const Home: React.FC = () => {
}))
setIsDataLoaded(false) // 重置加载状态
try {
/**
* 获取最终用于请求的 toolId:
* - 如果传入了 toolId:
* - 通用模式(isToolBtn === true):保持传入值(可能是空字符串),不再从 sessionStorage / URL 回退
* - 工具模式(isToolBtn === false):如果传入为空,再从 sessionStorage / URL 回退
* - 如果没有传入 toolId:保留原有回退逻辑
*/
let finalToolId = toolId || ''
const storedToolId = sessionStorage.getItem('currentToolId') || ''
const searchParams = new URLSearchParams(location.search)
const urlToolId = searchParams.get('toolId') || ''
const shouldForceClearToolId = !storedToolId && !urlToolId
if (!finalToolId && !isToolBtn) {
let finalToolId = toolId || ''
if (shouldForceClearToolId && !isToolBtn) {
finalToolId = ''
}
else if (!finalToolId && !isToolBtn) {
// 仅在工具模式下才使用回退逻辑,避免通用模式误用上一次的 toolId
finalToolId = sessionStorage.getItem('currentToolId') || ''
if (!finalToolId) {
const searchParams = new URLSearchParams(location.search)
finalToolId = searchParams.get('toolId') || ''
}
finalToolId = storedToolId || urlToolId
}
const res = await fetchEfficiencyQuestionList({ toolId: finalToolId })
if (res && res.data && res.data.questions) {
setOtherQuestions((prev: any) => ({
......@@ -171,7 +166,20 @@ export const Home: React.FC = () => {
storageArea: localStorage,
}),
)
sessionStorage.setItem('currentToolId', '')
// 登录成功后强制重置为通用模式:清除所有 toolId 相关状态
// 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')
// 使用 replace 避免产生新的历史记录
window.history.replaceState({}, '', currentUrl.toString())
}
// 4. 触发自定义事件,通知 ChatEditor 强制重置为通用模式
window.dispatchEvent(new CustomEvent('forceResetToGeneralMode'))
initConversation()
dispatch(fetchConversations())
}
......@@ -196,11 +204,24 @@ export const Home: React.FC = () => {
storageArea: localStorage,
}),
)
// 登录成功后强制重置为通用模式:清除所有 toolId 相关状态
// 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'))
initConversation()
dispatch(fetchConversations())
}
}
}, [setToken])
}, [setToken, dispatch])
// 修改 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