import type { FC } from 'react' import { useState } from 'react' import { useTranslation } from 'react-i18next' import Modal from '@/app/components/base/modal' import Button from '@/app/components/base/button' import { BookOpen01 } from '@/app/components/base/icons/src/vender/line/education' import type { ApiBasedExtension } from '@/models/common' import { addApiBasedExtension, updateApiBasedExtension, } from '@/service/common' import { useToastContext } from '@/app/components/base/toast' export type ApiBasedExtensionData = { name?: string apiEndpoint?: string apiKey?: string } type ApiBasedExtensionModalProps = { data: ApiBasedExtension onCancel: () => void onSave?: (newData: ApiBasedExtension) => void } const ApiBasedExtensionModal: FC = ({ data, onCancel, onSave, }) => { const { t } = useTranslation() const [localeData, setLocaleData] = useState(data) const [loading, setLoading] = useState(false) const { notify } = useToastContext() const handleDataChange = (type: string, value: string) => { setLocaleData({ ...localeData, [type]: value }) } const handleSave = async () => { setLoading(true) if (localeData && localeData.api_key && localeData.api_key?.length < 5) { notify({ type: 'error', message: t('common.apiBasedExtension.modal.apiKey.lengthError') }) setLoading(false) return } try { let res: ApiBasedExtension = {} if (!data.id) { res = await addApiBasedExtension({ url: '/api-based-extension', body: localeData, }) } else { res = await updateApiBasedExtension({ url: `/api-based-extension/${data.id}`, body: { ...localeData, api_key: data.api_key === localeData.api_key ? '[__HIDDEN__]' : localeData.api_key, }, }) notify({ type: 'success', message: t('common.actionMsg.modifiedSuccessfully') }) } if (onSave) onSave(res) } finally { setLoading(false) } } return ( { }} className='!p-8 !pb-6 !max-w-none !w-[640px]' >
{ data.name ? t('common.apiBasedExtension.modal.editTitle') : t('common.apiBasedExtension.modal.title') }
{t('common.apiBasedExtension.modal.name.title')}
handleDataChange('name', e.target.value)} className='block px-3 w-full h-9 bg-gray-100 rounded-lg text-sm text-gray-900 outline-none appearance-none' placeholder={t('common.apiBasedExtension.modal.name.placeholder') || ''} />
{t('common.apiBasedExtension.modal.apiEndpoint.title')} {t('common.apiBasedExtension.link')}
handleDataChange('api_endpoint', e.target.value)} className='block px-3 w-full h-9 bg-gray-100 rounded-lg text-sm text-gray-900 outline-none appearance-none' placeholder={t('common.apiBasedExtension.modal.apiEndpoint.placeholder') || ''} />
{t('common.apiBasedExtension.modal.apiKey.title')}
handleDataChange('api_key', e.target.value)} className='block grow mr-2 px-3 h-9 bg-gray-100 rounded-lg text-sm text-gray-900 outline-none appearance-none' placeholder={t('common.apiBasedExtension.modal.apiKey.placeholder') || ''} />
) } export default ApiBasedExtensionModal