import { memo, useCallback, useRef, } from 'react' import { useTranslation } from 'react-i18next' import Modal from '@/app/components/base/modal/modal' import { useInvalidPluginCredentialInfoHook, useSetPluginOAuthCustomClientHook, } from '../hooks/use-credential' import type { PluginPayload } from '../types' import AuthForm from '@/app/components/base/form/form-scenarios/auth' import type { FormRefObject, FormSchema, } from '@/app/components/base/form/types' import { FormTypeEnum } from '@/app/components/base/form/types' import { transformFormSchemasSecretInput } from '../utils' import { useToastContext } from '@/app/components/base/toast' type OAuthClientSettingsProps = { pluginPayload: PluginPayload onClose?: () => void editValues?: Record disabled?: boolean schemas: FormSchema[] onAuth?: () => Promise } const OAuthClientSettings = ({ pluginPayload, onClose, editValues, disabled, schemas, onAuth, }: OAuthClientSettingsProps) => { const { t } = useTranslation() const { notify } = useToastContext() const defaultValues = schemas.reduce((acc, schema) => { if (schema.default) acc[schema.name] = schema.default return acc }, {} as Record) const { mutateAsync: setPluginOAuthCustomClient } = useSetPluginOAuthCustomClientHook(pluginPayload) const invalidatePluginCredentialInfo = useInvalidPluginCredentialInfoHook(pluginPayload) const formRef = useRef(null) const handleConfirm = useCallback(async () => { const form = formRef.current?.getForm() const store = form?.store const { __oauth_client__, ...values } = store?.state.values const isPristineSecretInputNames: string[] = [] for (let i = 0; i < schemas.length; i++) { const schema = schemas[i] if (schema.required && !values[schema.name]) { notify({ type: 'error', message: t('common.errorMsg.fieldRequired', { field: schema.name }), }) return } if (schema.type === FormTypeEnum.secretInput) { const fieldMeta = form?.getFieldMeta(schema.name) if (fieldMeta?.isPristine) isPristineSecretInputNames.push(schema.name) } } const transformedValues = transformFormSchemasSecretInput(isPristineSecretInputNames, values) await setPluginOAuthCustomClient({ client_params: transformedValues, enable_oauth_custom_client: __oauth_client__ === 'custom', }) notify({ type: 'success', message: t('common.api.actionSuccess'), }) onClose?.() invalidatePluginCredentialInfo() }, [onClose, invalidatePluginCredentialInfo, setPluginOAuthCustomClient, notify, t, schemas]) const handleConfirmAndAuthorize = useCallback(async () => { await handleConfirm() if (onAuth) await onAuth() }, [handleConfirm, onAuth]) return ( ) } export default memo(OAuthClientSettings)