mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-28 21:42:27 +08:00
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:
parent
71b527b67c
commit
2b7dfc0e88
@ -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(() => {
|
||||
|
||||
@ -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])
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user