11import { 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" ;
34import { type PendingUserInput } from "../../session-logic" ;
45import {
56 derivePendingUserInputProgress ,
@@ -57,6 +58,16 @@ export const ComposerPendingUserInputPanel = memo(function ComposerPendingUserIn
5758 ) ;
5859} ) ;
5960
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+
6071const ComposerPendingUserInputCard = memo ( function ComposerPendingUserInputCard ( {
6172 prompt,
6273 cwd,
@@ -83,41 +94,29 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
8394 // Message-mode requests remain answerable after their provider turn ends.
8495 const canRespond = prompt . responseCapability !== "not_resumable" ;
8596 const responseDisabled = disabled || isResponding || ! canRespond ;
86- const progress = derivePendingUserInputProgress ( prompt . questions , answers , questionIndex ) ;
97+ const progress = usePendingUserInputProgress ( prompt . questions , answers , questionIndex ) ;
8798 const activeQuestion = progress . activeQuestion ;
8899 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 ) ;
90102 const [ optimisticSingleSelect , setOptimisticSingleSelect ] = useState < {
91103 questionId : string ;
92104 optionValue : string ;
93105 } | null > ( null ) ;
94106 const [ collapsedQuestionId , setCollapsedQuestionId ] = useState < string | null > ( null ) ;
95107 const isCollapsed = collapsedQuestionId !== null && collapsedQuestionId === activeQuestion ?. id ;
96108
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+ }
121120
122121 // A delayed answer only advances its own question, while permission remains granted.
123122 useEffect ( ( ) => {
@@ -131,25 +130,24 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
131130 return clearAutoAdvance ;
132131 } , [ activeQuestion ?. id , disabled ] ) ;
133132
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 ) {
142136 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 ) ;
153151
154152 useEffect ( ( ) => {
155153 if ( ! activeQuestion || responseDisabled || isCollapsed ) return ;
@@ -172,11 +170,11 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
172170 const option = activeQuestion . options [ optionIndex ] ;
173171 if ( ! option ) return ;
174172 event . preventDefault ( ) ;
175- handleOptionSelection ( activeQuestion . id , option . value ?? option . label ) ;
173+ selectOptionFromShortcut ( activeQuestion . id , option . value ?? option . label ) ;
176174 } ;
177175 document . addEventListener ( "keydown" , handler ) ;
178176 return ( ) => document . removeEventListener ( "keydown" , handler ) ;
179- } , [ activeQuestion , handleOptionSelection , isCollapsed , responseDisabled ] ) ;
177+ } , [ activeQuestion , isCollapsed , responseDisabled ] ) ;
180178
181179 if ( ! activeQuestion ) {
182180 return null ;
0 commit comments