Code cleanup

This commit is contained in:
Matt Rubens 2025-02-03 23:48:22 -05:00
parent f9299a98df
commit 3d2ba7b361

View file

@ -142,23 +142,29 @@ export const ExtensionStateContextProvider: React.FC<{ children: React.ReactNode
}, [])
const handleInputChange = useCallback(
// Returns a function that handles an input change event for a specific API configuration field.
// The optional "softUpdate" flag determines whether to immediately update local state or send an external update.
(field: keyof ApiConfiguration, softUpdate?: boolean) => (event: any) => {
if (softUpdate === true) {
setState((currentState) => {
// Use the functional form of setState to ensure the latest state is used in the update logic.
setState((currentState) => {
if (softUpdate) {
// Return a new state object with the updated apiConfiguration.
// This will trigger a re-render with the new configuration value.
return {
...currentState,
apiConfiguration: { ...currentState.apiConfiguration, [field]: event.target.value },
}
})
return
}
setState((currentState) => {
vscode.postMessage({
type: "upsertApiConfiguration",
text: currentState.currentApiConfigName,
apiConfiguration: { ...currentState.apiConfiguration, [field]: event.target.value },
})
return currentState // No state update needed
} else {
// For non-soft updates, send a message to the VS Code extension with the updated config.
// This side effect communicates the change without updating local React state.
vscode.postMessage({
type: "upsertApiConfiguration",
text: currentState.currentApiConfigName,
apiConfiguration: { ...currentState.apiConfiguration, [field]: event.target.value },
})
// Return the unchanged state as no local state update is intended in this branch.
return currentState
}
})
},
[],