The best way to control Claude Code, Codex and more from any computer or phone
perf(web): the pending user input panel compiles with the React Compiler (#20592)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 16, 2026 at 22:03 UTC
4e3f936fbda8e3eafae23551bcf61bd836092396
1 file changed
+45
-47
apps/web/src/components/chat/ComposerPendingUserInputPanel.tsx
+45
-47
@@ -1,5 +1,6 @@
1
import { type RuntimeRequestId, type ScopedThreadRef } from "@t3tools/contracts";
2
-import { memo, useCallback, useEffect, useRef, useState } from "react";
2
+import { memo, useEffect, useEffectEvent, useRef, useState } from "react";
3
+import { useStableCallback } from "~/hooks/useStableCallback";
4
import { type PendingUserInput } from "../../session-logic";
5
import {
6
derivePendingUserInputProgress,
@@ -57,6 +58,16 @@ export const ComposerPendingUserInputPanel = memo(function ComposerPendingUserIn
58
);
59
});
60
61
+// The active question keys the shortcut listener, so it must keep its identity across renders.
62
+function usePendingUserInputProgress(
63
+ questions: PendingUserInput["questions"],
64
+ answers: Record<string, PendingUserInputDraftAnswer>,
65
+ questionIndex: number,
66
+) {
67
+ "use memo";
68
+ return derivePendingUserInputProgress(questions, answers, questionIndex);
69
+}
70
+
71
const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard({
72
prompt,
73
cwd,
@@ -83,10 +94,11 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
94
// Message-mode requests remain answerable after their provider turn ends.
95
const canRespond = prompt.responseCapability !== "not_resumable";
96
const responseDisabled = disabled || isResponding || !canRespond;
86
- const progress = derivePendingUserInputProgress(prompt.questions, answers, questionIndex);
97
+ const progress = usePendingUserInputProgress(prompt.questions, answers, questionIndex);
98
const activeQuestion = progress.activeQuestion;
99
const autoAdvanceTimerRef = useRef<number | null>(null);
89
- const onAdvanceRef = useRef(onAdvance);
100
+ // The auto-advance timer fires after the render that armed it.
101
+ const advance = useStableCallback(onAdvance);
102
const [optimisticSingleSelect, setOptimisticSingleSelect] = useState<{
103
questionId: string;
104
optionValue: string;
@@ -94,30 +106,17 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
106
const [collapsedQuestionId, setCollapsedQuestionId] = useState<string | null>(null);
107
const isCollapsed = collapsedQuestionId !== null && collapsedQuestionId === activeQuestion?.id;
108
97
- useEffect(() => {
98
- onAdvanceRef.current = onAdvance;
99
- }, [onAdvance]);
100
-
101
- useEffect(() => {
102
- if (!activeQuestion || activeQuestion.multiSelect || !optimisticSingleSelect) {
103
- return;
104
- }
105
- if (optimisticSingleSelect.questionId !== activeQuestion.id) {
106
- setOptimisticSingleSelect(null);
107
- return;
108
- }
109
- if (
110
- progress.customAnswer.trim().length === 0 &&
111
- progress.selectedOptionValues.includes(optimisticSingleSelect.optionValue)
112
- ) {
113
- setOptimisticSingleSelect(null);
114
- }
115
- }, [
116
- activeQuestion,
117
- optimisticSingleSelect,
118
- progress.customAnswer,
119
- progress.selectedOptionValues,
120
- ]);
109
+ // The optimistic pick is dropped once the draft confirms it or the question moves on.
110
+ if (
111
+ optimisticSingleSelect &&
112
+ activeQuestion &&
113
+ !activeQuestion.multiSelect &&
114
+ (optimisticSingleSelect.questionId !== activeQuestion.id ||
115
+ (progress.customAnswer.trim().length === 0 &&
116
+ progress.selectedOptionValues.includes(optimisticSingleSelect.optionValue)))
117
+ ) {
118
+ setOptimisticSingleSelect(null);
119
+ }
120
121
// A delayed answer only advances its own question, while permission remains granted.
122
useEffect(() => {
@@ -131,25 +130,24 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
130
return clearAutoAdvance;
131
}, [activeQuestion?.id, disabled]);
132
134
- const handleOptionSelection = useCallback(
135
- (questionId: string, optionValue: string) => {
136
- if (disabled || isResponding) return;
137
- if (activeQuestion?.multiSelect) {
138
- onToggleOption(questionId, optionValue);
139
- return;
140
- }
141
- setOptimisticSingleSelect({ questionId, optionValue });
133
+ const handleOptionSelection = (questionId: string, optionValue: string) => {
134
+ if (disabled || isResponding) return;
135
+ if (activeQuestion?.multiSelect) {
136
onToggleOption(questionId, optionValue);
143
- if (autoAdvanceTimerRef.current !== null) {
144
- window.clearTimeout(autoAdvanceTimerRef.current);
145
- }
146
- autoAdvanceTimerRef.current = window.setTimeout(() => {
147
- autoAdvanceTimerRef.current = null;
148
- onAdvanceRef.current();
149
- }, 200);
150
- },
151
- [activeQuestion, disabled, isResponding, onToggleOption],
152
- );
137
+ return;
138
+ }
139
+ setOptimisticSingleSelect({ questionId, optionValue });
140
+ onToggleOption(questionId, optionValue);
141
+ if (autoAdvanceTimerRef.current !== null) {
142
+ window.clearTimeout(autoAdvanceTimerRef.current);
143
+ }
144
+ autoAdvanceTimerRef.current = window.setTimeout(() => {
145
+ autoAdvanceTimerRef.current = null;
146
+ advance();
147
+ }, 200);
148
+ };
149
+
150
+ const selectOptionFromShortcut = useEffectEvent(handleOptionSelection);
151
152
useEffect(() => {
153
if (!activeQuestion || responseDisabled || isCollapsed) return;
@@ -172,11 +170,11 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
170
const option = activeQuestion.options[optionIndex];
171
if (!option) return;
172
event.preventDefault();
175
- handleOptionSelection(activeQuestion.id, option.value ?? option.label);
173
+ selectOptionFromShortcut(activeQuestion.id, option.value ?? option.label);
174
};
175
document.addEventListener("keydown", handler);
176
return () => document.removeEventListener("keydown", handler);
179
- }, [activeQuestion, handleOptionSelection, isCollapsed, responseDisabled]);
177
+ }, [activeQuestion, isCollapsed, responseDisabled]);
178
179
if (!activeQuestion) {
180
return null;