Commit 186aef40 by Liu

fix:常见问题调用时机

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