Skip to content

Commit 5ce8d13

Browse files
committed
feat(web): question tabs show answers and clicks never send
1 parent 380bba3 commit 5ce8d13

10 files changed

Lines changed: 197 additions & 12 deletions

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ import {
214214
buildPendingUserInputAnswers,
215215
carryDisplacedCustomAnswerIntoPrompt,
216216
derivePendingUserInputProgress,
217+
findFirstUnansweredPendingUserInputQuestionIndex,
217218
projectPendingUserInputAnswer,
218219
setPendingUserInputCustomAnswer,
219220
togglePendingUserInputOptionSelection,
@@ -10455,11 +10456,20 @@ export default function ChatView(props: ChatViewProps) {
1045510456
if (activePendingProgress.isLastQuestion) {
1045610457
if (activePendingResolvedAnswers) {
1045710458
void onRespondToUserInput(activePendingUserInput.requestId, activePendingResolvedAnswers);
10459+
return;
1045810460
}
10461+
// Submitting with gaps shows the first question still waiting for an answer.
10462+
setActivePendingUserInputQuestionIndex(
10463+
findFirstUnansweredPendingUserInputQuestionIndex(
10464+
activePendingUserInput.questions,
10465+
activePendingDraftAnswers,
10466+
),
10467+
);
1045910468
return;
1046010469
}
1046110470
setActivePendingUserInputQuestionIndex(activePendingProgress.questionIndex + 1);
1046210471
}, [
10472+
activePendingDraftAnswers,
1046310473
activePendingProgress,
1046410474
activePendingResolvedAnswers,
1046510475
activePendingUserInput,
@@ -11880,6 +11890,9 @@ export default function ChatView(props: ChatViewProps) {
1188011890
onPreviousActivePendingUserInputQuestion={
1188111891
onPreviousActivePendingUserInputQuestion
1188211892
}
11893+
onSelectActivePendingUserInputQuestion={
11894+
setActivePendingUserInputQuestionIndex
11895+
}
1188311896
onChangeActivePendingUserInputCustomAnswer={
1188411897
onChangeActivePendingUserInputCustomAnswer
1188511898
}

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,7 @@ const ComposerFooterPrimaryActions = memo(function ComposerFooterPrimaryActions(
13711371
canAdvance: boolean;
13721372
isResponding: boolean;
13731373
isComplete: boolean;
1374+
unansweredCount: number;
13741375
} | null;
13751376
isRunning: boolean;
13761377
canInterrupt: boolean;
@@ -1579,6 +1580,7 @@ export interface ChatComposerProps {
15791580
questionIndex: number;
15801581
isLastQuestion: boolean;
15811582
canAdvance: boolean;
1583+
answeredQuestionCount: number;
15821584
customAnswer: string;
15831585
activeQuestion: {
15841586
id: string;
@@ -1682,6 +1684,7 @@ export interface ChatComposerProps {
16821684
onAdvanceActivePendingUserInput: () => void;
16831685
onDismissActivePendingUserInput: (requestId: RuntimeRequestId) => void;
16841686
onPreviousActivePendingUserInputQuestion: () => void;
1687+
onSelectActivePendingUserInputQuestion: (questionIndex: number) => void;
16851688
onChangeActivePendingUserInputCustomAnswer: (
16861689
questionId: string,
16871690
value: string,
@@ -1801,6 +1804,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
18011804
onAdvanceActivePendingUserInput,
18021805
onDismissActivePendingUserInput,
18031806
onPreviousActivePendingUserInputQuestion,
1807+
onSelectActivePendingUserInputQuestion,
18041808
onChangeActivePendingUserInputCustomAnswer,
18051809
onProviderModelSelect,
18061810
onOpenProviderSetup,
@@ -3043,11 +3047,15 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
30433047
!(questionPreparations[attachmentTargetKey] ?? 0),
30443048
isResponding: activePendingIsResponding,
30453049
isComplete: Boolean(activePendingResolvedAnswers),
3050+
unansweredCount:
3051+
(pendingUserInputs[0]?.questions.length ?? 0) -
3052+
activePendingProgress.answeredQuestionCount,
30463053
}
30473054
: null,
30483055
[
30493056
activePendingIsResponding,
30503057
activePendingProgress,
3058+
pendingUserInputs,
30513059
activePendingResolvedAnswers,
30523060
attachmentBlockReason,
30533061
questionPreparations,
@@ -6879,6 +6887,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
68796887
questionIndex={activePendingQuestionIndex}
68806888
onToggleOption={onSelectActivePendingUserInputOption}
68816889
onAdvance={onAdvanceActivePendingUserInput}
6890+
onSelectQuestion={onSelectActivePendingUserInputQuestion}
68826891
onDismiss={onDismissActivePendingUserInput}
68836892
/>
68846893
) : !isComposerCollapsedMobile && showPlanFollowUpPrompt && activeProposedPlan ? (
@@ -6900,6 +6909,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
69006909
questionIndex={activePendingQuestionIndex}
69016910
onToggleOption={onSelectActivePendingUserInputOption}
69026911
onAdvance={onAdvanceActivePendingUserInput}
6912+
onSelectQuestion={onSelectActivePendingUserInputQuestion}
69036913
onDismiss={onDismissActivePendingUserInput}
69046914
/>
69056915
{!isChoiceOnlyPendingQuestion ||

‎apps/web/src/components/chat/ComposerPendingUserInputPanel.navigation.test.tsx‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ function Questions({ initialIndex = 0 }: { initialIndex?: number }) {
4949
answers={answers}
5050
questionIndex={questionIndex}
5151
onAdvance={advance}
52+
onSelectQuestion={setQuestionIndex}
5253
onToggleOption={(questionId, value) => {
5354
const question = prompt.questions.find((entry) => entry.id === questionId)!;
5455
setAnswers((existing) => ({
@@ -87,8 +88,10 @@ async function chooseOption() {
8788
await act(async () => {
8889
renderer!.root
8990
.findAllByType("button")
90-
.find((button) =>
91-
button.findAllByType("span").some((span) => span.children.includes("Incremental")),
91+
.find(
92+
(button) =>
93+
button.props.role !== "tab" &&
94+
button.findAllByType("span").some((span) => span.children.includes("Incremental")),
9295
)!
9396
.props.onClick();
9497
});
@@ -142,3 +145,26 @@ it("still auto-advances once after the answer draft rerenders the same question"
142145
await act(async () => vi.advanceTimersByTime(200));
143146
expect(activeIndex()).toBe("1");
144147
});
148+
149+
it("does not submit when the last question is answered", async () => {
150+
await act(async () => {
151+
renderer = create(<Questions initialIndex={2} />);
152+
});
153+
await chooseOption();
154+
await act(async () => vi.advanceTimersByTime(200));
155+
expect(activeIndex()).toBe("2");
156+
});
157+
158+
it("jumps to a question from its tab, which shows the chosen answer", async () => {
159+
await act(async () => {
160+
renderer = create(<Questions />);
161+
});
162+
await chooseOption();
163+
await act(async () => vi.advanceTimersByTime(200));
164+
const tabs = () =>
165+
renderer!.root.findAll((node) => node.props.role === "tab" && node.type === "button");
166+
expect(tabs()[0]!.props["aria-label"]).toBe("Question 1: Incremental");
167+
expect(tabs()[2]!.props["aria-label"]).toBe("Question 3");
168+
await act(async () => tabs()[2]!.props.onClick());
169+
expect(activeIndex()).toBe("2");
170+
});

‎apps/web/src/components/chat/ComposerPendingUserInputPanel.permissions.test.tsx‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ vi.mock("../ui/collapsible", () => {
1414

1515
import { ComposerPendingUserInputPanel } from "./ComposerPendingUserInputPanel";
1616

17-
it("cancels an answer's pending auto-submit on revocation and resumes after a new grant", async () => {
17+
it("cancels an answer's pending auto-advance on revocation and resumes after a new grant", async () => {
1818
vi.useFakeTimers();
1919
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
2020
vi.stubGlobal("window", {
@@ -38,6 +38,13 @@ it("cancels an answer's pending auto-submit on revocation and resumes after a ne
3838
options: [{ label: "Incremental", description: "One module at a time" }],
3939
multiSelect: false,
4040
},
41+
{
42+
id: "scope",
43+
header: "Scope",
44+
question: "How much?",
45+
options: [{ label: "All of it", description: "Every module" }],
46+
multiSelect: false,
47+
},
4148
],
4249
};
4350
const panel = (disabled: boolean) => (
@@ -49,6 +56,7 @@ it("cancels an answer's pending auto-submit on revocation and resumes after a ne
4956
questionIndex={0}
5057
onToggleOption={onToggleOption}
5158
onAdvance={onAdvance}
59+
onSelectQuestion={() => {}}
5260
onDismiss={onDismiss}
5361
/>
5462
);

‎apps/web/src/components/chat/ComposerPendingUserInputPanel.test.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ function renderPanel(pendingUserInput: PendingUserInput = prompt) {
3333
questionIndex={0}
3434
onToggleOption={() => {}}
3535
onAdvance={() => {}}
36+
onSelectQuestion={() => {}}
3637
onDismiss={() => {}}
3738
/>,
3839
);

‎apps/web/src/components/chat/ComposerPendingUserInputPanel.tsx‎

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { memo, useCallback, useEffect, useRef, useState } from "react";
33
import { type PendingUserInput } from "../../session-logic";
44
import {
55
derivePendingUserInputProgress,
6+
summarizePendingUserInputAnswer,
67
type PendingUserInputDraftAnswer,
78
} from "../../pendingUserInput";
89
import { CheckIcon } from "lucide-react";
@@ -17,7 +18,9 @@ interface PendingUserInputPanelProps {
1718
answers: Record<string, PendingUserInputDraftAnswer>;
1819
questionIndex: number;
1920
onToggleOption: (questionId: string, optionValue: string) => void;
21+
/** Moves past an answered question; never called on the last one. */
2022
onAdvance: () => void;
23+
onSelectQuestion: (questionIndex: number) => void;
2124
onDismiss: (requestId: RuntimeRequestId) => void;
2225
}
2326

@@ -29,6 +32,7 @@ export const ComposerPendingUserInputPanel = memo(function ComposerPendingUserIn
2932
questionIndex,
3033
onToggleOption,
3134
onAdvance,
35+
onSelectQuestion,
3236
onDismiss,
3337
}: PendingUserInputPanelProps) {
3438
if (pendingUserInputs.length === 0) return null;
@@ -45,6 +49,7 @@ export const ComposerPendingUserInputPanel = memo(function ComposerPendingUserIn
4549
questionIndex={questionIndex}
4650
onToggleOption={onToggleOption}
4751
onAdvance={onAdvance}
52+
onSelectQuestion={onSelectQuestion}
4853
onDismiss={onDismiss}
4954
/>
5055
);
@@ -58,6 +63,7 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
5863
questionIndex,
5964
onToggleOption,
6065
onAdvance,
66+
onSelectQuestion,
6167
onDismiss,
6268
}: {
6369
prompt: PendingUserInput;
@@ -67,6 +73,7 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
6773
questionIndex: number;
6874
onToggleOption: (questionId: string, optionValue: string) => void;
6975
onAdvance: () => void;
76+
onSelectQuestion: (questionIndex: number) => void;
7077
onDismiss: (requestId: RuntimeRequestId) => void;
7178
}) {
7279
// Message-mode requests remain answerable after their provider turn ends.
@@ -126,6 +133,8 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
126133
return clearAutoAdvance;
127134
}, [activeQuestion?.id, disabled]);
128135

136+
// Choosing an option moves on to the next question, but never submits: the
137+
// last question waits for Submit so a click can't send answers by accident.
129138
const handleOptionSelection = useCallback(
130139
(questionId: string, optionValue: string) => {
131140
if (disabled || isResponding) return;
@@ -137,19 +146,20 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
137146
onToggleOption(questionId, optionValue);
138147
if (autoAdvanceTimerRef.current !== null) {
139148
window.clearTimeout(autoAdvanceTimerRef.current);
149+
autoAdvanceTimerRef.current = null;
140150
}
151+
if (progress.isLastQuestion) return;
141152
autoAdvanceTimerRef.current = window.setTimeout(() => {
142153
autoAdvanceTimerRef.current = null;
143154
onAdvanceRef.current();
144155
}, 200);
145156
},
146-
[activeQuestion, disabled, isResponding, onToggleOption],
157+
[activeQuestion, disabled, isResponding, onToggleOption, progress.isLastQuestion],
147158
);
148159

149-
// Keyboard shortcut: number keys 1-9 select corresponding options when focus is
150-
// outside editable fields. Multi-select prompts toggle options in place; single-
151-
// select prompts keep the existing auto-advance behavior. Collapsed prompts opt
152-
// out, since the numbers they refer to are not on screen.
160+
// Keyboard shortcuts when focus is outside editable fields: number keys 1-9
161+
// select the corresponding option, and left/right arrows switch questions.
162+
// Collapsed prompts opt out, since what the keys refer to is not on screen.
153163
useEffect(() => {
154164
if (!activeQuestion || responseDisabled || isCollapsed) return;
155165
const handler = (event: globalThis.KeyboardEvent) => {
@@ -164,6 +174,13 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
164174
) {
165175
return;
166176
}
177+
if (event.key === "ArrowLeft" || event.key === "ArrowRight") {
178+
const nextIndex = progress.questionIndex + (event.key === "ArrowLeft" ? -1 : 1);
179+
if (nextIndex < 0 || nextIndex >= prompt.questions.length) return;
180+
event.preventDefault();
181+
onSelectQuestion(nextIndex);
182+
return;
183+
}
167184
const digit = Number.parseInt(event.key, 10);
168185
if (Number.isNaN(digit) || digit < 1 || digit > 9) return;
169186
const optionIndex = digit - 1;
@@ -175,7 +192,15 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
175192
};
176193
document.addEventListener("keydown", handler);
177194
return () => document.removeEventListener("keydown", handler);
178-
}, [activeQuestion, handleOptionSelection, isCollapsed, responseDisabled]);
195+
}, [
196+
activeQuestion,
197+
handleOptionSelection,
198+
isCollapsed,
199+
onSelectQuestion,
200+
progress.questionIndex,
201+
prompt.questions.length,
202+
responseDisabled,
203+
]);
179204

180205
if (!activeQuestion) {
181206
return null;
@@ -200,7 +225,9 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
200225
<ComposerBanner.Icon />
201226
<ComposerBanner.Content>
202227
<span className="shrink-0 font-medium text-muted-foreground">
203-
{activeQuestion.header}
228+
{isCollapsed || prompt.questions.length === 1
229+
? activeQuestion.header
230+
: `${progress.answeredQuestionCount} of ${prompt.questions.length} answered`}
204231
</span>
205232
{isCollapsed ? (
206233
<span className="min-w-0 flex-1 truncate text-secondary-label">
@@ -209,7 +236,7 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
209236
) : null}
210237
</ComposerBanner.Content>
211238
<ComposerBanner.Actions>
212-
{prompt.questions.length > 1 ? (
239+
{isCollapsed && prompt.questions.length > 1 ? (
213240
<span className="text-3xs font-medium text-muted-foreground tabular-nums">
214241
{questionIndex + 1}/{prompt.questions.length}
215242
</span>
@@ -240,6 +267,45 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
240267
</ComposerBanner.Actions>
241268
</CollapsibleTrigger>
242269
<CollapsiblePanel>
270+
{prompt.questions.length > 1 ? (
271+
<ComposerBanner.Body className="pe-1 pb-2">
272+
<div
273+
role="tablist"
274+
aria-label="Questions"
275+
className="flex gap-1 overflow-x-auto [scrollbar-width:none]"
276+
>
277+
{prompt.questions.map((question, index) => {
278+
const summary = summarizePendingUserInputAnswer(question, answers[question.id]);
279+
const isActive = index === progress.questionIndex;
280+
return (
281+
<button
282+
key={question.id}
283+
type="button"
284+
role="tab"
285+
aria-selected={isActive}
286+
aria-label={summary ? `${question.header}: ${summary}` : question.header}
287+
data-pending-user-input-tab={summary === null ? "unanswered" : "answered"}
288+
onClick={() => onSelectQuestion(index)}
289+
className={cn(
290+
"flex max-w-48 min-w-0 shrink-0 cursor-pointer items-center gap-1 rounded-md px-2 py-1 text-2xs outline-none transition-colors duration-150 focus-visible:ring-1 focus-visible:ring-inset focus-visible:ring-primary/25",
291+
isActive
292+
? "bg-muted/70 text-foreground"
293+
: "text-muted-foreground hover:bg-muted/30 hover:text-foreground/85",
294+
)}
295+
>
296+
<span className="shrink-0 font-medium">{question.header}</span>
297+
{summary !== null ? (
298+
<>
299+
<CheckIcon className="size-3 shrink-0 text-primary" />
300+
<span className="min-w-0 truncate text-secondary-label">{summary}</span>
301+
</>
302+
) : null}
303+
</button>
304+
);
305+
})}
306+
</div>
307+
</ComposerBanner.Body>
308+
) : null}
243309
<ComposerBanner.Scroll>
244310
<ComposerBanner.Body className="pe-1 pb-1 wrap-anywhere">
245311
<p className="text-sm text-foreground/85">{activeQuestion.question}</p>

‎apps/web/src/components/chat/ComposerPrimaryActions.test.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function renderPendingActions(isRunning: boolean) {
2828
canAdvance: true,
2929
isResponding: false,
3030
isComplete: true,
31+
unansweredCount: 0,
3132
},
3233
isRunning,
3334
canInterrupt: isRunning,

0 commit comments

Comments
 (0)