Commit 35fc01e1 by Liu

fix:清空缓存逻辑

parent 9fa0ba6e
......@@ -144,7 +144,8 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
const storedToolId = safeSessionStorageGetItem('currentToolId')
// 如果 currentToolId 是空字符串,视为 null,确保通用模式能正确高亮
setSessionToolId(storedToolId && storedToolId.trim() ? storedToolId : null)
}, [toolIdFromUrl])
// 当路由切换(如点击历史记录)时,同步最新的 sessionStorage,避免同标签页删除后状态不同步
}, [toolIdFromUrl, location.pathname])
const startAnimation = () => {
intervalRef.current = setInterval(() => {
......
......@@ -168,6 +168,15 @@ export const Home: React.FC = () => {
if (_loginCode) {
res = await fetchLoginByToken(_loginCode)
if (res.data) {
// 登录成功后先清理旧状态,避免沿用上一次的工具模式
dispatch(clearCurrentToolId())
safeSessionStorageRemoveItem('currentToolId')
const currentUrl = new URL(window.location.href)
if (currentUrl.searchParams.has('toolId')) {
currentUrl.searchParams.delete('toolId')
// 使用 replace 避免产生新的历史记录
window.history.replaceState({}, '', currentUrl.toString())
}
setToken(res.data.token)
// 主动触发 storage 事件,确保其他组件能监听到变化
window.dispatchEvent(
......@@ -179,25 +188,13 @@ export const Home: React.FC = () => {
storageArea: localStorage,
}),
)
// 登录成功后强制重置为通用模式:清除所有 toolId 相关状态
// 1. 清除 Redux 中的 currentToolId
dispatch(clearCurrentToolId())
// 2. 清除 sessionStorage 中的 currentToolId
safeSessionStorageRemoveItem('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 强制重置为通用模式
// 触发自定义事件,通知 ChatEditor 强制重置为通用模式
window.dispatchEvent(new CustomEvent('forceResetToGeneralMode'))
initConversation()
dispatch(fetchConversations())
}
}
else {
else {
initConversation()
dispatch(fetchConversations())
}
......@@ -206,6 +203,14 @@ export const Home: React.FC = () => {
// 模拟登录 可以用来测试
res = await fetchLoginByUid('123123')
if (res.data) {
// 登录成功后先清理旧状态,避免沿用上一次的工具模式
dispatch(clearCurrentToolId())
safeSessionStorageRemoveItem('currentToolId')
const currentUrl = new URL(window.location.href)
if (currentUrl.searchParams.has('toolId')) {
currentUrl.searchParams.delete('toolId')
window.history.replaceState({}, '', currentUrl.toString())
}
setToken(res.data.token)
// 主动触发 storage 事件,确保其他组件能监听到变化
window.dispatchEvent(
......@@ -217,18 +222,7 @@ export const Home: React.FC = () => {
storageArea: localStorage,
}),
)
// 登录成功后强制重置为通用模式:清除所有 toolId 相关状态
// 1. 清除 Redux 中的 currentToolId
dispatch(clearCurrentToolId())
// 2. 清除 sessionStorage 中的 currentToolId
safeSessionStorageRemoveItem('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 强制重置为通用模式
// 触发自定义事件,通知 ChatEditor 强制重置为通用模式
window.dispatchEvent(new CustomEvent('forceResetToGeneralMode'))
initConversation()
dispatch(fetchConversations())
......
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