@@ -11,22 +11,23 @@ export const StreamingMessageView = <
1111 props : MessageTextContainerProps < StreamChatGenerics > ,
1212) => {
1313 const { message } = useMessageContext < StreamChatGenerics > ( ) ;
14- const { text } = message ;
15- const [ streamedMessageText , setStreamedMessageText ] = useState < string > ( text || '' ) ;
16- const textCursor = useRef < number > ( text ?. length || 0 ) ;
14+ const { text = '' } = message ;
15+ const textLength = text . length ;
16+ const [ streamedMessageText , setStreamedMessageText ] = useState < string > ( text ) ;
17+ const textCursor = useRef < number > ( textLength ) ;
1718
1819 useEffect ( ( ) => {
19- if ( ! text || textCursor . current >= text . length ) {
20+ if ( ! text || textCursor . current >= textLength ) {
2021 return ;
2122 }
2223 // TODO: make this configurable maybe
2324 const newCursorValue = textCursor . current + 1 ;
2425 const newBatch = text ?. substring ( textCursor . current , newCursorValue ) ;
26+ textCursor . current += newBatch . length ;
2527 setTimeout ( ( ) => {
2628 setStreamedMessageText ( ( prevStreamedMessageText ) => prevStreamedMessageText . concat ( newBatch ) ) ;
2729 } , 0 ) ;
28- textCursor . current = newCursorValue ;
29- } , [ streamedMessageText , text ] ) ;
30+ } , [ streamedMessageText , text , textLength ] ) ;
3031
3132 return < MessageTextContainer message = { { ...message , text : streamedMessageText } } { ...props } /> ;
3233} ;
0 commit comments