@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.js CHANGED
@@ -2827,48 +2827,57 @@ var React32 = __toESM(require("react"));
2827
2827
  // src/components/hooks/use-text-streaming.ts
2828
2828
  var import_react11 = require("react");
2829
2829
  var CHAR_DELAY = 25;
2830
- var PUNCTUATION_DELAY = 200;
2831
- var PUNCTUATION_MARKS = [".", "!", "?", ";", ":"];
2830
+ var PUNCTUATION_DELAY = 400;
2832
2831
  function useTextStreaming(content, shouldStream, handleIsTextStreaming) {
2833
- const [displayedContent, setDisplayedContent] = (0, import_react11.useState)("");
2834
- const timeoutId = (0, import_react11.useRef)(null);
2832
+ const [displayedText, setDisplayedText] = (0, import_react11.useState)("");
2833
+ const contentRef = (0, import_react11.useRef)(content);
2834
+ const indexRef = (0, import_react11.useRef)(0);
2835
+ const timerRef = (0, import_react11.useRef)(null);
2835
2836
  const hasStartedStreaming = (0, import_react11.useRef)(false);
2836
- const getDelayForChar = (text, position) => {
2837
- if (position === 0) return CHAR_DELAY;
2838
- const previousChar = text[position - 1] || "";
2839
- const isEllipsis = text.slice(position - 1, position + 2) === "...";
2840
- if (isEllipsis) {
2841
- return CHAR_DELAY;
2842
- }
2843
- return PUNCTUATION_MARKS.includes(previousChar) ? PUNCTUATION_DELAY : CHAR_DELAY;
2844
- };
2845
2837
  (0, import_react11.useEffect)(() => {
2846
- if (!shouldStream && !hasStartedStreaming.current) {
2847
- setDisplayedContent(content);
2848
- handleIsTextStreaming?.(false);
2849
- return;
2838
+ contentRef.current = content;
2839
+ if (hasStartedStreaming.current && !timerRef.current && indexRef.current < contentRef.current.length) {
2840
+ typeNext();
2850
2841
  }
2851
- handleIsTextStreaming?.(true);
2852
- hasStartedStreaming.current = true;
2853
- if (displayedContent.length < content.length) {
2854
- const delay = getDelayForChar(content, displayedContent.length);
2855
- timeoutId.current = setTimeout(() => {
2856
- setDisplayedContent((prev) => prev + content[prev.length]);
2842
+ }, [content]);
2843
+ const typeNext = (0, import_react11.useCallback)(() => {
2844
+ if (indexRef.current < contentRef.current.length) {
2845
+ const nextChar = contentRef.current.charAt(indexRef.current);
2846
+ setDisplayedText((prev) => prev + nextChar);
2847
+ indexRef.current++;
2848
+ const delay = /[.!?;:]/.test(nextChar) ? PUNCTUATION_DELAY : CHAR_DELAY;
2849
+ timerRef.current = setTimeout(() => {
2850
+ timerRef.current = null;
2851
+ typeNext();
2857
2852
  }, delay);
2858
2853
  } else {
2859
2854
  handleIsTextStreaming?.(false);
2860
2855
  }
2856
+ }, []);
2857
+ (0, import_react11.useEffect)(() => {
2858
+ if (!shouldStream && !hasStartedStreaming.current) {
2859
+ setDisplayedText(contentRef.current);
2860
+ indexRef.current = contentRef.current.length;
2861
+ if (timerRef.current) {
2862
+ clearTimeout(timerRef.current);
2863
+ timerRef.current = null;
2864
+ }
2865
+ handleIsTextStreaming?.(false);
2866
+ } else {
2867
+ if (indexRef.current < contentRef.current.length && !timerRef.current) {
2868
+ handleIsTextStreaming?.(true);
2869
+ hasStartedStreaming.current = true;
2870
+ typeNext();
2871
+ }
2872
+ }
2861
2873
  return () => {
2862
- if (timeoutId.current) clearTimeout(timeoutId.current);
2874
+ if (timerRef.current) {
2875
+ clearTimeout(timerRef.current);
2876
+ timerRef.current = null;
2877
+ }
2863
2878
  };
2864
- }, [
2865
- content,
2866
- displayedContent,
2867
- shouldStream,
2868
- getDelayForChar,
2869
- handleIsTextStreaming
2870
- ]);
2871
- return displayedContent;
2879
+ }, [shouldStream, typeNext]);
2880
+ return displayedText;
2872
2881
  }
2873
2882
 
2874
2883
  // src/components/molecules/call-out.tsx