TypeScript
perf(web): the pending user input panel compiles with the React Compiler (#20592)
Julius Marminge4e3f936fbda8
diff --git a/apps/web/src/components/chat/ComposerPendingUserInputPanel.tsx b/apps/web/src/components/chat/ComposerPendingUserInputPanel.tsxindex 59f1ca46976..232177df2d3 100644--- a/apps/web/src/components/chat/ComposerPendingUserInputPanel.tsx+++ b/apps/web/src/components/chat/ComposerPendingUserInputPanel.tsx@@ -1,5 +1,6 @@import { type RuntimeRequestId, type ScopedThreadRef } from "@t3tools/contracts";-import { memo, useCallback, useEffect, useRef, useState } from "react";+import { memo, useEffect, useEffectEvent, useRef, useState } from "react";+import { useStableCallback } from "~/hooks/useStableCallback";import { type PendingUserInput } from "../../session-logic";import {derivePendingUserInputProgress,@@ -57,6 +58,16 @@ export const ComposerPendingUserInputPanel = memo(function ComposerPendingUserIn);});+// The active question keys the shortcut listener, so it must keep its identity across renders.+function usePendingUserInputProgress(+ questions: PendingUserInput["questions"],+ answers: Record<string, PendingUserInputDraftAnswer>,+ questionIndex: number,+) {+ "use memo";+ return derivePendingUserInputProgress(questions, answers, questionIndex);+}+const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard({prompt,cwd,@@ -83,10 +94,11 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(// Message-mode requests remain answerable after their provider turn ends.const canRespond = prompt.responseCapability !== "not_resumable";const responseDisabled = disabled || isResponding || !canRespond;- const progress = derivePendingUserInputProgress(prompt.questions, answers, questionIndex);+ const progress = usePendingUserInputProgress(prompt.questions, answers, questionIndex);const activeQuestion = progress.activeQuestion;const autoAdvanceTimerRef = useRef<number | null>(null);- const onAdvanceRef = useRef(onAdvance);+ // The auto-advance timer fires after the render that armed it.+ const advance = useStableCallback(onAdvance);const [optimisticSingleSelect, setOptimisticSingleSelect] = useState<{questionId: string;optionValue: string;@@ -94,30 +106,17 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(const [collapsedQuestionId, setCollapsedQuestionId] = useState<string | null>(null);const isCollapsed = collapsedQuestionId !== null && collapsedQuestionId === activeQuestion?.id;- useEffect(() => {- onAdvanceRef.current = onAdvance;- }, [onAdvance]);-- useEffect(() => {- if (!activeQuestion || activeQuestion.multiSelect || !optimisticSingleSelect) {- return;- }- if (optimisticSingleSelect.questionId !== activeQuestion.id) {- setOptimisticSingleSelect(null);- return;- }- if (- progress.customAnswer.trim().length === 0 &&- progress.selectedOptionValues.includes(optimisticSingleSelect.optionValue)- ) {- setOptimisticSingleSelect(null);- }- }, [- activeQuestion,- optimisticSingleSelect,- progress.customAnswer,- progress.selectedOptionValues,- ]);+ // The optimistic pick is dropped once the draft confirms it or the question moves on.+ if (+ optimisticSingleSelect &&+ activeQuestion &&+ !activeQuestion.multiSelect &&+ (optimisticSingleSelect.questionId !== activeQuestion.id ||+ (progress.customAnswer.trim().length === 0 &&+ progress.selectedOptionValues.includes(optimisticSingleSelect.optionValue)))+ ) {+ setOptimisticSingleSelect(null);+ }// A delayed answer only advances its own question, while permission remains granted.useEffect(() => {@@ -131,25 +130,24 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(return clearAutoAdvance;}, [activeQuestion?.id, disabled]);- const handleOptionSelection = useCallback(- (questionId: string, optionValue: string) => {- if (disabled || isResponding) return;- if (activeQuestion?.multiSelect) {- onToggleOption(questionId, optionValue);- return;- }- setOptimisticSingleSelect({ questionId, optionValue });+ const handleOptionSelection = (questionId: string, optionValue: string) => {+ if (disabled || isResponding) return;+ if (activeQuestion?.multiSelect) {onToggleOption(questionId, optionValue);- if (autoAdvanceTimerRef.current !== null) {- window.clearTimeout(autoAdvanceTimerRef.current);- }- autoAdvanceTimerRef.current = window.setTimeout(() => {- autoAdvanceTimerRef.current = null;- onAdvanceRef.current();- }, 200);- },- [activeQuestion, disabled, isResponding, onToggleOption],- );+ return;+ }+ setOptimisticSingleSelect({ questionId, optionValue });+ onToggleOption(questionId, optionValue);+ if (autoAdvanceTimerRef.current !== null) {+ window.clearTimeout(autoAdvanceTimerRef.current);+ }+ autoAdvanceTimerRef.current = window.setTimeout(() => {+ autoAdvanceTimerRef.current = null;+ advance();+ }, 200);+ };++ const selectOptionFromShortcut = useEffectEvent(handleOptionSelection);useEffect(() => {if (!activeQuestion || responseDisabled || isCollapsed) return;@@ -172,11 +170,11 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(const option = activeQuestion.options[optionIndex];if (!option) return;event.preventDefault();- handleOptionSelection(activeQuestion.id, option.value ?? option.label);+ selectOptionFromShortcut(activeQuestion.id, option.value ?? option.label);};document.addEventListener("keydown", handler);return () => document.removeEventListener("keydown", handler);- }, [activeQuestion, handleOptionSelection, isCollapsed, responseDisabled]);+ }, [activeQuestion, isCollapsed, responseDisabled]);if (!activeQuestion) {return null;