Commit e019d3ac by Liu

fix:工具默认展示第0个,去掉制度活化硬性选中

parent eea71f34
......@@ -131,30 +131,15 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
await waitForToken()
// 从路由中获取 userRoles 参数
const userRoles = getUserRolesForApi()
// 检查缓存(包含 userRoles 信息,有效期 5 分钟)
const cacheKey = `toolList_${JSON.stringify(userRoles)}`
const cached = sessionStorage.getItem(cacheKey)
if (cached) {
try {
const { toolList, timestamp } = JSON.parse(cached)
if (Date.now() - timestamp < 5 * 60 * 1000) {
setToolList(toolList)
return
}
}
catch {
// 缓存解析失败,继续调用接口
}
}
// 调用真实 API 获取工具列表
const res = await fetchToolList({ userRoles })
if (res?.data && Array.isArray(res.data) && res.data.length > 0) {
setToolList(res.data)
// 缓存工具列表(包含 userRoles 和时间戳)
sessionStorage.setItem(cacheKey, JSON.stringify({
toolList: res.data,
timestamp: Date.now(),
}))
// 存入第0个工具信息到缓存
const defaultTool = res.data[0]
safeSessionStorageSetItem('currentToolId', defaultTool.toolId)
safeSessionStorageSetItem('currentToolName', defaultTool.toolName)
}
}
catch (error) {
......@@ -175,6 +160,12 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
}
setSelectedToolId(null)
setIsToolBtnActive(true)
// 存入默认高亮按钮信息到缓存
if (toolList.length > 0) {
const defaultTool = toolList[0]
safeSessionStorageSetItem('currentToolId', defaultTool.toolId)
safeSessionStorageSetItem('currentToolName', defaultTool.toolName)
}
return
}
......@@ -184,6 +175,12 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
setSearchParams(newSearchParams, { replace: true })
setSelectedToolId(null)
setIsToolBtnActive(true)
// 存入默认高亮按钮信息到缓存
if (toolList.length > 0) {
const defaultTool = toolList[0]
safeSessionStorageSetItem('currentToolId', defaultTool.toolId)
safeSessionStorageSetItem('currentToolName', defaultTool.toolName)
}
return
}
......@@ -193,6 +190,14 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
if (currentToolId && hasPersistentToolSource) {
setSelectedToolId(currentToolId)
setIsToolBtnActive(false)
// 存入当前选中工具信息到缓存
if (toolList.length > 0) {
const currentTool = toolList.find(tool => String(tool.toolId) === String(currentToolId))
if (currentTool) {
safeSessionStorageSetItem('currentToolId', currentTool.toolId)
safeSessionStorageSetItem('currentToolName', currentTool.toolName)
}
}
return
}
......@@ -206,12 +211,26 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
if (toolIdFromUrl) {
setSelectedToolId(toolIdFromUrl)
setIsToolBtnActive(false)
// 存入当前选中工具信息到缓存
if (toolList.length > 0) {
const currentTool = toolList.find(tool => String(tool.toolId) === String(toolIdFromUrl))
if (currentTool) {
safeSessionStorageSetItem('currentToolId', currentTool.toolId)
safeSessionStorageSetItem('currentToolName', currentTool.toolName)
}
}
}
else {
setSelectedToolId(null)
setIsToolBtnActive(true)
// 存入默认高亮按钮信息到缓存
if (toolList.length > 0) {
const defaultTool = toolList[0]
safeSessionStorageSetItem('currentToolId', defaultTool.toolId)
safeSessionStorageSetItem('currentToolName', defaultTool.toolName)
}
}
}, [currentToolId, sessionToolId, toolIdFromUrl, searchParams, setSearchParams, fromCollect])
}, [currentToolId, sessionToolId, toolIdFromUrl, searchParams, setSearchParams, fromCollect, toolList])
// 监听 sessionStorage 中的 currentToolId(历史点击时写入)来辅助高亮逻辑
useEffect(() => {
......@@ -287,8 +306,8 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
toolId = selectedToolId
}
else {
// 制度活化:不传递 toolId
toolId = undefined
// 制度活化:使用第0个工具的 toolId
toolId = toolList[0]?.toolId
}
onSubmit?.(content.trim(), toolId)
setContent('')
......@@ -317,12 +336,14 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
document.execCommand('insertText', false, text)
}
// 处理制度活化按钮点击:先创建新会话
// 处理默认工具按钮点击:先创建新会话
const handleGeneralClick = async () => {
if (!checkAuth())
return
// 获取默认工具信息
const defaultTool = toolList.length > 0 ? toolList[0] : { toolId: '', toolName: '默认工具' }
// 通知上层开始 loading
onToolClick?.(true, undefined, '制度活化', false, undefined, true)
onToolClick?.(true, undefined, defaultTool.toolName, false, undefined, true)
try {
// 重置提交按钮状态
dispatch(setIsAsking(false))
......@@ -332,11 +353,15 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
setIsToolBtnActive(true)
setSelectedToolId(null)
safeSessionStorageRemoveItem('showToolQuestion')
safeSessionStorageRemoveItem('currentToolId')
setSessionToolId(null)
// 存入默认高亮按钮信息到缓存
if (toolList.length > 0) {
safeSessionStorageSetItem('currentToolId', defaultTool.toolId)
safeSessionStorageSetItem('currentToolName', defaultTool.toolName)
setSessionToolId(defaultTool.toolId)
}
setShowToolQuestion(false)
// 先通知上层更新欢迎语(即便后续接口异常也能生效)
onToolClick?.(true, undefined, '制度活化', false)
onToolClick?.(true, undefined, defaultTool.toolName, false)
// 清空路由中的 toolId 参数
if (toolIdFromUrl) {
const newSearchParams = new URLSearchParams(searchParams)
......@@ -348,33 +373,33 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
// 等待 token 就绪后再调用接口
await waitForToken()
const requestData = {
busiId: '',
busiId: defaultTool.toolId,
}
const res = await fetchSessionConversationId(requestData)
if (res?.data?.conversationId) {
const conversationId = res.data.conversationId
// 获取到会话ID后,通知上层更新欢迎语并传递 conversationId
onToolClick?.(true, undefined, '制度活化', false, conversationId)
onToolClick?.(true, undefined, defaultTool.toolName, false, conversationId)
// 在 navigate 之前设置标记,避免 Chat 组件的 useEffect 重复调用接口
sessionStorage.setItem('toolHistoryLoading', conversationId)
// 更新路由到新的会话ID
navigate(`/chat/${conversationId}`, {
replace: true,
state: {
toolId: null,
toolId: defaultTool.toolId,
skipHistoryLoad: true, // 标记跳过历史记录加载,因为 ChatEditor 会处理
},
})
// 使用获取到的会话ID调用历史会话
await waitForToken()
const qaRes = await fetchUserQaRecordPage(conversationId, '')
const qaRes = await fetchUserQaRecordPage(conversationId, defaultTool.toolId)
console.log('qaRes chatEditor11111', qaRes)
// 通过自定义事件将历史记录数据传递给父组件进行渲染
window.dispatchEvent(new CustomEvent('toolHistoryLoaded', {
detail: {
conversationId,
toolId: '',
toolName: '制度活化',
toolId: defaultTool.toolId,
toolName: defaultTool.toolName,
qaRecords: qaRes?.data || [],
},
}))
......@@ -389,7 +414,7 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
}
finally {
// 通知上层结束 loading
onToolClick?.(true, undefined, '制度活化', false, undefined, false)
onToolClick?.(true, undefined, defaultTool.toolName, false, undefined, false)
}
}
......@@ -411,7 +436,9 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
dispatch(setCurrentToolId(tool.toolId))
setSelectedToolId(tool.toolId)
setIsToolBtnActive(false)
// 存入当前选中工具信息到缓存
safeSessionStorageSetItem('currentToolId', tool.toolId)
safeSessionStorageSetItem('currentToolName', tool.toolName)
setSessionToolId(tool.toolId)
// 先调用 fetchSessionConversationId 获取会话ID
......@@ -611,9 +638,9 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
const toolIdStr = String(tool.toolId)
const isSelectedByState = selectedToolId && toolIdStr === String(selectedToolId)
const isSelectedBySession = !selectedToolId && sessionToolId && toolIdStr === String(sessionToolId)
// 制度活化高亮:路由内没有 toolId 或 toolId 为空时默认高亮,点击后也要高亮
const isGeneralMode = tool.toolName === '制度活化' && isToolBtnActive && !selectedToolId && !sessionToolId && !toolIdFromUrl
const isSelected = isSelectedByState || isSelectedBySession || isGeneralMode
// 默认高亮第0个工具:路由内没有 toolId 或 toolId 为空时默认高亮,点击后也要高亮
const isDefaultHighlight = index === 0 && isToolBtnActive && !selectedToolId && !sessionToolId && !toolIdFromUrl
const isSelected = isSelectedByState || isSelectedBySession || isDefaultHighlight
const baseBtnClass
= 'w-auto h-[32px] px-3 rounded-full shadow-none text-[12px] flex items-center gap-2 transition-all duration-200 border'
......@@ -627,7 +654,7 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
// 高亮状态直接返回,避免重复触发
if (isSelected)
return
if (tool.toolName === '制度活化')
if (index === 0)
await handleGeneralClick()
else
await handleToolClick(tool)
......
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