@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.
package/dist/index.mjs CHANGED
@@ -2626,50 +2626,59 @@ RatingComment.displayName = "RatingComment";
2626
2626
  import * as React32 from "react";
2627
2627
 
2628
2628
  // src/components/hooks/use-text-streaming.ts
2629
- import { useEffect as useEffect10, useRef, useState as useState10 } from "react";
2629
+ import { useState as useState10, useEffect as useEffect10, useRef, useCallback as useCallback2 } from "react";
2630
2630
  var CHAR_DELAY = 25;
2631
- var PUNCTUATION_DELAY = 200;
2632
- var PUNCTUATION_MARKS = [".", "!", "?", ";", ":"];
2631
+ var PUNCTUATION_DELAY = 400;
2633
2632
  function useTextStreaming(content, shouldStream, handleIsTextStreaming) {
2634
- const [displayedContent, setDisplayedContent] = useState10("");
2635
- const timeoutId = useRef(null);
2633
+ const [displayedText, setDisplayedText] = useState10("");
2634
+ const contentRef = useRef(content);
2635
+ const indexRef = useRef(0);
2636
+ const timerRef = useRef(null);
2636
2637
  const hasStartedStreaming = useRef(false);
2637
- const getDelayForChar = (text, position) => {
2638
- if (position === 0) return CHAR_DELAY;
2639
- const previousChar = text[position - 1] || "";
2640
- const isEllipsis = text.slice(position - 1, position + 2) === "...";
2641
- if (isEllipsis) {
2642
- return CHAR_DELAY;
2643
- }
2644
- return PUNCTUATION_MARKS.includes(previousChar) ? PUNCTUATION_DELAY : CHAR_DELAY;
2645
- };
2646
2638
  useEffect10(() => {
2647
- if (!shouldStream && !hasStartedStreaming.current) {
2648
- setDisplayedContent(content);
2649
- handleIsTextStreaming?.(false);
2650
- return;
2639
+ contentRef.current = content;
2640
+ if (hasStartedStreaming.current && !timerRef.current && indexRef.current < contentRef.current.length) {
2641
+ typeNext();
2651
2642
  }
2652
- handleIsTextStreaming?.(true);
2653
- hasStartedStreaming.current = true;
2654
- if (displayedContent.length < content.length) {
2655
- const delay = getDelayForChar(content, displayedContent.length);
2656
- timeoutId.current = setTimeout(() => {
2657
- setDisplayedContent((prev) => prev + content[prev.length]);
2643
+ }, [content]);
2644
+ const typeNext = useCallback2(() => {
2645
+ if (indexRef.current < contentRef.current.length) {
2646
+ const nextChar = contentRef.current.charAt(indexRef.current);
2647
+ setDisplayedText((prev) => prev + nextChar);
2648
+ indexRef.current++;
2649
+ const delay = /[.!?;:]/.test(nextChar) ? PUNCTUATION_DELAY : CHAR_DELAY;
2650
+ timerRef.current = setTimeout(() => {
2651
+ timerRef.current = null;
2652
+ typeNext();
2658
2653
  }, delay);
2659
2654
  } else {
2660
2655
  handleIsTextStreaming?.(false);
2661
2656
  }
2657
+ }, []);
2658
+ useEffect10(() => {
2659
+ if (!shouldStream && !hasStartedStreaming.current) {
2660
+ setDisplayedText(contentRef.current);
2661
+ indexRef.current = contentRef.current.length;
2662
+ if (timerRef.current) {
2663
+ clearTimeout(timerRef.current);
2664
+ timerRef.current = null;
2665
+ }
2666
+ handleIsTextStreaming?.(false);
2667
+ } else {
2668
+ if (indexRef.current < contentRef.current.length && !timerRef.current) {
2669
+ handleIsTextStreaming?.(true);
2670
+ hasStartedStreaming.current = true;
2671
+ typeNext();
2672
+ }
2673
+ }
2662
2674
  return () => {
2663
- if (timeoutId.current) clearTimeout(timeoutId.current);
2675
+ if (timerRef.current) {
2676
+ clearTimeout(timerRef.current);
2677
+ timerRef.current = null;
2678
+ }
2664
2679
  };
2665
- }, [
2666
- content,
2667
- displayedContent,
2668
- shouldStream,
2669
- getDelayForChar,
2670
- handleIsTextStreaming
2671
- ]);
2672
- return displayedContent;
2680
+ }, [shouldStream, typeNext]);
2681
+ return displayedText;
2673
2682
  }
2674
2683
 
2675
2684
  // src/components/molecules/call-out.tsx