@assistant-ui/react 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1244,7 +1244,8 @@ var import_react36 = require("react");
1244
1244
  // src/utils/hooks/useSmooth.tsx
1245
1245
  var import_react35 = require("react");
1246
1246
  var TextStreamAnimator = class {
1247
- constructor(setText) {
1247
+ constructor(currentText, setText) {
1248
+ this.currentText = currentText;
1248
1249
  this.setText = setText;
1249
1250
  }
1250
1251
  animationFrameId = null;
@@ -1265,33 +1266,31 @@ var TextStreamAnimator = class {
1265
1266
  const currentTime = Date.now();
1266
1267
  const deltaTime = currentTime - this.lastUpdateTime;
1267
1268
  let timeToConsume = deltaTime;
1268
- this.setText((currentText) => {
1269
- const targetText = this.targetText;
1270
- if (currentText === targetText) {
1271
- this.animationFrameId = null;
1272
- return currentText;
1273
- }
1274
- const remainingChars = targetText.length - currentText.length;
1275
- const baseTimePerChar = Math.min(5, 250 / remainingChars);
1276
- let charsToAdd = 0;
1277
- while (timeToConsume >= baseTimePerChar && charsToAdd < remainingChars) {
1278
- charsToAdd++;
1279
- timeToConsume -= baseTimePerChar;
1280
- }
1269
+ const remainingChars = this.targetText.length - this.currentText.length;
1270
+ const baseTimePerChar = Math.min(5, 250 / remainingChars);
1271
+ let charsToAdd = 0;
1272
+ while (timeToConsume >= baseTimePerChar && charsToAdd < remainingChars) {
1273
+ charsToAdd++;
1274
+ timeToConsume -= baseTimePerChar;
1275
+ }
1276
+ if (charsToAdd !== remainingChars) {
1281
1277
  this.animationFrameId = requestAnimationFrame(this.animate);
1282
- if (charsToAdd === 0) {
1283
- return currentText;
1284
- }
1285
- const newText = targetText.slice(0, currentText.length + charsToAdd);
1286
- this.lastUpdateTime = currentTime - timeToConsume;
1287
- return newText;
1288
- });
1278
+ } else {
1279
+ this.animationFrameId = null;
1280
+ }
1281
+ if (charsToAdd === 0) return;
1282
+ this.currentText = this.targetText.slice(
1283
+ 0,
1284
+ this.currentText.length + charsToAdd
1285
+ );
1286
+ this.lastUpdateTime = currentTime - timeToConsume;
1287
+ this.setText(this.currentText);
1289
1288
  };
1290
1289
  };
1291
1290
  var useSmooth = (text, smooth = false) => {
1292
1291
  const [displayedText, setDisplayedText] = (0, import_react35.useState)(text);
1293
1292
  const [animatorRef] = (0, import_react35.useState)(
1294
- new TextStreamAnimator(setDisplayedText)
1293
+ new TextStreamAnimator(text, setDisplayedText)
1295
1294
  );
1296
1295
  (0, import_react35.useEffect)(() => {
1297
1296
  if (!smooth) {
@@ -1300,6 +1299,7 @@ var useSmooth = (text, smooth = false) => {
1300
1299
  }
1301
1300
  if (!text.startsWith(animatorRef.targetText)) {
1302
1301
  setDisplayedText(text);
1302
+ animatorRef.currentText = text;
1303
1303
  animatorRef.targetText = text;
1304
1304
  animatorRef.stop();
1305
1305
  return;