The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 23:15 UTC
fix(clients): show question option previews (#21933)
Julius Marminge committed
Oct 19, 2026 at 21:12 UTC
36641146c9c0f99d7f9193edbd47f13ccf389c84
4 files changed
+238
-17
apps/mobile/src/features/threads/PendingUserInputCard.tsx
+58
-9
@@ -2,7 +2,7 @@ import { Button } from "../../components/ui/Button";
2
import { QuestionAttachments } from "./QuestionAttachments";
3
import type { RuntimeRequestId } from "@t3tools/contracts";
4
import type { ThreadUserInputQuestion } from "@t3tools/client-runtime/state/thread-requests";
5
-import { useCallback, useRef } from "react";
5
+import { useCallback, useRef, useState } from "react";
6
import { Platform, Pressable, ScrollView, View, type LayoutChangeEvent } from "react-native";
7
import Animated, {
8
Easing,
@@ -79,6 +79,22 @@ function QuestionText(props: { readonly markdown: string }) {
79
}
80
81
export function PendingUserInputCard(props: PendingUserInputCardProps) {
82
+ const [focusedOption, setFocusedOption] = useState<{
83
+ requestId: RuntimeRequestId;
84
+ questionId: string;
85
+ optionIndex: number;
86
+ } | null>(null);
87
+ const focusedQuestion = props.pendingUserInput.questions.find(
88
+ (question) => question.id === focusedOption?.questionId,
89
+ );
90
+ if (
91
+ focusedOption &&
92
+ (focusedOption.requestId !== props.pendingUserInput.requestId ||
93
+ !focusedQuestion ||
94
+ (props.drafts[focusedQuestion.id]?.customAnswer?.trim().length ?? 0) > 0)
95
+ ) {
96
+ setFocusedOption(null);
97
+ }
98
const questionCount = props.pendingUserInput.questions.length;
99
const canRespond = props.pendingUserInput.responseCapability !== "not_resumable";
100
const isResponding = props.respondingUserInputId === props.pendingUserInput.requestId;
@@ -238,6 +254,19 @@ export function PendingUserInputCard(props: PendingUserInputCardProps) {
254
) : null}
255
{props.pendingUserInput.questions.map((question) => {
256
const draft = props.drafts[question.id];
257
+ const customAnswerActive = (draft?.customAnswer?.trim().length ?? 0) > 0;
258
+ const selectedOptionValue = draft?.selectedOptionValues?.at(-1);
259
+ const savedOptionMatches = question.options.filter(
260
+ (option) => (option.value ?? option.label.trim()) === selectedOptionValue,
261
+ );
262
+ const previewOption = customAnswerActive
263
+ ? undefined
264
+ : focusedOption?.requestId === props.pendingUserInput.requestId &&
265
+ focusedOption.questionId === question.id
266
+ ? question.options[focusedOption.optionIndex]
267
+ : savedOptionMatches.length === 1
268
+ ? savedOptionMatches[0]
269
+ : undefined;
270
return (
271
<View key={question.id} className="gap-2 pt-1">
272
<Text className="font-t3-bold text-xs uppercase tracking-[1px] text-foreground-muted">
@@ -245,14 +274,16 @@ export function PendingUserInputCard(props: PendingUserInputCardProps) {
274
</Text>
275
<QuestionText markdown={question.question} />
276
<View className="gap-2">
248
- {question.options.map((option) => {
277
+ {question.options.map((option, optionIndex) => {
278
const optionValue = option.value ?? option.label.trim();
279
const selected = isPendingUserInputOptionSelected(question, draft, optionValue);
280
const description =
281
option.description !== option.label ? option.description : undefined;
282
return (
283
<Pressable
255
- key={optionValue}
284
+ // Options keep their positions within a question and may share answer values.
285
+ // oxlint-disable-next-line react/no-array-index-key
286
+ key={`${question.id}:${optionIndex}`}
287
accessibilityRole={question.multiSelect ? "checkbox" : "radio"}
288
accessibilityState={{ checked: selected, disabled: responseDisabled }}
289
disabled={responseDisabled}
@@ -260,13 +291,18 @@ export function PendingUserInputCard(props: PendingUserInputCardProps) {
291
"min-h-12 w-full rounded-2xl border px-3.5 py-3",
292
selected ? "border-primary bg-primary/10" : "border-border bg-input",
293
)}
263
- onPress={() =>
294
+ onPress={() => {
295
+ setFocusedOption({
296
+ requestId: props.pendingUserInput.requestId,
297
+ questionId: question.id,
298
+ optionIndex,
299
+ });
300
props.onSelectOption(
301
props.pendingUserInput.requestId,
302
question,
303
optionValue,
268
- )
269
- }
304
+ );
305
+ }}
306
>
307
<View className="min-w-0 flex-1 gap-0.5">
308
<Text
@@ -287,6 +323,14 @@ export function PendingUserInputCard(props: PendingUserInputCardProps) {
323
);
324
})}
325
</View>
326
+ {previewOption?.preview?.trim() ? (
327
+ <View className="gap-1.5 rounded-2xl border border-border bg-input p-3.5">
328
+ <Text className="font-t3-bold text-xs text-foreground-muted">
329
+ Preview · {previewOption.label}
330
+ </Text>
331
+ <QuestionText markdown={previewOption.preview} />
332
+ </View>
333
+ ) : null}
334
{question.allowCustomAnswer !== false ? (
335
<QuestionAttachments
336
requestId={props.pendingUserInput.requestId}
@@ -294,9 +338,14 @@ export function PendingUserInputCard(props: PendingUserInputCardProps) {
338
questions={props.pendingUserInput.questions}
339
disabled={responseDisabled}
340
value={draft?.customAnswer ?? ""}
297
- onChangeText={(value) =>
298
- props.onChangeCustomAnswer(props.pendingUserInput.requestId, question.id, value)
299
- }
341
+ onChangeText={(value) => {
342
+ setFocusedOption(null);
343
+ props.onChangeCustomAnswer(
344
+ props.pendingUserInput.requestId,
345
+ question.id,
346
+ value,
347
+ );
348
+ }}
349
onInputFocusChange={props.onInputFocusChange}
350
/>
351
) : null}
apps/web/src/components/chat/ComposerPendingUserInputPanel.navigation.browser.test.tsx
+127
-3
@@ -11,9 +11,11 @@ import { createRoot, type Root } from "react-dom/client";
11
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
12
13
import {
14
+ setPendingUserInputCustomAnswer,
15
togglePendingUserInputOptionSelection,
16
type PendingUserInputDraftAnswer,
17
} from "../../pendingUserInput";
18
+import type { PendingUserInput } from "../../session-logic";
19
20
vi.mock(import("../ChatMarkdown"), async (importOriginal) => ({
21
...(await importOriginal()),
@@ -40,17 +42,48 @@ const prompt = {
42
})),
43
};
44
43
-function Questions({ initialIndex = 0 }: { initialIndex?: number }) {
45
+const previewPrompt = {
46
+ ...prompt,
47
+ questions: prompt.questions.map((question, index) => ({
48
+ ...question,
49
+ options: ["First", "Second"].map((label) => ({
50
+ label,
51
+ value: index === 1 ? label : "same-answer",
52
+ description: "",
53
+ preview: `Preview body: ${label}, question ${index + 1}`,
54
+ })),
55
+ })),
56
+};
57
+
58
+function Questions({
59
+ initialIndex = 0,
60
+ pendingPrompt = prompt,
61
+}: {
62
+ initialIndex?: number;
63
+ pendingPrompt?: PendingUserInput;
64
+}) {
65
const [questionIndex, setQuestionIndex] = useState(initialIndex);
66
const [answers, setAnswers] = useState<Record<string, PendingUserInputDraftAnswer>>({});
67
+ const question = pendingPrompt.questions[questionIndex]!;
68
const advance = () => setQuestionIndex(questionIndex + 1);
69
return (
70
<>
71
<output>{questionIndex}</output>
72
<button onClick={advance}>Next</button>
73
<button onClick={() => setQuestionIndex(questionIndex - 1)}>Previous</button>
74
+ <input
75
+ aria-label="Custom answer"
76
+ value={answers[question.id]?.customAnswer ?? ""}
77
+ onChange={(event) => {
78
+ const value = event.currentTarget.value;
79
+ setAnswers((existing) => ({
80
+ ...existing,
81
+ [question.id]: setPendingUserInputCustomAnswer(existing[question.id], value, question),
82
+ }));
83
+ }}
84
+ />
85
<ComposerPendingUserInputPanel
53
- pendingUserInputs={[prompt]}
86
+ pendingUserInputs={[pendingPrompt]}
87
cwd="/repo"
88
threadRef={threadRef}
89
disabled={false}
@@ -59,7 +92,7 @@ function Questions({ initialIndex = 0 }: { initialIndex?: number }) {
92
questionIndex={questionIndex}
93
onAdvance={advance}
94
onToggleOption={(questionId, value) => {
62
- const question = prompt.questions.find((entry) => entry.id === questionId)!;
95
+ const question = pendingPrompt.questions.find((entry) => entry.id === questionId)!;
96
setAnswers((existing) => ({
97
...existing,
98
[questionId]: togglePendingUserInputOptionSelection(
@@ -292,3 +325,94 @@ it("isolates drafts by environment, thread and request and discards only resolve
325
);
326
}
327
});
328
+
329
+function expectPreview(text: string | null) {
330
+ const previews = [...container.querySelectorAll("p")]
331
+ .map((element) => element.textContent)
332
+ .filter((content) => content?.startsWith("Preview body:"));
333
+ expect(previews).toEqual(text === null ? [] : [`Preview body: ${text}`]);
334
+}
335
+
336
+async function typeCustomAnswer(value: string) {
337
+ const input = container.querySelector("input")!;
338
+ await act(async () => {
339
+ input.focus();
340
+ Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(input, value);
341
+ input.dispatchEvent(new Event("input", { bubbles: true }));
342
+ });
343
+}
344
+
345
+it("previews options by focus, click, and shortcut index but only restores unambiguous saved selections", async () => {
346
+ await act(async () => root.render(<Questions pendingPrompt={previewPrompt} />));
347
+ expectPreview("First, question 1");
348
+
349
+ await act(async () => buttonWithText("Second").focus());
350
+ expectPreview("Second, question 1");
351
+ await act(async () => buttonWithText("First").focus());
352
+ expectPreview("First, question 1");
353
+
354
+ await act(async () => buttonWithText("Second").click());
355
+ expectPreview("Second, question 1");
356
+ await act(async () => document.dispatchEvent(new KeyboardEvent("keydown", { key: "1" })));
357
+ expectPreview("First, question 1");
358
+ await act(async () => document.dispatchEvent(new KeyboardEvent("keydown", { key: "2" })));
359
+ expectPreview("Second, question 1");
360
+
361
+ await act(async () => vi.advanceTimersByTime(200));
362
+ expect(activeIndex()).toBe("1");
363
+ expectPreview("First, question 2");
364
+
365
+ await act(async () => buttonWithText("Second").focus());
366
+ expectPreview("Second, question 2");
367
+ await act(async () =>
368
+ root.render(
369
+ <Questions
370
+ pendingPrompt={{ ...previewPrompt, requestId: RuntimeRequestId.make("replacement") }}
371
+ />,
372
+ ),
373
+ );
374
+ expectPreview("First, question 2");
375
+
376
+ await act(async () => buttonWithText("Second").click());
377
+ expectPreview("Second, question 2");
378
+ await navigate("Next");
379
+ expectPreview("First, question 3");
380
+ await navigate("Previous");
381
+ expectPreview("Second, question 2");
382
+
383
+ await navigate("Previous");
384
+ expectPreview(null);
385
+ await act(async () =>
386
+ root.render(
387
+ <Questions
388
+ pendingPrompt={{ ...previewPrompt, requestId: RuntimeRequestId.make("remounted") }}
389
+ />,
390
+ ),
391
+ );
392
+ expectPreview(null);
393
+ await act(async () => buttonWithText("Second").click());
394
+ expectPreview("Second, question 1");
395
+});
396
+
397
+it("hides and clears hovered or focused previews while a custom answer is active", async () => {
398
+ await act(async () => root.render(<Questions pendingPrompt={previewPrompt} />));
399
+ await act(async () =>
400
+ buttonWithText("Second").dispatchEvent(new MouseEvent("mouseover", { bubbles: true })),
401
+ );
402
+ expectPreview("Second, question 1");
403
+
404
+ await typeCustomAnswer("Use a different approach");
405
+ expectPreview(null);
406
+ await typeCustomAnswer("");
407
+ expectPreview("First, question 1");
408
+
409
+ await typeCustomAnswer("Use a different approach");
410
+ await act(async () => buttonWithText("Second").focus());
411
+ expectPreview(null);
412
+ await act(async () =>
413
+ buttonWithText("Second").dispatchEvent(new MouseEvent("mouseover", { bubbles: true })),
414
+ );
415
+ expectPreview(null);
416
+ await typeCustomAnswer("");
417
+ expectPreview("First, question 1");
418
+});
apps/web/src/components/chat/ComposerPendingUserInputPanel.tsx
+51
-4
@@ -91,6 +91,10 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
91
questionId: string;
92
optionValue: string;
93
} | null>(null);
94
+ const [focusedOption, setFocusedOption] = useState<{
95
+ questionId: string;
96
+ optionIndex: number;
97
+ } | null>(null);
98
const [collapsedQuestionId, setCollapsedQuestionId] = useState<string | null>(null);
99
// The active question keys the shortcut listener, so it must keep its identity across renders.
100
const progress = derivePendingUserInputProgress(prompt.questions, answers, questionIndex);
@@ -102,6 +106,23 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
106
!customAnswerActive && progress.selectedOptionValues.includes(option.value ?? option.label),
107
);
108
const isCollapsed = collapsedQuestionId !== null && collapsedQuestionId === activeQuestion?.id;
109
+ const selectedOptionValue = progress.selectedOptionValues.at(-1);
110
+ const savedOptionMatches = activeQuestion?.options.filter(
111
+ (option) => (option.value ?? option.label) === selectedOptionValue,
112
+ );
113
+ const previewOption = customAnswerActive
114
+ ? undefined
115
+ : focusedOption && focusedOption.questionId === activeQuestion?.id
116
+ ? activeQuestion?.options[focusedOption.optionIndex]
117
+ : selectedOptionValue === undefined
118
+ ? activeQuestion?.options[0]
119
+ : savedOptionMatches?.length === 1
120
+ ? savedOptionMatches[0]
121
+ : undefined;
122
+
123
+ if (focusedOption && (focusedOption.questionId !== activeQuestion?.id || customAnswerActive)) {
124
+ setFocusedOption(null);
125
+ }
126
127
// The optimistic pick is dropped once the draft confirms it or the question moves on.
128
if (
@@ -127,8 +148,9 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
148
return clearAutoAdvance;
149
}, [activeQuestion?.id, disabled]);
150
130
- const handleOptionSelection = (questionId: string, optionValue: string) => {
151
+ const handleOptionSelection = (questionId: string, optionValue: string, optionIndex: number) => {
152
if (disabled || isResponding) return;
153
+ setFocusedOption({ questionId, optionIndex });
154
if (activeQuestion?.multiSelect) {
155
onToggleOption(questionId, optionValue);
156
return;
@@ -167,7 +189,7 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
189
const option = activeQuestion.options[optionIndex];
190
if (!option) return;
191
event.preventDefault();
170
- selectOptionFromShortcut(activeQuestion.id, option.value ?? option.label);
192
+ selectOptionFromShortcut(activeQuestion.id, option.value ?? option.label, optionIndex);
193
};
194
document.addEventListener("keydown", handler);
195
return () => document.removeEventListener("keydown", handler);
@@ -283,11 +305,19 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
305
);
306
return (
307
<button
286
- key={`${activeQuestion.id}:${optionValue}`}
308
+ // Options keep their positions within a question and may share answer values.
309
+ // oxlint-disable-next-line react/no-array-index-key
310
+ key={`${activeQuestion.id}:${index}`}
311
type="button"
312
disabled={disabled || isResponding}
313
+ onMouseEnter={() =>
314
+ setFocusedOption({ questionId: activeQuestion.id, optionIndex: index })
315
+ }
316
+ onFocus={() =>
317
+ setFocusedOption({ questionId: activeQuestion.id, optionIndex: index })
318
+ }
319
onClick={() => {
290
- handleOptionSelection(activeQuestion.id, optionValue);
320
+ handleOptionSelection(activeQuestion.id, optionValue, index);
321
}}
322
className={className}
323
>
@@ -296,6 +326,23 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
326
);
327
})}
328
</div>
329
+ {previewOption?.preview?.trim() ? (
330
+ <section
331
+ aria-label={`Preview: ${previewOption.label}`}
332
+ className="mt-2 min-w-0 rounded-md border border-border/60 bg-muted/25 p-2.5"
333
+ >
334
+ <p className="mb-1.5 text-3xs font-medium text-muted-foreground">
335
+ Preview · {previewOption.label}
336
+ </p>
337
+ <ChatMarkdown
338
+ key={`${activeQuestion.id}:${activeQuestion.options.indexOf(previewOption)}`}
339
+ text={previewOption.preview}
340
+ cwd={cwd}
341
+ threadRef={threadRef}
342
+ parseRawHtml={false}
343
+ />
344
+ </section>
345
+ ) : null}
346
</ComposerBanner.Body>
347
</ComposerBanner.Scroll>
348
</CollapsiblePanel>
apps/web/src/pendingUserInput.ts
+2
-1
@@ -1,4 +1,5 @@
1
-import type { ThreadContextRecord, UserInputQuestion } from "@t3tools/contracts";
1
+import type { ThreadContextRecord } from "@t3tools/contracts";
2
+import type { ThreadUserInputQuestion as UserInputQuestion } from "@t3tools/client-runtime/state/thread-requests";
3
import { replaceComposerContextReferences } from "@t3tools/shared/composerContextReferences";
4
import { formatThreadLink } from "@t3tools/shared/threadLinks";
5