Commit 186aef40 by Liu

fix:常见问题调用时机

parent dede7a75
......@@ -18,7 +18,7 @@ interface ChatEditorProps {
onChange?: (value: string) => void
onFocus?: () => void
onSubmit?: (value: string, toolId?: string) => void
onToolClick?: (isToolBtn: boolean, toolId?: string, toolName?: string, shouldChangeStyle?: boolean) => void
onToolClick?: (isToolBtn: boolean, toolId?: string, toolName?: string, shouldChangeStyle?: boolean, conversationId?: string) => void
placeholders: string[]
showContentTips?: boolean
initialValue?: string
......@@ -402,8 +402,6 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
setIsToolBtnActive(false)
safeSessionStorageSetItem('currentToolId', tool.toolId)
setSessionToolId(tool.toolId)
// 先通知上层更新欢迎语(即便后续接口异常也能生效)
onToolClick?.(false, tool.toolId, tool.toolName, true)
// 先调用 fetchSessionConversationId 获取会话ID
try {
......@@ -415,6 +413,8 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
const res = await fetchSessionConversationId(requestData)
if (res?.data?.conversationId) {
const conversationId = res.data.conversationId
// 获取到会话ID后,通知上层更新欢迎语并传递 conversationId
onToolClick?.(false, tool.toolId, tool.toolName, true, conversationId)
// 在 navigate 之前设置标记,避免 Chat 组件的 useEffect 重复调用接口
// 使用 sessionStorage 作为标记,因为 location.state 可能有时序问题
sessionStorage.setItem('toolHistoryLoading', conversationId)
......@@ -445,6 +445,8 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
}
catch (error) {
console.error('获取会话ID或历史记录失败:', error)
// 出错时也要通知上层更新欢迎语(不传递 conversationId)
onToolClick?.(false, tool.toolId, tool.toolName, true)
// 出错时也要清除标记
sessionStorage.removeItem('toolHistoryLoading')
}
......
......@@ -859,10 +859,10 @@ export const Chat: React.FC = () => {
</div>
<ChatEditor
onSubmit={(question, toolId) => handleSubmitQuestion(question, undefined, toolId)}
onToolClick={(isToolBtn, toolId, toolName, shouldChangeStyle) => {
onToolClick={(isToolBtn, toolId, toolName, shouldChangeStyle, conversationId) => {
// 发送自定义事件到父组件
window.dispatchEvent(new CustomEvent('toolButtonClick', {
detail: { isToolBtn, toolId, toolName, shouldChangeStyle },
detail: { isToolBtn, toolId, toolName, shouldChangeStyle, conversationId },
}))
}}
placeholders={[]}
......
......@@ -241,7 +241,7 @@ export const Home: React.FC = () => {
// 监听工具按钮点击事件
useEffect(() => {
const handleToolClickEvent = (event: CustomEvent) => {
const { isToolBtn, toolId } = event.detail
const { isToolBtn, toolId, conversationId } = event.detail
setIsToolBtnActive(isToolBtn)
// 更新样式控制状态
setShouldChangeStyle(true)
......@@ -256,7 +256,8 @@ export const Home: React.FC = () => {
dispatch(clearCurrentToolId())
}
_handleToolClick(isToolBtn, toolId)
// 传递 conversationId 给 _handleToolClick
_handleToolClick(isToolBtn, toolId, false, conversationId)
}
window.addEventListener('toolButtonClick', handleToolClickEvent as EventListener)
return () => {
......
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