fix(useSmoothStream): remove unnecessary comments and markdown displayedContent (#8416)

refactor(useSmoothStream): remove unnecessary comments and improve state initialization in Markdown component

- Cleaned up comments in useSmoothStream for clarity.
- Updated displayedContent state initialization in Markdown to handle post-processing conditionally.
This commit is contained in:
MyPrototypeWhat 2025-07-23 17:18:30 +08:00 committed by GitHub
parent 71b527b67c
commit 2b7dfc0e88
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 8 deletions

View File

@ -3,12 +3,9 @@ import { useCallback, useEffect, useRef, useState } from 'react'
interface UseSmoothStreamOptions {
onUpdate: (text: string) => void
streamDone: boolean
// 我们不再需要固定的interval但可以保留一个最小延迟以保证动画感
minDelay?: number
initialText?: string
}
// 如果不行还可以使用Array.from(chunk)分割
// const reg = /[\u4E00-\u9FFF]|[a-zA-Z0-9]+|\s+|[^\s\w]/g
export const useSmoothStream = ({ onUpdate, streamDone, minDelay = 10, initialText = '' }: UseSmoothStreamOptions) => {
const [chunkQueue, setChunkQueue] = useState<string[]>([])
@ -17,8 +14,6 @@ export const useSmoothStream = ({ onUpdate, streamDone, minDelay = 10, initialTe
const lastUpdateTimeRef = useRef<number>(0)
const addChunk = useCallback((chunk: string) => {
// 英文按照word拆分, 中文按照字拆分,使用正则表达式
// const words = chunk.match(/[\w\d]+/g)
const chars = Array.from(chunk)
setChunkQueue((prev) => [...prev, ...(chars || [])])
}, [])
@ -84,7 +79,7 @@ export const useSmoothStream = ({ onUpdate, streamDone, minDelay = 10, initialTe
cancelAnimationFrame(animationFrameRef.current)
}
}
}, [renderLoop]) // 依赖 renderLoop
}, [renderLoop])
// 当外部流结束,且队列即将变空时,进行最后一次"瞬移"渲染
useEffect(() => {

View File

@ -46,7 +46,7 @@ const Markdown: FC<Props> = ({ block, postProcess }) => {
const { mathEngine } = useSettings()
const isTrulyDone = 'status' in block && block.status === 'success'
const [displayedContent, setDisplayedContent] = useState(block.content)
const [displayedContent, setDisplayedContent] = useState(postProcess ? postProcess(block.content) : block.content)
const [isStreamDone, setIsStreamDone] = useState(isTrulyDone)
const prevContentRef = useRef(block.content)
@ -83,7 +83,7 @@ const Markdown: FC<Props> = ({ block, postProcess }) => {
prevBlockIdRef.current = block.id
// 更新 stream 状态
const isStreaming = 'status' in block && block.status === 'streaming'
const isStreaming = block.status === 'streaming'
setIsStreamDone(!isStreaming)
}, [block.content, block.id, block.status, addChunk, reset])