Commit eea71f34 by Liu

fix:饼图对重新分析的影响

parent 84526182
import React, { useEffect, useRef, useState } from 'react'
import React, { useEffect, useState } from 'react'
import { Chip } from '@heroui/react'
import { ChatAnswerAttachment } from './ChatAnswerAttchment'
import { ChatAnswerOperate } from './ChatAnswerOperate'
......@@ -43,10 +43,11 @@ function CheckIcon({ ...props }) {
export const ChatAnswerParser: React.FC<ChatAnswerParserProps> = ({ isLastAnswer, onTyping, onComplate, answer, isStopTyping, onSubmitQuestion, hideOperate, onWorkflowSubmit, isWorkflowSubmitting, isAsking = false }) => {
const formatAnswer = formatMarkdown(answer.answer || '')
const [displayedText, setDisplayedText] = useState('')
const [currentIndex, setCurrentIndex] = useState(0)
const [isTyping, setIsTyping] = useState(false)
const [hideOperateByCard, setHideOperateByCard] = useState(false)
const [isImageAnswer, setIsImageAnswer] = useState(false)
const [hasProcessedCardList, setHasProcessedCardList] = useState(false) // 添加标记,避免重复处理cardList
const completedOnceRef = useRef(false)
function extractImageSources(htmlString: string): string[] {
const imgRegex = /<img[^>]+src="([^">]+)"/gi
......@@ -118,34 +119,44 @@ export const ChatAnswerParser: React.FC<ChatAnswerParserProps> = ({ isLastAnswer
}
useEffect(() => {
// Streaming stage: render plain text only (no markdown parsing / no typewriter),
// to avoid heavy re-rendering and page flicker.
if (!answer.endAnswerFlag) {
setDisplayedText(answer.answer || '')
setIsTyping(false)
completedOnceRef.current = false
if (isStopTyping) {
return
}
// Stream ended: render full markdown once (no typewriter) to avoid heavy re-renders.
if (completedOnceRef.current)
return
completedOnceRef.current = true
// Keep existing contract: notify typing start/end callbacks once.
onTyping()
setIsTyping(false)
if (formatAnswer.includes('<img')) {
handleImageAnswer()
if (!isTyping) {
onTyping()
setIsTyping(true)
}
if (currentIndex < formatAnswer.length) {
const nextChar = formatAnswer[currentIndex]
if (nextChar === '<' || isImageAnswer) {
setIsImageAnswer(true)
const timer = setTimeout(() => {
setCurrentIndex(prevIndex => prevIndex + 1)
}, 10) // 调整此值以改变打字速度
return () => clearTimeout(timer)
}
else {
const timer = setTimeout(() => {
setDisplayedText(formatAnswer.slice(0, currentIndex + 1))
setCurrentIndex(prevIndex => prevIndex + 1)
}, 10) // 调整此值以改变打字速度
return () => clearTimeout(timer)
}
}
else {
setDisplayedText(formatAnswer)
onComplate()
if (answer.endAnswerFlag) {
if (isImageAnswer) {
handleImageAnswer()
}
else {
setIsTyping(false)
onComplate()
}
// 流式输出结束时检查 cardList 中的 URL
handleCardListUrls()
}
}
// 流式输出结束时检查 cardList 中的 URL
handleCardListUrls()
}, [answer.endAnswerFlag, answer.answer])
}, [answer, currentIndex])
const handleStopTyping = async () => {
const res = await fetchTerminateQuestion(answer)
......@@ -186,7 +197,7 @@ export const ChatAnswerParser: React.FC<ChatAnswerParserProps> = ({ isLastAnswer
{!!displayedText.length && (
<div style={{ background: '#F7FAFD' }} className={answer.cardList?.length ? 'mb-[20px]' : ''}>
<MarkdownDetail disablePie={!answer.endAnswerFlag}>
<MarkdownDetail>
{displayedText}
</MarkdownDetail>
</div>
......
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