import type { FC } from 'react' import { memo, useEffect, useState, } from 'react' import { useTranslation } from 'react-i18next' import { RiBook2Line, RiCloseLine, RiInformation2Line, RiLock2Fill, } from '@remixicon/react' import type { CreateExternalAPIReq, FormSchema } from '../declarations' import Form from './Form' import ActionButton from '@/app/components/base/action-button' import Confirm from '@/app/components/base/confirm' import { PortalToFollowElem, PortalToFollowElemContent, } from '@/app/components/base/portal-to-follow-elem' import { createExternalAPI } from '@/service/datasets' import { useToastContext } from '@/app/components/base/toast' import Button from '@/app/components/base/button' import Tooltip from '@/app/components/base/tooltip' type AddExternalAPIModalProps = { data?: CreateExternalAPIReq onSave: (formValue: CreateExternalAPIReq) => void onCancel: () => void onEdit?: (formValue: CreateExternalAPIReq) => Promise datasetBindings?: { id: string; name: string }[] isEditMode: boolean } const formSchemas: FormSchema[] = [ { variable: 'name', type: 'text', label: { en_US: 'Name', }, required: true, }, { variable: 'endpoint', type: 'text', label: { en_US: 'API Endpoint', }, required: true, }, { variable: 'api_key', type: 'secret', label: { en_US: 'API Key', }, required: true, }, ] const AddExternalAPIModal: FC = ({ data, onSave, onCancel, datasetBindings, isEditMode, onEdit }) => { const { t } = useTranslation() const { notify } = useToastContext() const [loading, setLoading] = useState(false) const [showConfirm, setShowConfirm] = useState(false) const [formData, setFormData] = useState({ name: '', settings: { endpoint: '', api_key: '' } }) useEffect(() => { if (isEditMode && data) setFormData(data) }, [isEditMode, data]) const hasEmptyInputs = Object.values(formData).some(value => typeof value === 'string' ? value.trim() === '' : Object.values(value).some(v => v.trim() === ''), ) const handleDataChange = (val: CreateExternalAPIReq) => { setFormData(val) } const handleSave = async () => { if (formData && formData.settings.api_key && formData.settings.api_key?.length < 5) { notify({ type: 'error', message: t('common.apiBasedExtension.modal.apiKey.lengthError') }) setLoading(false) return } try { setLoading(true) if (isEditMode && onEdit) { await onEdit( { ...formData, settings: { ...formData.settings, api_key: formData.settings.api_key ? '[__HIDDEN__]' : formData.settings.api_key }, }, ) notify({ type: 'success', message: 'External API updated successfully' }) } else { const res = await createExternalAPI({ body: formData }) if (res && res.id) { notify({ type: 'success', message: 'External API saved successfully' }) onSave(res) } } onCancel() } catch (error) { console.error('Error saving/updating external API:', error) notify({ type: 'error', message: 'Failed to save/update External API' }) } finally { setLoading(false) } } return (
{ isEditMode ? t('dataset.editExternalAPIFormTitle') : t('dataset.createExternalAPI') }
{isEditMode && (datasetBindings?.length ?? 0) > 0 && (
{t('dataset.editExternalAPIFormWarning.front')}  {datasetBindings?.length} {t('dataset.editExternalAPIFormWarning.end')} 
{`${datasetBindings?.length} ${t('dataset.editExternalAPITooltipTitle')}`}
{datasetBindings?.map(binding => (
{binding.name}
))}
} asChild={false} position='bottom' >
)}
{t('dataset.externalAPIForm.encrypted.front')} PKCS1_OAEP {t('dataset.externalAPIForm.encrypted.end')}
{showConfirm && (datasetBindings?.length ?? 0) > 0 && ( setShowConfirm(false)} onConfirm={handleSave} /> )}
) } export default memo(AddExternalAPIModal)