这些界面改动和专家服务草稿停在工作区,没有随上一笔主分支提交进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
169 lines
5.7 KiB
TypeScript
169 lines
5.7 KiB
TypeScript
import type {
|
|
ApiBasedExtensionPayload,
|
|
ApiBasedExtensionResponse,
|
|
} from '@dify/contracts/api/console/api-based-extension/types.gen'
|
|
import { Button } from '@langgenius/dify-ui/button'
|
|
import {
|
|
Dialog,
|
|
DialogBody,
|
|
DialogContent,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogHeaderCloseButton,
|
|
DialogTitle,
|
|
} from '@langgenius/dify-ui/dialog'
|
|
import { Field, FieldError, FieldLabel } from '@langgenius/dify-ui/field'
|
|
import { Form } from '@langgenius/dify-ui/form'
|
|
import { Input } from '@langgenius/dify-ui/input'
|
|
import { useMutation } from '@tanstack/react-query'
|
|
import { useTranslation } from 'react-i18next'
|
|
import { consoleQuery } from '@/service/console'
|
|
|
|
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(['common'])
|
|
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: () => {
|
|
onSaved()
|
|
},
|
|
},
|
|
)
|
|
return
|
|
}
|
|
|
|
createApiBasedExtensionMutation.mutate(
|
|
{
|
|
body,
|
|
},
|
|
{
|
|
onSuccess: onSaved,
|
|
},
|
|
)
|
|
}
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange} disablePointerDismissal>
|
|
<DialogContent backdropProps={{ forceRender: true }} className="w-160">
|
|
<DialogHeader>
|
|
<DialogTitle>
|
|
{mode === 'edit'
|
|
? t(($) => $['apiBasedExtension.modal.editTitle'], { ns: 'common' })
|
|
: t(($) => $['apiBasedExtension.modal.title'], { ns: 'common' })}
|
|
</DialogTitle>
|
|
<DialogHeaderCloseButton aria-label={t(($) => $['operation.close'], { ns: 'common' })} />
|
|
</DialogHeader>
|
|
<Form<ApiBasedExtensionPayload> className="contents" onFormSubmit={handleSubmit}>
|
|
<DialogBody className="grid gap-4">
|
|
<Field name="name">
|
|
<FieldLabel>{nameLabel}</FieldLabel>
|
|
<Input
|
|
required
|
|
defaultValue={editingApiBasedExtension?.name || ''}
|
|
placeholder={t(($) => $['apiBasedExtension.modal.name.placeholder'], {
|
|
ns: 'common',
|
|
})}
|
|
/>
|
|
<FieldError match="valueMissing">
|
|
{t(($) => $['errorMsg.fieldRequired'], { ns: 'common', field: nameLabel })}
|
|
</FieldError>
|
|
</Field>
|
|
|
|
<Field name="api_endpoint">
|
|
<FieldLabel>{apiEndpointLabel}</FieldLabel>
|
|
<Input
|
|
required
|
|
defaultValue={editingApiBasedExtension?.api_endpoint || ''}
|
|
placeholder={t(($) => $['apiBasedExtension.modal.apiEndpoint.placeholder'], {
|
|
ns: 'common',
|
|
})}
|
|
/>
|
|
<FieldError match="valueMissing">
|
|
{t(($) => $['errorMsg.fieldRequired'], { ns: 'common', field: apiEndpointLabel })}
|
|
</FieldError>
|
|
</Field>
|
|
|
|
<Field
|
|
name="api_key"
|
|
validate={(value) => {
|
|
if (typeof value === 'string' && value.length > 0 && value.length < 5)
|
|
return t(($) => $['apiBasedExtension.modal.apiKey.lengthError'], { ns: 'common' })
|
|
|
|
return null
|
|
}}
|
|
>
|
|
<FieldLabel>{apiKeyLabel}</FieldLabel>
|
|
<Input
|
|
required
|
|
defaultValue={editingApiBasedExtension?.api_key || ''}
|
|
placeholder={t(($) => $['apiBasedExtension.modal.apiKey.placeholder'], {
|
|
ns: 'common',
|
|
})}
|
|
/>
|
|
<FieldError match="valueMissing">
|
|
{t(($) => $['errorMsg.fieldRequired'], { ns: 'common', field: apiKeyLabel })}
|
|
</FieldError>
|
|
<FieldError match="customError" />
|
|
</Field>
|
|
</DialogBody>
|
|
<DialogFooter>
|
|
<Button type="button" onClick={() => onOpenChange(false)}>
|
|
{t(($) => $['operation.cancel'], { ns: 'common' })}
|
|
</Button>
|
|
<Button type="submit" variant="primary" disabled={isSaving}>
|
|
{t(($) => $['operation.save'], { ns: 'common' })}
|
|
</Button>
|
|
</DialogFooter>
|
|
</Form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|