mirror of
https://github.com/StanGirard/quivr.git
synced 2024-12-22 10:51:46 +03:00
ea227df30e
https://github.com/StanGirard/quivr/assets/63923024/08554dd5-2765-436a-b900-48b38d582f6c https://github.com/StanGirard/quivr/issues/1389
53 lines
1.2 KiB
TypeScript
53 lines
1.2 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
|
|
type UseStreamTextProps = {
|
|
text: string;
|
|
enabled?: boolean;
|
|
shouldStream?: boolean;
|
|
};
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
export const useStreamText = ({
|
|
text,
|
|
enabled = true,
|
|
shouldStream = true,
|
|
}: UseStreamTextProps) => {
|
|
const [streamingText, setStreamingText] = useState<string>("");
|
|
const [lastStreamIndex, setLastStreamIndex] = useState(0);
|
|
|
|
const isDone = lastStreamIndex === text.length;
|
|
|
|
const lastStream = !isDone ? text[lastStreamIndex] : "";
|
|
|
|
useEffect(() => {
|
|
if (!enabled) {
|
|
setStreamingText("");
|
|
|
|
return;
|
|
}
|
|
|
|
if (!shouldStream) {
|
|
setStreamingText(text);
|
|
setLastStreamIndex(text.length);
|
|
|
|
return;
|
|
}
|
|
|
|
const messageInterval = setInterval(() => {
|
|
if (lastStreamIndex < text.length) {
|
|
setStreamingText(
|
|
(prevText) => prevText + (text[lastStreamIndex] ?? "")
|
|
);
|
|
setLastStreamIndex((prevIndex) => prevIndex + 1);
|
|
} else {
|
|
clearInterval(messageInterval);
|
|
}
|
|
}, 30);
|
|
|
|
return () => {
|
|
clearInterval(messageInterval);
|
|
};
|
|
}, [text, lastStreamIndex, enabled, shouldStream]);
|
|
|
|
return { streamingText, isDone, lastStream };
|
|
};
|