-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix(stream): add shared StreamingText component for smooth streaming animation #3423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: staging
Are you sure you want to change the base?
Changes from all commits
3b543f3
7935b21
754bf99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,107 +1,22 @@ | ||
| import { memo, useEffect, useRef, useState } from 'react' | ||
| import { cn } from '@/lib/core/utils/cn' | ||
| import { memo } from 'react' | ||
| import { StreamingIndicator, StreamingText } from '@/components/ui' | ||
| import { CopilotMarkdownRenderer } from '../markdown-renderer' | ||
|
|
||
| /** Character animation delay in milliseconds */ | ||
| const CHARACTER_DELAY = 3 | ||
| export { StreamingIndicator } | ||
|
|
||
| /** Props for the StreamingIndicator component */ | ||
| interface StreamingIndicatorProps { | ||
| /** Optional class name for layout adjustments */ | ||
| className?: string | ||
| } | ||
|
|
||
| /** Shows animated dots during message streaming when no content has arrived */ | ||
| export const StreamingIndicator = memo(({ className }: StreamingIndicatorProps) => ( | ||
| <div className={cn('flex h-[1.25rem] items-center text-muted-foreground', className)}> | ||
| <div className='flex space-x-0.5'> | ||
| <div className='h-1 w-1 animate-bounce rounded-full bg-muted-foreground [animation-delay:0ms] [animation-duration:1.2s]' /> | ||
| <div className='h-1 w-1 animate-bounce rounded-full bg-muted-foreground [animation-delay:150ms] [animation-duration:1.2s]' /> | ||
| <div className='h-1 w-1 animate-bounce rounded-full bg-muted-foreground [animation-delay:300ms] [animation-duration:1.2s]' /> | ||
| </div> | ||
| </div> | ||
| )) | ||
|
|
||
| StreamingIndicator.displayName = 'StreamingIndicator' | ||
| const renderCopilotMarkdown = (content: string) => <CopilotMarkdownRenderer content={content} /> | ||
|
|
||
| /** Props for the SmoothStreamingText component */ | ||
| interface SmoothStreamingTextProps { | ||
| /** Content to display with streaming animation */ | ||
| content: string | ||
| /** Whether the content is actively streaming */ | ||
| isStreaming: boolean | ||
| } | ||
|
|
||
| /** Displays text with character-by-character animation for smooth streaming */ | ||
| export const SmoothStreamingText = memo( | ||
| ({ content, isStreaming }: SmoothStreamingTextProps) => { | ||
| const [displayedContent, setDisplayedContent] = useState(() => (isStreaming ? '' : content)) | ||
| const contentRef = useRef(content) | ||
| const timeoutRef = useRef<NodeJS.Timeout | null>(null) | ||
| const indexRef = useRef(isStreaming ? 0 : content.length) | ||
| const isAnimatingRef = useRef(false) | ||
|
|
||
| useEffect(() => { | ||
| contentRef.current = content | ||
|
|
||
| if (content.length === 0) { | ||
| setDisplayedContent('') | ||
| indexRef.current = 0 | ||
| return | ||
| } | ||
|
|
||
| if (isStreaming) { | ||
| if (indexRef.current < content.length) { | ||
| const animateText = () => { | ||
| const currentContent = contentRef.current | ||
| const currentIndex = indexRef.current | ||
|
|
||
| if (currentIndex < currentContent.length) { | ||
| const newDisplayed = currentContent.slice(0, currentIndex + 1) | ||
| setDisplayedContent(newDisplayed) | ||
| indexRef.current = currentIndex + 1 | ||
| timeoutRef.current = setTimeout(animateText, CHARACTER_DELAY) | ||
| } else { | ||
| isAnimatingRef.current = false | ||
| } | ||
| } | ||
|
|
||
| if (!isAnimatingRef.current) { | ||
| if (timeoutRef.current) { | ||
| clearTimeout(timeoutRef.current) | ||
| } | ||
| isAnimatingRef.current = true | ||
| animateText() | ||
| } | ||
| } | ||
| } else { | ||
| if (timeoutRef.current) { | ||
| clearTimeout(timeoutRef.current) | ||
| } | ||
| setDisplayedContent(content) | ||
| indexRef.current = content.length | ||
| isAnimatingRef.current = false | ||
| } | ||
|
|
||
| return () => { | ||
| if (timeoutRef.current) { | ||
| clearTimeout(timeoutRef.current) | ||
| } | ||
| isAnimatingRef.current = false | ||
| } | ||
| }, [content, isStreaming]) | ||
|
|
||
| return ( | ||
| <div className='min-h-[1.25rem] max-w-full'> | ||
| <CopilotMarkdownRenderer content={displayedContent} /> | ||
| </div> | ||
| ) | ||
| }, | ||
| (prevProps, nextProps) => { | ||
| return ( | ||
| prevProps.content === nextProps.content && prevProps.isStreaming === nextProps.isStreaming | ||
| ) | ||
| } | ||
| ) | ||
| /** Copilot-specific streaming text that renders with CopilotMarkdownRenderer */ | ||
| export const SmoothStreamingText = memo(({ content, isStreaming }: SmoothStreamingTextProps) => { | ||
| return ( | ||
| <StreamingText content={content} isStreaming={isStreaming} renderer={renderCopilotMarkdown} /> | ||
| ) | ||
| }) | ||
|
|
||
| SmoothStreamingText.displayName = 'SmoothStreamingText' |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,112 @@ | ||||||||||||||||||||||||
| 'use client' | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import { memo, type ReactNode, useEffect, useRef, useState } from 'react' | ||||||||||||||||||||||||
| import { cn } from '@/lib/core/utils/cn' | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** Target characters to advance per animation frame (~30 chars/frame at 60fps ≈ 1800 chars/sec) */ | ||||||||||||||||||||||||
| const CHARS_PER_FRAME = 30 | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** Props for the StreamingIndicator component */ | ||||||||||||||||||||||||
| interface StreamingIndicatorProps { | ||||||||||||||||||||||||
| className?: string | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** Shows animated dots during message streaming when no content has arrived */ | ||||||||||||||||||||||||
| export const StreamingIndicator = memo(({ className }: StreamingIndicatorProps) => ( | ||||||||||||||||||||||||
| <div className={cn('flex h-[1.25rem] items-center text-muted-foreground', className)}> | ||||||||||||||||||||||||
| <div className='flex space-x-0.5'> | ||||||||||||||||||||||||
| <div className='h-1 w-1 animate-bounce rounded-full bg-muted-foreground [animation-delay:0ms] [animation-duration:1.2s]' /> | ||||||||||||||||||||||||
| <div className='h-1 w-1 animate-bounce rounded-full bg-muted-foreground [animation-delay:150ms] [animation-duration:1.2s]' /> | ||||||||||||||||||||||||
| <div className='h-1 w-1 animate-bounce rounded-full bg-muted-foreground [animation-delay:300ms] [animation-duration:1.2s]' /> | ||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||
| )) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| StreamingIndicator.displayName = 'StreamingIndicator' | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** Props for the StreamingText component */ | ||||||||||||||||||||||||
| interface StreamingTextProps { | ||||||||||||||||||||||||
| content: string | ||||||||||||||||||||||||
| isStreaming: boolean | ||||||||||||||||||||||||
| renderer?: (content: string) => ReactNode | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** Default renderer: plain span with whitespace-pre-wrap */ | ||||||||||||||||||||||||
| function DefaultRenderer({ content }: { content: string }) { | ||||||||||||||||||||||||
| return <span className='whitespace-pre-wrap'>{content}</span> | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** Displays text with character-by-character animation using rAF batching for smooth streaming */ | ||||||||||||||||||||||||
| export const StreamingText = memo( | ||||||||||||||||||||||||
| ({ content, isStreaming, renderer }: StreamingTextProps) => { | ||||||||||||||||||||||||
| const [displayedContent, setDisplayedContent] = useState(() => (isStreaming ? '' : content)) | ||||||||||||||||||||||||
| const contentRef = useRef(content) | ||||||||||||||||||||||||
| const rafRef = useRef<number | null>(null) | ||||||||||||||||||||||||
| const indexRef = useRef(isStreaming ? 0 : content.length) | ||||||||||||||||||||||||
| const isAnimatingRef = useRef(false) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||
| contentRef.current = content | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (content.length === 0) { | ||||||||||||||||||||||||
| setDisplayedContent('') | ||||||||||||||||||||||||
| indexRef.current = 0 | ||||||||||||||||||||||||
| return | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
Comment on lines
+51
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When the early-return path is taken ( Adding the explicit reset makes the invariant self-contained and easier to reason about:
Suggested change
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (isStreaming) { | ||||||||||||||||||||||||
| if (indexRef.current < content.length) { | ||||||||||||||||||||||||
| const animateText = () => { | ||||||||||||||||||||||||
| const currentContent = contentRef.current | ||||||||||||||||||||||||
| const currentIndex = indexRef.current | ||||||||||||||||||||||||
| if (currentIndex < currentContent.length) { | ||||||||||||||||||||||||
| const nextIndex = Math.min(currentIndex + CHARS_PER_FRAME, currentContent.length) | ||||||||||||||||||||||||
| setDisplayedContent(currentContent.slice(0, nextIndex)) | ||||||||||||||||||||||||
| indexRef.current = nextIndex | ||||||||||||||||||||||||
| rafRef.current = requestAnimationFrame(animateText) | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| isAnimatingRef.current = false | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (!isAnimatingRef.current) { | ||||||||||||||||||||||||
| if (rafRef.current) { | ||||||||||||||||||||||||
| cancelAnimationFrame(rafRef.current) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| isAnimatingRef.current = true | ||||||||||||||||||||||||
| rafRef.current = requestAnimationFrame(animateText) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| if (rafRef.current) { | ||||||||||||||||||||||||
| cancelAnimationFrame(rafRef.current) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| setDisplayedContent(content) | ||||||||||||||||||||||||
| indexRef.current = content.length | ||||||||||||||||||||||||
| isAnimatingRef.current = false | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return () => { | ||||||||||||||||||||||||
| if (rafRef.current) { | ||||||||||||||||||||||||
| cancelAnimationFrame(rafRef.current) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| isAnimatingRef.current = false | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| }, [content, isStreaming]) | ||||||||||||||||||||||||
|
Comment on lines
+48
to
+95
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Animation loop restarted on every streaming chunk The This is unnecessary because Consider removing the rAF cancellation from the cleanup and only cancelling when useEffect(() => {
contentRef.current = content
if (content.length === 0) {
setDisplayedContent('')
indexRef.current = 0
return
}
if (isStreaming) {
// contentRef is already updated above; the running loop will pick it up.
if (!isAnimatingRef.current && indexRef.current < content.length) {
isAnimatingRef.current = true
rafRef.current = requestAnimationFrame(animateText)
}
} else {
if (rafRef.current) cancelAnimationFrame(rafRef.current)
rafRef.current = null
setDisplayedContent(content)
indexRef.current = content.length
isAnimatingRef.current = false
}
return () => {
if (!isStreaming && rafRef.current) {
cancelAnimationFrame(rafRef.current)
}
isAnimatingRef.current = false
}
}, [content, isStreaming])This way, a single rAF loop runs continuously while streaming and simply catches up to |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||
| <div className='min-h-[1.25rem] max-w-full'> | ||||||||||||||||||||||||
| {renderer ? renderer(displayedContent) : <DefaultRenderer content={displayedContent} />} | ||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||
| (prevProps, nextProps) => { | ||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||
| prevProps.content === nextProps.content && | ||||||||||||||||||||||||
| prevProps.isStreaming === nextProps.isStreaming && | ||||||||||||||||||||||||
| prevProps.renderer === nextProps.renderer | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| StreamingText.displayName = 'StreamingText' | ||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.