{"version":3,"file":"useMessageTextStreaming.mjs","names":[],"sources":["../../../../../src/components/Message/hooks/useMessageTextStreaming.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nimport { useStableCallback } from '../../../utils/useStableCallback';\nimport type { StreamedMessageTextProps } from '../StreamedMessageText';\n\nexport type UseMessageTextStreamingProps = Pick<\n  StreamedMessageTextProps,\n  'streamingLetterIntervalMs' | 'renderingLetterCount'\n> & { text: string };\n\nconst DEFAULT_LETTER_INTERVAL = 30;\nconst DEFAULT_RENDERING_LETTER_COUNT = 2;\n\n/**\n * Reveals `text` in a typewriter fashion as it grows. The text present on mount shows at once.\n *\n * @param params.streamingLetterIntervalMs - Delay in ms between reveal steps. Defaults to 30.\n * @param params.renderingLetterCount - Number of letters revealed per step. Defaults to 2.\n * @param params.text - Full text to reveal.\n * @returns `streamedMessageText`, the part of `text` revealed so far, and `skipAnimation`, which\n * reveals all of it at once.\n */\nexport const useMessageTextStreaming = ({\n  renderingLetterCount = DEFAULT_RENDERING_LETTER_COUNT,\n  streamingLetterIntervalMs = DEFAULT_LETTER_INTERVAL,\n  text,\n}: UseMessageTextStreamingProps) => {\n  const [streamedMessageText, setStreamedMessageText] = useState<string>(text);\n  const textCursor = useRef<number>(text.length);\n\n  useEffect(() => {\n    const textLength = text.length;\n\n    const interval = setInterval(() => {\n      if (!text || textCursor.current >= textLength) {\n        clearInterval(interval);\n        return;\n      }\n      const newCursorValue = textCursor.current + renderingLetterCount;\n      const newText = text.substring(0, newCursorValue);\n      textCursor.current += newText.length - textCursor.current;\n      setStreamedMessageText(newText);\n    }, streamingLetterIntervalMs);\n\n    return () => {\n      clearInterval(interval);\n    };\n  }, [streamingLetterIntervalMs, renderingLetterCount, text]);\n\n  const skipAnimation = useStableCallback(() => {\n    textCursor.current = text.length;\n    setStreamedMessageText(text);\n  });\n\n  return { skipAnimation, streamedMessageText } as const;\n};\n"],"mappings":";;;AAUA,IAAM,0BAA0B;AAChC,IAAM,iCAAiC;;;;;;;;;;AAWvC,IAAa,2BAA2B,EACtC,uBAAuB,gCACvB,4BAA4B,yBAC5B,WACkC;CAClC,MAAM,CAAC,qBAAqB,0BAA0B,SAAiB,IAAI;CAC3E,MAAM,aAAa,OAAe,KAAK,MAAM;CAE7C,gBAAgB;EACd,MAAM,aAAa,KAAK;EAExB,MAAM,WAAW,kBAAkB;GACjC,IAAI,CAAC,QAAQ,WAAW,WAAW,YAAY;IAC7C,cAAc,QAAQ;IACtB;GACF;GACA,MAAM,iBAAiB,WAAW,UAAU;GAC5C,MAAM,UAAU,KAAK,UAAU,GAAG,cAAc;GAChD,WAAW,WAAW,QAAQ,SAAS,WAAW;GAClD,uBAAuB,OAAO;EAChC,GAAG,yBAAyB;EAE5B,aAAa;GACX,cAAc,QAAQ;EACxB;CACF,GAAG;EAAC;EAA2B;EAAsB;CAAI,CAAC;CAO1D,OAAO;EAAE,eALa,wBAAwB;GAC5C,WAAW,UAAU,KAAK;GAC1B,uBAAuB,IAAI;EAC7B,CAES;EAAe;CAAoB;AAC9C"}