-
Notifications
You must be signed in to change notification settings - Fork 5.9k
ui: setting configuration #1597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 113 additions & 6 deletions
119
...rc/components/settings_v2/providers/modal/subcomponents/handlers/DefaultSubmitHandler.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,114 @@ | ||
| export default function DefaultSubmitHandler(configValues) { | ||
| // Log each field value individually for clarity | ||
| console.log('Field values:'); | ||
| Object.entries(configValues).forEach(([key, value]) => { | ||
| console.log(`${key}: ${value}`); | ||
| import { useConfig } from '../../../../../ConfigContext'; | ||
| import React from 'react'; | ||
|
|
||
| /** | ||
| * Custom hook for provider configuration submission | ||
| * Returns a submit handler function and submission state | ||
| */ | ||
| export const useDefaultSubmit = () => { | ||
| const { upsert } = useConfig(); | ||
| const [isSubmitting, setIsSubmitting] = React.useState(false); | ||
| const [error, setError] = React.useState(null); | ||
| const [isSuccess, setIsSuccess] = React.useState(false); | ||
|
|
||
| /** | ||
| * Submit handler for provider configuration | ||
| * @param {Object} provider - The provider object with metadata | ||
| * @param {Object} configValues - The form values to be submitted | ||
| * @param {Function} onSuccess - Optional callback for successful submission | ||
| */ | ||
| const handleSubmit = async (provider, configValues, onSuccess) => { | ||
| setIsSubmitting(true); | ||
| setError(null); | ||
| setIsSuccess(false); | ||
|
|
||
| try { | ||
| const parameters = provider.metadata.config_keys || []; | ||
|
|
||
| // Create an array of promises for all the upsert operations | ||
| const upsertPromises = parameters.map((parameter) => { | ||
| // Skip parameters that don't have a value and aren't required | ||
| if (!configValues[parameter.name] && !parameter.required) { | ||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| // For required parameters with no value, use the default if available | ||
| const value = | ||
| configValues[parameter.name] !== undefined | ||
| ? configValues[parameter.name] | ||
| : parameter.default; | ||
|
|
||
| // Skip if there's still no value | ||
| if (value === undefined || value === null) { | ||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| // Create the provider-specific config key | ||
| // Format: provider.{provider_name}.{parameter_name} | ||
| const configKey = `provider.${provider.name}.${parameter.name}`; | ||
|
|
||
| // Pass the is_secret flag from the parameter definition | ||
| return upsert(configKey, value, parameter.secret || false); | ||
| }); | ||
|
|
||
| // Wait for all upsert operations to complete | ||
| await Promise.all(upsertPromises); | ||
|
|
||
| setIsSuccess(true); | ||
|
|
||
| // Call the success callback if provided | ||
| if (onSuccess) { | ||
| onSuccess(); | ||
| } | ||
| } catch (err) { | ||
| console.error('Failed to save provider configuration:', err); | ||
| setError('Failed to save configuration. Please try again.'); | ||
| } finally { | ||
| setIsSubmitting(false); | ||
| } | ||
| }; | ||
|
|
||
| return { | ||
| handleSubmit, | ||
| isSubmitting, | ||
| error, | ||
| isSuccess, | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Standalone function to submit provider configuration | ||
| * Useful for components that don't want to use the hook | ||
| */ | ||
| export const DefaultSubmitHandler = async (upsertFn, provider, configValues) => { | ||
| const parameters = provider.metadata.config_keys || []; | ||
|
|
||
| const upsertPromises = parameters.map((parameter) => { | ||
| // Skip parameters that don't have a value and aren't required | ||
| if (!configValues[parameter.name] && !parameter.required) { | ||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| // For required parameters with no value, use the default if available | ||
| const value = | ||
| configValues[parameter.name] !== undefined ? configValues[parameter.name] : parameter.default; | ||
|
|
||
| // Skip if there's still no value | ||
| if (value === undefined || value === null) { | ||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| // Create the provider-specific config key | ||
| const configKey = `${parameter.name}`; | ||
|
|
||
| // Explicitly define is_secret as a boolean (true/false) or null | ||
| // This is critical for Rust's Option<bool> type | ||
| const isSecret = parameter.secret === true; | ||
|
|
||
| // Pass the is_secret flag from the parameter definition | ||
| return upsertFn(configKey, value, isSecret); | ||
| }); | ||
| } | ||
|
|
||
| // Wait for all upsert operations to complete | ||
| return Promise.all(upsertPromises); | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.