import type { ApiBasedExtensionPayload, ApiBasedExtensionResponse, } from '@dify/contracts/api/console/api-based-extension/types.gen' import { Button } from '@langgenius/dify-ui/button' import { Dialog, DialogCloseButton, DialogContent, DialogTitle } from '@langgenius/dify-ui/dialog' import { Field, FieldControl, FieldDescription, FieldError, FieldLabel, } from '@langgenius/dify-ui/field' import { Form } from '@langgenius/dify-ui/form' import { toast } from '@langgenius/dify-ui/toast' import { useMutation } from '@tanstack/react-query' import { useTranslation } from 'react-i18next' import { useDocLink } from '@/context/i18n' import { consoleQuery } from '@/service/client' type ApiBasedExtensionModalProps = { open: boolean onOpenChange: (open: boolean) => void onSaved: () => void } & ( | { mode: 'create' } | { mode: 'edit' apiBasedExtension: ApiBasedExtensionResponse } ) export function ApiBasedExtensionModal(props: ApiBasedExtensionModalProps) { const { open, mode, onOpenChange, onSaved } = props const { t } = useTranslation() const docLink = useDocLink() const createApiBasedExtensionMutation = useMutation( consoleQuery.apiBasedExtension.post.mutationOptions(), ) const updateApiBasedExtensionMutation = useMutation( consoleQuery.apiBasedExtension.byId.post.mutationOptions(), ) const editingApiBasedExtension = mode === 'edit' ? props.apiBasedExtension : null const isSaving = createApiBasedExtensionMutation.isPending || updateApiBasedExtensionMutation.isPending const nameLabel = t(($) => $['apiBasedExtension.modal.name.title'], { ns: 'common' }) const apiEndpointLabel = t(($) => $['apiBasedExtension.modal.apiEndpoint.title'], { ns: 'common', }) const apiKeyLabel = t(($) => $['apiBasedExtension.modal.apiKey.title'], { ns: 'common' }) const handleSubmit = (formValues: ApiBasedExtensionPayload) => { const body: ApiBasedExtensionPayload = { name: formValues.name, api_endpoint: formValues.api_endpoint, api_key: formValues.api_key, } if (editingApiBasedExtension) { updateApiBasedExtensionMutation.mutate( { params: { id: editingApiBasedExtension.id, }, body: { ...body, api_key: editingApiBasedExtension.api_key === body.api_key ? '[__HIDDEN__]' : body.api_key, }, }, { onSuccess: () => { toast.success(t(($) => $['actionMsg.modifiedSuccessfully'], { ns: 'common' })) onSaved() }, }, ) return } createApiBasedExtensionMutation.mutate( { body, }, { onSuccess: onSaved, }, ) } return ( {mode === 'edit' ? t(($) => $['apiBasedExtension.modal.editTitle'], { ns: 'common' }) : t(($) => $['apiBasedExtension.modal.title'], { ns: 'common' })} className="grid gap-4 pt-2" onFormSubmit={handleSubmit}> {nameLabel} $['apiBasedExtension.modal.name.placeholder'], { ns: 'common' }) || '' } /> {t(($) => $['errorMsg.fieldRequired'], { ns: 'common', field: nameLabel })} {apiEndpointLabel} $['apiBasedExtension.modal.apiEndpoint.placeholder'], { ns: 'common' }) || '' } /> {t(($) => $['errorMsg.fieldRequired'], { ns: 'common', field: apiEndpointLabel })} { if (typeof value === 'string' && value.length > 0 && value.length < 5) return t(($) => $['apiBasedExtension.modal.apiKey.lengthError'], { ns: 'common' }) return null }} > {apiKeyLabel} $['apiBasedExtension.modal.apiKey.placeholder'], { ns: 'common' }) || '' } /> {t(($) => $['errorMsg.fieldRequired'], { ns: 'common', field: apiKeyLabel })}
) }