Skip to content

Commit a30b8fd

Browse files
committed
fix: improve typewriter animation
1 parent a8643a7 commit a30b8fd

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

package/src/components/Message/MessageSimple/MessageContent.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,6 @@ const MessageContentWithContext = <
217217
return bordersFromTheme;
218218
};
219219

220-
console.log('ISE: NEW: ', message.ai_generated, message.id);
221-
222220
return (
223221
<Pressable
224222
disabled={preventPress}

package/src/components/Message/MessageSimple/MessageTextContainer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const MessageTextContainerWithContext = <
8383
message,
8484
) as MessageType<StreamChatGenerics>;
8585

86-
if (!message.text && !message.ai_generated) return null;
86+
if (!message.text) return null;
8787

8888
const markdownStyles = { ...markdown, ...markdownStylesProp };
8989

package/src/components/Message/MessageSimple/StreamingMessageView.tsx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,26 @@ export const StreamingMessageView = <
1212
) => {
1313
const { message } = useMessageContext<StreamChatGenerics>();
1414
const { text = '' } = message;
15-
const textLength = text.length;
1615
const [streamedMessageText, setStreamedMessageText] = useState<string>(text);
17-
const textCursor = useRef<number>(textLength);
16+
const textCursor = useRef<number>(text.length);
1817

1918
useEffect(() => {
20-
if (!text || textCursor.current >= textLength) {
21-
return;
22-
}
23-
// TODO: make this configurable maybe
24-
const newCursorValue = textCursor.current + 1;
25-
const newBatch = text?.substring(textCursor.current, newCursorValue);
26-
textCursor.current += newBatch.length;
27-
setTimeout(() => {
28-
setStreamedMessageText((prevStreamedMessageText) => prevStreamedMessageText.concat(newBatch));
19+
const textLength = text.length;
20+
const interval = setInterval(() => {
21+
if (!text || textCursor.current >= textLength) {
22+
clearInterval(interval);
23+
}
24+
// TODO: make this configurable maybe
25+
const newCursorValue = textCursor.current + 1;
26+
const newText = text.substring(0, newCursorValue);
27+
textCursor.current += newText.length - textCursor.current;
28+
setStreamedMessageText(newText);
2929
}, 0);
30-
}, [streamedMessageText, text, textLength]);
30+
31+
return () => {
32+
clearInterval(interval);
33+
};
34+
}, [text]);
3135

3236
return <MessageTextContainer message={{ ...message, text: streamedMessageText }} {...props} />;
3337
};

0 commit comments

Comments
 (0)