Commit a41a24e8 by Liu

fix: 点击常见问题提问逻辑

parent aefb636d
...@@ -39,6 +39,8 @@ export const Chat: React.FC = () => { ...@@ -39,6 +39,8 @@ export const Chat: React.FC = () => {
const [allItems, setAllItems] = useState<ChatRecord[]>([]) const [allItems, setAllItems] = useState<ChatRecord[]>([])
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
const { shouldSendQuestion, currentToolId, conversations } = useAppSelector((state: RootState) => state.conversation) const { shouldSendQuestion, currentToolId, conversations } = useAppSelector((state: RootState) => state.conversation)
// 标记历史记录是否已加载完成,用于确保自动发送问题在历史记录加载后执行
const historyLoadedRef = useRef<{ conversationId: string | null }>({ conversationId: null })
const scrollableRef = useRef<HTMLDivElement | any>(null) const scrollableRef = useRef<HTMLDivElement | any>(null)
const position = useScroll(scrollableRef) const position = useScroll(scrollableRef)
const currentIdRef = useRef<string | undefined>(id) const currentIdRef = useRef<string | undefined>(id)
...@@ -458,6 +460,8 @@ export const Chat: React.FC = () => { ...@@ -458,6 +460,8 @@ export const Chat: React.FC = () => {
} }
finally { finally {
setIsLoading(false) setIsLoading(false)
// 标记该会话的历史记录已加载完成
historyLoadedRef.current.conversationId = finalConversationId
// 清除处理标记(支持新会话ID的情况) // 清除处理标记(支持新会话ID的情况)
// finalConversationId 可能是新会话ID(从收藏返回时)或原来的 conversationId // finalConversationId 可能是新会话ID(从收藏返回时)或原来的 conversationId
if (processingConversationIdRef.current === finalConversationId) { if (processingConversationIdRef.current === finalConversationId) {
...@@ -491,6 +495,8 @@ export const Chat: React.FC = () => { ...@@ -491,6 +495,8 @@ export const Chat: React.FC = () => {
currentIdRef.current = id currentIdRef.current = id
lastSentQuestionRef.current = '' // 重置标记 lastSentQuestionRef.current = '' // 重置标记
// 重置历史记录加载标记,确保新会话时能正确触发自动发送
historyLoadedRef.current.conversationId = null
// 保存 fromCollect 标记到 ref,避免 location.state 被清除后丢失 // 保存 fromCollect 标记到 ref,避免 location.state 被清除后丢失
// 优先使用 location.state,其次使用 sessionStorage(用于 navigate(-1) 的情况) // 优先使用 location.state,其次使用 sessionStorage(用于 navigate(-1) 的情况)
fromCollectRef.current = Boolean(location.state?.fromCollect) || Boolean(sessionStorage.getItem('fromCollect')) fromCollectRef.current = Boolean(location.state?.fromCollect) || Boolean(sessionStorage.getItem('fromCollect'))
...@@ -513,6 +519,9 @@ export const Chat: React.FC = () => { ...@@ -513,6 +519,9 @@ export const Chat: React.FC = () => {
&& currentIdRef.current && currentIdRef.current
&& !isLoading && !isLoading
&& shouldSendQuestion !== lastSentQuestionRef.current && shouldSendQuestion !== lastSentQuestionRef.current
// 确保历史记录已加载完成(如果是从常见问题点击进入,需要等待历史记录加载完成)
// 新会话(allItems.length <= 1)或历史记录已加载完成时,才自动发送
&& (historyLoadedRef.current.conversationId === currentIdRef.current || allItems.length <= 1)
) { ) {
lastSentQuestionRef.current = shouldSendQuestion lastSentQuestionRef.current = shouldSendQuestion
// 立即清除shouldSendQuestion,防止重复发送 // 立即清除shouldSendQuestion,防止重复发送
...@@ -522,7 +531,7 @@ export const Chat: React.FC = () => { ...@@ -522,7 +531,7 @@ export const Chat: React.FC = () => {
handleSubmitQuestion(shouldSendQuestion, undefined, currentToolId) handleSubmitQuestion(shouldSendQuestion, undefined, currentToolId)
}, 100) }, 100)
} }
}, [shouldSendQuestion, isLoading, currentToolId]) }, [shouldSendQuestion, isLoading, allItems.length, currentToolId, id])
// 根据 currentToolId 获取对应的 toolName // 根据 currentToolId 获取对应的 toolName
useEffect(() => { useEffect(() => {
...@@ -618,6 +627,8 @@ export const Chat: React.FC = () => { ...@@ -618,6 +627,8 @@ export const Chat: React.FC = () => {
} }
finally { finally {
setIsLoading(false) setIsLoading(false)
// 标记该会话的历史记录已加载完成(通过工具切换事件加载)
historyLoadedRef.current.conversationId = conversationId
} }
} }
window.addEventListener('toolHistoryLoaded', handleToolHistoryLoaded as EventListener) window.addEventListener('toolHistoryLoaded', handleToolHistoryLoaded as EventListener)
......
...@@ -15,7 +15,7 @@ export const ChatWelcome: React.FC<ChatWelcomeProps> = ({ toolName: _toolName }) ...@@ -15,7 +15,7 @@ export const ChatWelcome: React.FC<ChatWelcomeProps> = ({ toolName: _toolName })
const currentToolId = typeof window !== 'undefined' ? sessionStorage.getItem('currentToolId') : '' const currentToolId = typeof window !== 'undefined' ? sessionStorage.getItem('currentToolId') : ''
if (currentToolId === '6712395743240') { if (currentToolId === '6712395743240') {
return 'HI~我是您的数据助手,可以帮你查询业务数据哦' return 'Hi~我是您的数据助手,可以帮你查询业务数据哦'
} }
if (currentToolId === '6712395743241') { if (currentToolId === '6712395743241') {
......
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