Skip to content

Commit

Permalink
feat: add shouldStream guard on useStreamText
Browse files Browse the repository at this point in the history
  • Loading branch information
mamadoudicko committed Oct 4, 2023
1 parent b5a3554 commit f01a9de
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ import { checkIfShouldDisplayStep } from "../helpers/checkIfShouldDisplayStep";
import { useStreamText } from "../hooks/useStreamText";
import { stepsContainerStyle } from "../styles";

const stepId = "DOWNLOAD";

export const Step1 = (): JSX.Element => {
const { currentStep, setCurrentStep } = useOnboardingContext();
const shouldStepBeDisplayed = checkIfShouldDisplayStep({
currentStep,
step: "DOWNLOAD",
step: stepId,
});

const shouldStreamMessage = currentStep === stepId;

const { t } = useTranslation(["chat"]);
const firstMessage = t("onboarding.download_message_1");
const secondMessageStream = t("onboarding.download_message_2");
Expand All @@ -26,11 +30,13 @@ export const Step1 = (): JSX.Element => {
useStreamText({
text: firstMessage,
enabled: shouldStepBeDisplayed,
shouldStream: shouldStreamMessage,
});
const { streamingText: firstMessageStrem, isDone: isStep1Done } =
useStreamText({
text: secondMessageStream,
enabled: isAssistantDone && shouldStepBeDisplayed,
shouldStream: shouldStreamMessage,
});

if (!shouldStepBeDisplayed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,32 @@ import { checkIfShouldDisplayStep } from "../helpers/checkIfShouldDisplayStep";
import { useStreamText } from "../hooks/useStreamText";
import { stepsContainerStyle } from "../styles";

const stepId = "UPLOAD";

export const Step2 = (): JSX.Element => {
const { currentStep } = useOnboardingContext();
const shouldStepBeDisplayed = checkIfShouldDisplayStep({
currentStep,
step: "UPLOAD",
step: stepId,
});

const { t } = useTranslation(["chat"]);
const firstMessage = t("onboarding.upload_message_1");
const secondMessageStream = t("onboarding.upload_message_2");

const shouldStreamMessage = currentStep === stepId;

const { streamingText: streamingAssistantMessage, isDone: isAssistantDone } =
useStreamText({
text: firstMessage,
enabled: shouldStepBeDisplayed,
shouldStream: shouldStreamMessage,
});

const { streamingText: firstMessageStream } = useStreamText({
text: secondMessageStream,
enabled: isAssistantDone && shouldStepBeDisplayed,
shouldStream: shouldStreamMessage,
});

if (!shouldStepBeDisplayed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,31 @@ import { checkIfShouldDisplayStep } from "../helpers/checkIfShouldDisplayStep";
import { useStreamText } from "../hooks/useStreamText";
import { stepsContainerStyle } from "../styles";

const stepId = "UPLOADED";

export const Step3 = (): JSX.Element => {
const { currentStep } = useOnboardingContext();
const shouldStepBeDisplayed = checkIfShouldDisplayStep({
currentStep,
step: "UPLOADED",
step: stepId,
});

const { t } = useTranslation(["chat"]);
const firstMessage = t("onboarding.last_step");
const secondMessageStream = t("onboarding.ask_question_to_file");
const shouldStreamMessage = currentStep === stepId;

const { streamingText: streamingAssistantMessage, isDone: isAssistantDone } =
useStreamText({
text: firstMessage,
enabled: shouldStepBeDisplayed,
shouldStream: shouldStreamMessage,
});

const { streamingText: firstMessageStream } = useStreamText({
text: secondMessageStream,
enabled: isAssistantDone && shouldStepBeDisplayed,
shouldStream: shouldStreamMessage,
});

if (!shouldStepBeDisplayed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@ 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 }: UseStreamTextProps) => {
export const useStreamText = ({
text,
enabled = true,
shouldStream = true,
}: UseStreamTextProps) => {
const [streamingText, setStreamingText] = useState<string>("");
const [currentIndex, setCurrentIndex] = useState(0);

const isDone = currentIndex === text.length;

useEffect(() => {
if (!shouldStream) {
setStreamingText(text);
setCurrentIndex(text.length);

return;
}

if (!enabled) {
setStreamingText("");

Expand Down

0 comments on commit f01a9de

Please sign in to comment.