import { useState } from 'react' import type { FC } from 'react' import { ValidatingTip } from '../../key-validator/ValidateStatus' import type { CredentialFormSchema, CredentialFormSchemaRadio, CredentialFormSchemaSecretInput, CredentialFormSchemaSelect, CredentialFormSchemaTextInput, FormValue, } from '../declarations' import { FormTypeEnum } from '../declarations' import { useLanguage } from '../hooks' import Input from './Input' import { SimpleSelect } from '@/app/components/base/select' type FormProps = { value: FormValue onChange: (val: FormValue) => void formSchemas: CredentialFormSchema[] validating: boolean validatedSuccess?: boolean showOnVariableMap: Record isEditMode: boolean } const Form: FC = ({ value, onChange, formSchemas, validating, validatedSuccess, showOnVariableMap, isEditMode, }) => { const language = useLanguage() const [changeKey, setChangeKey] = useState('') const handleFormChange = (key: string, val: string) => { if (isEditMode && (key === '__model_type' || key === '__model_name')) return setChangeKey(key) const shouldClearVariable: Record = {} if (showOnVariableMap[key]?.length) { showOnVariableMap[key].forEach((clearVariable) => { shouldClearVariable[clearVariable] = undefined }) } onChange({ ...value, [key]: val, ...shouldClearVariable }) } const renderField = (formSchema: CredentialFormSchema) => { if (formSchema.type === FormTypeEnum.textInput || formSchema.type === FormTypeEnum.secretInput) { const { variable, label, placeholder, required, show_on, } = formSchema as (CredentialFormSchemaTextInput | CredentialFormSchemaSecretInput) if (show_on.length && !show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value)) return null const disabed = isEditMode && (variable === '__model_type' || variable === '__model_name') return (
{label[language]} { required && ( * ) }
handleFormChange(variable, val)} validated={validatedSuccess} placeholder={placeholder?.[language]} disabled={disabed} /> {validating && changeKey === variable && }
) } if (formSchema.type === FormTypeEnum.radio) { const { options, variable, label, show_on, required, } = formSchema as CredentialFormSchemaRadio if (show_on.length && !show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value)) return null const disabed = isEditMode && (variable === '__model_type' || variable === '__model_name') return (
{label[language]} { required && ( * ) }
{ options.filter((option) => { if (option.show_on.length) return option.show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value) return true }).map(option => (
handleFormChange(variable, option.value)} key={`${variable}-${option.value}`} >
{option.label[language]}
)) }
{validating && changeKey === variable && }
) } if (formSchema.type === 'select') { const { options, variable, label, show_on, required, placeholder, } = formSchema as CredentialFormSchemaSelect if (show_on.length && !show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value)) return null return (
{label[language]} { required && ( * ) }
{ if (option.show_on.length) return option.show_on.every(showOnItem => value[showOnItem.variable] === showOnItem.value) return true }).map(option => ({ value: option.value, name: option.label[language] }))} onSelect={item => handleFormChange(variable, item.value as string)} placeholder={placeholder?.[language]} /> {validating && changeKey === variable && }
) } } return (
{ formSchemas.map(formSchema => renderField(formSchema)) }
) } export default Form