@assistant-ui/react 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -1171,7 +1171,8 @@ import { forwardRef as forwardRef7 } from "react";
1171
1171
  // src/utils/hooks/useSmooth.tsx
1172
1172
  import { useEffect as useEffect8, useState as useState5 } from "react";
1173
1173
  var TextStreamAnimator = class {
1174
- constructor(setText) {
1174
+ constructor(currentText, setText) {
1175
+ this.currentText = currentText;
1175
1176
  this.setText = setText;
1176
1177
  }
1177
1178
  animationFrameId = null;
@@ -1192,33 +1193,31 @@ var TextStreamAnimator = class {
1192
1193
  const currentTime = Date.now();
1193
1194
  const deltaTime = currentTime - this.lastUpdateTime;
1194
1195
  let timeToConsume = deltaTime;
1195
- this.setText((currentText) => {
1196
- const targetText = this.targetText;
1197
- if (currentText === targetText) {
1198
- this.animationFrameId = null;
1199
- return currentText;
1200
- }
1201
- const remainingChars = targetText.length - currentText.length;
1202
- const baseTimePerChar = Math.min(5, 250 / remainingChars);
1203
- let charsToAdd = 0;
1204
- while (timeToConsume >= baseTimePerChar && charsToAdd < remainingChars) {
1205
- charsToAdd++;
1206
- timeToConsume -= baseTimePerChar;
1207
- }
1196
+ const remainingChars = this.targetText.length - this.currentText.length;
1197
+ const baseTimePerChar = Math.min(5, 250 / remainingChars);
1198
+ let charsToAdd = 0;
1199
+ while (timeToConsume >= baseTimePerChar && charsToAdd < remainingChars) {
1200
+ charsToAdd++;
1201
+ timeToConsume -= baseTimePerChar;
1202
+ }
1203
+ if (charsToAdd !== remainingChars) {
1208
1204
  this.animationFrameId = requestAnimationFrame(this.animate);
1209
- if (charsToAdd === 0) {
1210
- return currentText;
1211
- }
1212
- const newText = targetText.slice(0, currentText.length + charsToAdd);
1213
- this.lastUpdateTime = currentTime - timeToConsume;
1214
- return newText;
1215
- });
1205
+ } else {
1206
+ this.animationFrameId = null;
1207
+ }
1208
+ if (charsToAdd === 0) return;
1209
+ this.currentText = this.targetText.slice(
1210
+ 0,
1211
+ this.currentText.length + charsToAdd
1212
+ );
1213
+ this.lastUpdateTime = currentTime - timeToConsume;
1214
+ this.setText(this.currentText);
1216
1215
  };
1217
1216
  };
1218
1217
  var useSmooth = (text, smooth = false) => {
1219
1218
  const [displayedText, setDisplayedText] = useState5(text);
1220
1219
  const [animatorRef] = useState5(
1221
- new TextStreamAnimator(setDisplayedText)
1220
+ new TextStreamAnimator(text, setDisplayedText)
1222
1221
  );
1223
1222
  useEffect8(() => {
1224
1223
  if (!smooth) {
@@ -1227,6 +1226,7 @@ var useSmooth = (text, smooth = false) => {
1227
1226
  }
1228
1227
  if (!text.startsWith(animatorRef.targetText)) {
1229
1228
  setDisplayedText(text);
1229
+ animatorRef.currentText = text;
1230
1230
  animatorRef.targetText = text;
1231
1231
  animatorRef.stop();
1232
1232
  return;