Commit 096be30e by Liu

fix: nav create

parent d19da468
...@@ -25,8 +25,11 @@ const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, c ...@@ -25,8 +25,11 @@ const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ isHistoryVisible, c
const { currentConversationId, shouldNavigateToNewConversation, currentToolId } = useAppSelector(state => state.conversation) const { currentConversationId, shouldNavigateToNewConversation, currentToolId } = useAppSelector(state => state.conversation)
const handleCreateConversation = () => { const handleCreateConversation = () => {
const sessionToolId = sessionStorage.getItem('currentToolId') || undefined
dispatch(createConversation({ dispatch(createConversation({
conversationData: {}, conversationData: {
...(sessionToolId ? { toolId: sessionToolId } : {}),
},
shouldNavigate: true, shouldNavigate: true,
shouldSendQuestion: '', shouldSendQuestion: '',
})) }))
......
...@@ -27,17 +27,23 @@ export const Chat: React.FC = () => { ...@@ -27,17 +27,23 @@ export const Chat: React.FC = () => {
const [searchParams, setSearchParams] = useSearchParams() const [searchParams, setSearchParams] = useSearchParams()
// 优先从 URL 查询参数读取 toolId(刷新后仍能保留),其次从 location.state 读取 // 优先从 URL 查询参数读取 toolId(刷新后仍能保留),其次从 location.state 读取
const rawToolIdFromUrl = searchParams.get('toolId') const rawToolIdFromUrl = searchParams.get('toolId')
// 20251211 调试:记录进入聊天页时 URL 与 state 的 toolId 来源
// eslint-disable-next-line no-console
console.log('20251211 Chat url/state init', {
rawToolIdFromUrl,
locationState: location.state,
})
// 只有在非 SPA 导航(location.state 不存在)且链接携带 toolId 时才清空,避免影响站内点击历史记录 // 只有在非 SPA 导航(location.state 不存在)且链接携带 toolId 时才清空,避免影响站内点击历史记录
const shouldForceClearUrlToolId = !location.state && Boolean(rawToolIdFromUrl) const shouldForceClearUrlToolId = !location.state && Boolean(rawToolIdFromUrl)
const toolIdFromUrl = shouldForceClearUrlToolId ? null : rawToolIdFromUrl const toolIdFromUrl = shouldForceClearUrlToolId ? null : rawToolIdFromUrl
// 添加调试日志,查看 location.state 的实际值 // 添加调试日志,查看 location.state 的实际值
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log('[Chat] location.state:', location.state) console.log('20251211 [Chat] location.state:', location.state)
const toolIdFromState = (location.state as { toolId?: string | null } | null)?.toolId const toolIdFromState = (location.state as { toolId?: string | null } | null)?.toolId
// 优先使用 URL 中的 toolId,其次使用 state 中的 toolId // 优先使用 URL 中的 toolId,其次使用 state 中的 toolId
const initialToolId = toolIdFromUrl !== null ? toolIdFromUrl : toolIdFromState const initialToolId = toolIdFromUrl !== null ? toolIdFromUrl : toolIdFromState
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log('[Chat] initialToolId:', { console.log('20251211[Chat] initialToolId:', {
fromUrl: toolIdFromUrl, fromUrl: toolIdFromUrl,
fromState: toolIdFromState, fromState: toolIdFromState,
final: initialToolId, final: initialToolId,
...@@ -59,6 +65,9 @@ export const Chat: React.FC = () => { ...@@ -59,6 +65,9 @@ export const Chat: React.FC = () => {
// 视为一次新的会话入口:重置为通用模式,清除历史遗留的工具模式状态 // 视为一次新的会话入口:重置为通用模式,清除历史遗留的工具模式状态
useEffect(() => { useEffect(() => {
if (!location.state && !rawToolIdFromUrl) { if (!location.state && !rawToolIdFromUrl) {
// 20251211 调试:无 URL、无 state 时清除 toolId
// eslint-disable-next-line no-console
console.log('20251211 Chat clear toolId (no state & no url)')
dispatch(clearCurrentToolId()) dispatch(clearCurrentToolId())
// sessionStorage.removeItem('currentToolId') // sessionStorage.removeItem('currentToolId')
} }
...@@ -68,6 +77,13 @@ export const Chat: React.FC = () => { ...@@ -68,6 +77,13 @@ export const Chat: React.FC = () => {
if (!shouldForceClearUrlToolId) if (!shouldForceClearUrlToolId)
return return
// 20251211 调试:URL 携带 toolId 且需要强制清空
// eslint-disable-next-line no-console
console.log('20251211 Chat force clear url toolId', {
rawToolIdFromUrl,
shouldForceClearUrlToolId,
})
// 1. 清除 Redux 中的 currentToolId // 1. 清除 Redux 中的 currentToolId
dispatch(clearCurrentToolId()) dispatch(clearCurrentToolId())
// 2. 清除 sessionStorage 中的 currentToolId // 2. 清除 sessionStorage 中的 currentToolId
......
...@@ -110,34 +110,6 @@ export const Home: React.FC = () => { ...@@ -110,34 +110,6 @@ export const Home: React.FC = () => {
} }
}, [location.search]) }, [location.search])
// 首页首次挂载时拉取常见问题(强制 toolId 为空,避免带入历史值)
const handleInitialQuestions = useCallback(async () => {
// eslint-disable-next-line no-console
console.log('handleInitialQuestions: 首次挂载/登录后初始化常见问题,强制 toolId 为空')
setOtherQuestions((prev: any) => ({
...prev,
content: [],
}))
setIsDataLoaded(false)
try {
const res = await fetchEfficiencyQuestionList({
toolId: '',
})
if (res && res.data && res.data.questions) {
setOtherQuestions((prev: any) => ({
...prev,
content: res.data.questions || [],
}))
}
}
catch (error) {
console.error('首页初始化常见问题失败:', error)
}
finally {
setIsDataLoaded(true)
}
}, [])
// 处理工具按钮点击 // 处理工具按钮点击
const _handleToolClick = useCallback(async (isToolBtn: boolean, toolId?: string, ignoreUrlToolId?: boolean) => { const _handleToolClick = useCallback(async (isToolBtn: boolean, toolId?: string, ignoreUrlToolId?: boolean) => {
// 提质增效模式 / 数据助手 / 通用模式:都先清空数据,重新拉常见问题 // 提质增效模式 / 数据助手 / 通用模式:都先清空数据,重新拉常见问题
...@@ -148,10 +120,15 @@ export const Home: React.FC = () => { ...@@ -148,10 +120,15 @@ export const Home: React.FC = () => {
setIsDataLoaded(false) // 重置加载状态 setIsDataLoaded(false) // 重置加载状态
try { try {
const storedToolId = safeSessionStorageGetItem('currentToolId') || '' const storedToolId = safeSessionStorageGetItem('currentToolId') || ''
const searchParams = new URLSearchParams(location.search) // eslint-disable-next-line no-console
console.log('20251211 _handleToolClick before calc', {
isToolBtn,
toolId,
ignoreUrlToolId,
storedToolId,
})
// 首页初始化加载常见问题时,允许忽略路由中的 toolId,避免带入上一次的工具 ID // 首页初始化加载常见问题时,允许忽略路由中的 toolId,避免带入上一次的工具 ID
const urlToolId = ignoreUrlToolId ? '' : (searchParams.get('toolId') || '') const shouldForceClearToolId = !storedToolId
const shouldForceClearToolId = !storedToolId && !urlToolId
let finalToolId = toolId || '' let finalToolId = toolId || ''
...@@ -165,13 +142,20 @@ export const Home: React.FC = () => { ...@@ -165,13 +142,20 @@ export const Home: React.FC = () => {
} }
else if (!finalToolId && !isToolBtn) { else if (!finalToolId && !isToolBtn) {
// 仅在工具模式下才使用回退逻辑,避免通用模式误用上一次的 toolId // 仅在工具模式下才使用回退逻辑,避免通用模式误用上一次的 toolId
finalToolId = storedToolId || urlToolId finalToolId = storedToolId
} }
// 调用真实 API 获取常见问题列表,使用 sessionStorage 中的 currentToolId // 调用真实 API 获取常见问题列表,优先使用 sessionStorage 中的 currentToolId
const sessionToolId = safeSessionStorageGetItem('currentToolId') || '' const sessionToolId = safeSessionStorageGetItem('currentToolId') || ''
// 调试:记录最终用于请求的 toolId
// 20251211 调试:记录最终用于请求的 toolId
// eslint-disable-next-line no-console
console.log('20251211 _handleToolClick fetch', {
sessionToolId,
finalToolId,
})
const res = await fetchEfficiencyQuestionList({ const res = await fetchEfficiencyQuestionList({
toolId: sessionToolId, toolId: sessionToolId || finalToolId,
}) })
if (res && res.data && res.data.questions) { if (res && res.data && res.data.questions) {
setOtherQuestions((prev: any) => ({ setOtherQuestions((prev: any) => ({
...@@ -234,7 +218,7 @@ export const Home: React.FC = () => { ...@@ -234,7 +218,7 @@ export const Home: React.FC = () => {
if (res.data) { if (res.data) {
// 登录成功后先打印完整的原始链接(删除之前) // 登录成功后先打印完整的原始链接(删除之前)
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log('登录成功,删除前完整链接:', window.location.href) console.log('20251211登录成功,删除前完整链接:', window.location.href)
// 登录成功后先清理旧状态,避免沿用上一次的工具模式 // 登录成功后先清理旧状态,避免沿用上一次的工具模式
dispatch(clearCurrentToolId()) dispatch(clearCurrentToolId())
safeSessionStorageRemoveItem('currentToolId') safeSessionStorageRemoveItem('currentToolId')
...@@ -246,7 +230,7 @@ export const Home: React.FC = () => { ...@@ -246,7 +230,7 @@ export const Home: React.FC = () => {
} }
// 删除后打印链接 // 删除后打印链接
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log('登录成功,删除后完整链接:', window.location.href) console.log('20251211登录成功,删除后完整链接:', window.location.href)
setToken(res.data.token) setToken(res.data.token)
// 主动触发 storage 事件,确保其他组件能监听到变化 // 主动触发 storage 事件,确保其他组件能监听到变化
window.dispatchEvent( window.dispatchEvent(
...@@ -262,23 +246,9 @@ export const Home: React.FC = () => { ...@@ -262,23 +246,9 @@ export const Home: React.FC = () => {
window.dispatchEvent(new CustomEvent('forceResetToGeneralMode')) window.dispatchEvent(new CustomEvent('forceResetToGeneralMode'))
initConversation() initConversation()
dispatch(fetchConversations()) dispatch(fetchConversations())
// sso_login 成功后,首次拉取常见问题,强制 toolId 为空
handleInitialQuestions()
} }
} }
else { else {
// 判断是否为站内导航返回
// 如果有 location.state,说明是站内导航,不清除 toolId
// 如果 sessionStorage 中有 toolId,说明可能是从其他页面返回,不清除
// 只有在没有 location.state 且 sessionStorage 也没有 toolId 时,才清除(新标签页)
const sessionToolId = safeSessionStorageGetItem('currentToolId')
if (!location.state && !sessionToolId) {
// 新标签页,清除残留的 toolId
safeSessionStorageRemoveItem('currentToolId')
dispatch(clearCurrentToolId())
}
// 如果有 location.state 或 sessionStorage 中有 toolId,不清除,保留 toolId
initConversation() initConversation()
dispatch(fetchConversations()) dispatch(fetchConversations())
} }
...@@ -286,7 +256,6 @@ export const Home: React.FC = () => { ...@@ -286,7 +256,6 @@ export const Home: React.FC = () => {
else { else {
// 模拟登录 可以用来测试 // 模拟登录 可以用来测试
res = await fetchLoginByUid('123123') res = await fetchLoginByUid('123123')
sessionStorage.removeItem('currentToolId')
if (res.data) { if (res.data) {
// 登录成功后先清理旧状态,避免沿用上一次的工具模式 // 登录成功后先清理旧状态,避免沿用上一次的工具模式
dispatch(clearCurrentToolId()) dispatch(clearCurrentToolId())
...@@ -313,7 +282,7 @@ export const Home: React.FC = () => { ...@@ -313,7 +282,7 @@ export const Home: React.FC = () => {
dispatch(fetchConversations()) dispatch(fetchConversations())
} }
} }
}, [setToken, dispatch, handleInitialQuestions]) }, [setToken, dispatch])
// 监听路由参数变化,提取 userRoles(确保路由参数被正确解析) // 监听路由参数变化,提取 userRoles(确保路由参数被正确解析)
useEffect(() => { useEffect(() => {
...@@ -322,7 +291,10 @@ export const Home: React.FC = () => { ...@@ -322,7 +291,10 @@ export const Home: React.FC = () => {
useEffect(() => { useEffect(() => {
login() login()
// 首页首次挂载时重置为通用模式:
// 1. 清除 Redux 中的 currentToolId
dispatch(clearCurrentToolId()) dispatch(clearCurrentToolId())
_handleToolClick(false, '', true)
}, []) // 依赖数组为空,只在组件挂载时执行一次 }, []) // 依赖数组为空,只在组件挂载时执行一次
return ( return (
......
...@@ -126,8 +126,6 @@ const QuestionListBase: React.FC<QuestionListProps & WithAuthProps> = ({ ...@@ -126,8 +126,6 @@ const QuestionListBase: React.FC<QuestionListProps & WithAuthProps> = ({
} }
else { else {
// 如果没有现有会话,仍然创建新会话(向后兼容) // 如果没有现有会话,仍然创建新会话(向后兼容)
// 这里可以改为提示用户,或者保持创建新会话的逻辑
// 为了最小修改,暂时保持创建新会话的逻辑
dispatch( dispatch(
createConversation({ createConversation({
conversationData: {}, conversationData: {},
......
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