TypeScript
fix(web): Enter no longer skips an unanswered question (#23626)
Mantas Kantautas6ca95bd92a06
diff --git a/apps/web/src/components/chat/ComposerPendingUserInputPanel.navigation.browser.test.tsx b/apps/web/src/components/chat/ComposerPendingUserInputPanel.navigation.browser.test.tsxindex 52800d94a8b..58ad8e8c090 100644--- a/apps/web/src/components/chat/ComposerPendingUserInputPanel.navigation.browser.test.tsx+++ b/apps/web/src/components/chat/ComposerPendingUserInputPanel.navigation.browser.test.tsx@@ -9,6 +9,7 @@ import {import { makeThreadRequestsResolvedAnnouncer } from "@t3tools/client-runtime/state/thread-requests";import { useLayoutEffect, useRef, type ComponentProps } from "react";import { expect, it, vi } from "vite-plus/test";+import { userEvent } from "vite-plus/test/browser";vi.mock(import("../ChatMarkdown"), async (importOriginal) => ({...(await importOriginal()),@@ -73,20 +74,16 @@ function questionProjection(threadId: ThreadId, requestIds = ["first", "second"]} satisfies OrchestrationV2ThreadProjection;}-let requests: ReturnType<typeof usePendingThreadRequests>;--function PendingRequestsProbe({- environmentId,- projection,- loading = false,-}: {+interface ProbeProps {environmentId: EnvironmentId;projection: OrchestrationV2ThreadProjection;loading?: boolean;-}) {+}++function useProbeRequests({ environmentId, projection, loading = false }: ProbeProps) {const promptRef = useRef("");const composerRef = useRef(null);- const state = usePendingThreadRequests({+ return usePendingThreadRequests({environmentId,activeThreadId: projection.thread.id,serverProjection: loading ? null : projection,@@ -97,6 +94,12 @@ function PendingRequestsProbe({composerRef,setThreadError: () => {},});+}++let requests: ReturnType<typeof usePendingThreadRequests>;++function PendingRequestsProbe(props: ProbeProps) {+ const state = useProbeRequests(props);useLayoutEffect(() => {requests = state;});@@ -138,6 +141,40 @@ it("retains answers, selections and question position after leaving chat, includexpect(requests).toMatchObject(expected);});+function QuestionPositionProbe(props: ProbeProps) {+ const state = useProbeRequests(props);+ return (+ <>+ <output>Question {state.activePendingQuestionIndex + 1}</output>+ <button+ type="button"+ onClick={() => state.onSelectActivePendingUserInputOption("question-0", "Incremental")}+ >+ Choose Incremental+ </button>+ <button type="button" onClick={() => state.onAdvanceActivePendingUserInput()}>+ Submit+ </button>+ </>+ );+}++it("stays on an unanswered question when the composer submits", async () => {+ await using screen = await render(+ <QuestionPositionProbe+ environmentId={EnvironmentId.make("unanswered-environment")}+ projection={questionProjection(threadRef.threadId)}+ />,+ );+ const submit = screen.getByRole("button", { name: "Submit" });+ await userEvent.click(submit);+ await expect.element(screen.getByRole("status")).toHaveTextContent("Question 1");++ await userEvent.click(screen.getByRole("button", { name: "Choose Incremental" }));+ await userEvent.click(submit);+ await expect.element(screen.getByRole("status")).toHaveTextContent("Question 2");+});+it("isolates drafts by environment, thread and request and discards only resolved requests", async () => {using _timers = fakeTimers();const environmentId = EnvironmentId.make("scope-environment");diff --git a/apps/web/src/components/chat/usePendingThreadRequests.ts b/apps/web/src/components/chat/usePendingThreadRequests.tsindex 91c5bd0d6c9..8a07b86b5ee 100644--- a/apps/web/src/components/chat/usePendingThreadRequests.ts+++ b/apps/web/src/components/chat/usePendingThreadRequests.ts@@ -473,6 +473,7 @@ export function usePendingThreadRequests(input: {}return;}+ if (!activePendingProgress.canAdvance) return;setActivePendingUserInputQuestionIndex(activePendingProgress.questionIndex + 1);};const onAdvanceActivePendingUserInputStable = useStableCallback(onAdvanceActivePendingUserInput);