@@ -142,23 +142,29 @@ export const ExtensionStateContextProvider: React.FC<{ children: React.ReactNode
142142 } , [ ] )
143143
144144 const handleInputChange = useCallback (
145+ // Returns a function that handles an input change event for a specific API configuration field.
146+ // The optional "softUpdate" flag determines whether to immediately update local state or send an external update.
145147 ( field : keyof ApiConfiguration , softUpdate ?: boolean ) => ( event : any ) => {
146- if ( softUpdate === true ) {
147- setState ( ( currentState ) => {
148+ // Use the functional form of setState to ensure the latest state is used in the update logic.
149+ setState ( ( currentState ) => {
150+ if ( softUpdate ) {
151+ // Return a new state object with the updated apiConfiguration.
152+ // This will trigger a re-render with the new configuration value.
148153 return {
149154 ...currentState ,
150155 apiConfiguration : { ...currentState . apiConfiguration , [ field ] : event . target . value } ,
151156 }
152- } )
153- return
154- }
155- setState ( ( currentState ) => {
156- vscode . postMessage ( {
157- type : "upsertApiConfiguration" ,
158- text : currentState . currentApiConfigName ,
159- apiConfiguration : { ...currentState . apiConfiguration , [ field ] : event . target . value } ,
160- } )
161- return currentState // No state update needed
157+ } else {
158+ // For non-soft updates, send a message to the VS Code extension with the updated config.
159+ // This side effect communicates the change without updating local React state.
160+ vscode . postMessage ( {
161+ type : "upsertApiConfiguration" ,
162+ text : currentState . currentApiConfigName ,
163+ apiConfiguration : { ...currentState . apiConfiguration , [ field ] : event . target . value } ,
164+ } )
165+ // Return the unchanged state as no local state update is intended in this branch.
166+ return currentState
167+ }
162168 } )
163169 } ,
164170 [ ] ,
0 commit comments