@alquimia-ai/ui 1.2.3 → 1.2.4

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.
@@ -2455,48 +2455,57 @@ var React32 = __toESM(require("react"));
2455
2455
  // src/components/hooks/use-text-streaming.ts
2456
2456
  var import_react8 = require("react");
2457
2457
  var CHAR_DELAY = 25;
2458
- var PUNCTUATION_DELAY = 200;
2459
- var PUNCTUATION_MARKS = [".", "!", "?", ";", ":"];
2458
+ var PUNCTUATION_DELAY = 400;
2460
2459
  function useTextStreaming(content, shouldStream, handleIsTextStreaming) {
2461
- const [displayedContent, setDisplayedContent] = (0, import_react8.useState)("");
2462
- const timeoutId = (0, import_react8.useRef)(null);
2460
+ const [displayedText, setDisplayedText] = (0, import_react8.useState)("");
2461
+ const contentRef = (0, import_react8.useRef)(content);
2462
+ const indexRef = (0, import_react8.useRef)(0);
2463
+ const timerRef = (0, import_react8.useRef)(null);
2463
2464
  const hasStartedStreaming = (0, import_react8.useRef)(false);
2464
- const getDelayForChar = (text, position) => {
2465
- if (position === 0) return CHAR_DELAY;
2466
- const previousChar = text[position - 1] || "";
2467
- const isEllipsis = text.slice(position - 1, position + 2) === "...";
2468
- if (isEllipsis) {
2469
- return CHAR_DELAY;
2470
- }
2471
- return PUNCTUATION_MARKS.includes(previousChar) ? PUNCTUATION_DELAY : CHAR_DELAY;
2472
- };
2473
2465
  (0, import_react8.useEffect)(() => {
2474
- if (!shouldStream && !hasStartedStreaming.current) {
2475
- setDisplayedContent(content);
2476
- handleIsTextStreaming?.(false);
2477
- return;
2466
+ contentRef.current = content;
2467
+ if (hasStartedStreaming.current && !timerRef.current && indexRef.current < contentRef.current.length) {
2468
+ typeNext();
2478
2469
  }
2479
- handleIsTextStreaming?.(true);
2480
- hasStartedStreaming.current = true;
2481
- if (displayedContent.length < content.length) {
2482
- const delay = getDelayForChar(content, displayedContent.length);
2483
- timeoutId.current = setTimeout(() => {
2484
- setDisplayedContent((prev) => prev + content[prev.length]);
2470
+ }, [content]);
2471
+ const typeNext = (0, import_react8.useCallback)(() => {
2472
+ if (indexRef.current < contentRef.current.length) {
2473
+ const nextChar = contentRef.current.charAt(indexRef.current);
2474
+ setDisplayedText((prev) => prev + nextChar);
2475
+ indexRef.current++;
2476
+ const delay = /[.!?;:]/.test(nextChar) ? PUNCTUATION_DELAY : CHAR_DELAY;
2477
+ timerRef.current = setTimeout(() => {
2478
+ timerRef.current = null;
2479
+ typeNext();
2485
2480
  }, delay);
2486
2481
  } else {
2487
2482
  handleIsTextStreaming?.(false);
2488
2483
  }
2484
+ }, []);
2485
+ (0, import_react8.useEffect)(() => {
2486
+ if (!shouldStream && !hasStartedStreaming.current) {
2487
+ setDisplayedText(contentRef.current);
2488
+ indexRef.current = contentRef.current.length;
2489
+ if (timerRef.current) {
2490
+ clearTimeout(timerRef.current);
2491
+ timerRef.current = null;
2492
+ }
2493
+ handleIsTextStreaming?.(false);
2494
+ } else {
2495
+ if (indexRef.current < contentRef.current.length && !timerRef.current) {
2496
+ handleIsTextStreaming?.(true);
2497
+ hasStartedStreaming.current = true;
2498
+ typeNext();
2499
+ }
2500
+ }
2489
2501
  return () => {
2490
- if (timeoutId.current) clearTimeout(timeoutId.current);
2502
+ if (timerRef.current) {
2503
+ clearTimeout(timerRef.current);
2504
+ timerRef.current = null;
2505
+ }
2491
2506
  };
2492
- }, [
2493
- content,
2494
- displayedContent,
2495
- shouldStream,
2496
- getDelayForChar,
2497
- handleIsTextStreaming
2498
- ]);
2499
- return displayedContent;
2507
+ }, [shouldStream, typeNext]);
2508
+ return displayedText;
2500
2509
  }
2501
2510
 
2502
2511
  // src/components/molecules/call-out.tsx