|
| 1 | +/** |
| 2 | + @license |
| 3 | + Copyright (c) 2015-2026 Lablup Inc. All rights reserved. |
| 4 | + */ |
| 5 | +import { DeploymentCreateModalMutation } from '../__generated__/DeploymentCreateModalMutation.graphql'; |
| 6 | +import { useCurrentDomainValue, useWebUINavigate } from '../hooks'; |
| 7 | +import { useCurrentProjectValue } from '../hooks/useCurrentProject'; |
| 8 | +import { App, Button, Checkbox, Form, Input, InputNumber, Select } from 'antd'; |
| 9 | +import { BAIButton, BAIFlex, BAIModal, toLocalId } from 'backend.ai-ui'; |
| 10 | +import React from 'react'; |
| 11 | +import { useTranslation } from 'react-i18next'; |
| 12 | +import { graphql, useMutation } from 'react-relay'; |
| 13 | + |
| 14 | +interface FormValues { |
| 15 | + name: string; |
| 16 | + tags: string[]; |
| 17 | + openToPublic: boolean; |
| 18 | + replicaCount: number; |
| 19 | +} |
| 20 | + |
| 21 | +interface DeploymentCreateModalProps { |
| 22 | + open: boolean; |
| 23 | + onClose: () => void; |
| 24 | +} |
| 25 | + |
| 26 | +const DeploymentCreateModal: React.FC<DeploymentCreateModalProps> = ({ |
| 27 | + open, |
| 28 | + onClose, |
| 29 | +}) => { |
| 30 | + 'use memo'; |
| 31 | + const { t } = useTranslation(); |
| 32 | + const [form] = Form.useForm<FormValues>(); |
| 33 | + const navigate = useWebUINavigate(); |
| 34 | + const { message } = App.useApp(); |
| 35 | + const { id: projectId } = useCurrentProjectValue(); |
| 36 | + const currentDomain = useCurrentDomainValue(); |
| 37 | + |
| 38 | + const [commitCreate, isCreating] = useMutation<DeploymentCreateModalMutation>( |
| 39 | + graphql` |
| 40 | + mutation DeploymentCreateModalMutation($input: CreateDeploymentInput!) { |
| 41 | + createModelDeployment(input: $input) { |
| 42 | + deployment { |
| 43 | + id |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + `, |
| 48 | + ); |
| 49 | + |
| 50 | + const handleFinish = (values: FormValues) => { |
| 51 | + if (!projectId) { |
| 52 | + message.error(t('general.ErrorOccurred')); |
| 53 | + return; |
| 54 | + } |
| 55 | + commitCreate({ |
| 56 | + variables: { |
| 57 | + input: { |
| 58 | + metadata: { |
| 59 | + projectId, |
| 60 | + domainName: currentDomain, |
| 61 | + name: values.name, |
| 62 | + tags: values.tags?.length ? values.tags : null, |
| 63 | + }, |
| 64 | + networkAccess: { |
| 65 | + // TODO: expose preferredDomainName once backend business logic is in place |
| 66 | + preferredDomainName: null, |
| 67 | + openToPublic: values.openToPublic, |
| 68 | + }, |
| 69 | + // TODO: expose strategy type selection once BLUE_GREEN is supported server-side |
| 70 | + defaultDeploymentStrategy: { type: 'ROLLING' }, |
| 71 | + replicaCount: values.replicaCount, |
| 72 | + initialRevision: null, |
| 73 | + }, |
| 74 | + }, |
| 75 | + onCompleted: (response, errors) => { |
| 76 | + if (errors && errors.length > 0) { |
| 77 | + message.error( |
| 78 | + errors.map((e) => e.message).join('\n') || |
| 79 | + t('deployment.FailedToCreateDeployment'), |
| 80 | + ); |
| 81 | + return; |
| 82 | + } |
| 83 | + const newId = toLocalId(response.createModelDeployment.deployment.id); |
| 84 | + form.resetFields(); |
| 85 | + onClose(); |
| 86 | + navigate(`/deployments/${newId}`); |
| 87 | + }, |
| 88 | + onError: (err) => { |
| 89 | + message.error(err.message ?? t('deployment.FailedToCreateDeployment')); |
| 90 | + }, |
| 91 | + }); |
| 92 | + }; |
| 93 | + |
| 94 | + const handleCancel = () => { |
| 95 | + form.resetFields(); |
| 96 | + onClose(); |
| 97 | + }; |
| 98 | + |
| 99 | + return ( |
| 100 | + <BAIModal |
| 101 | + open={open} |
| 102 | + title={t('deployment.CreateDeployment')} |
| 103 | + onCancel={handleCancel} |
| 104 | + width={520} |
| 105 | + footer={ |
| 106 | + <BAIFlex justify="end" gap="xs"> |
| 107 | + <Button onClick={handleCancel}>{t('button.Cancel')}</Button> |
| 108 | + <BAIButton |
| 109 | + type="primary" |
| 110 | + loading={isCreating} |
| 111 | + onClick={() => form.submit()} |
| 112 | + > |
| 113 | + {t('button.Create')} |
| 114 | + </BAIButton> |
| 115 | + </BAIFlex> |
| 116 | + } |
| 117 | + > |
| 118 | + <Form<FormValues> |
| 119 | + form={form} |
| 120 | + layout="vertical" |
| 121 | + onFinish={handleFinish} |
| 122 | + initialValues={{ openToPublic: false, replicaCount: 1, tags: [] }} |
| 123 | + style={{ marginTop: 8 }} |
| 124 | + > |
| 125 | + <Form.Item |
| 126 | + name="name" |
| 127 | + label={t('deployment.DeploymentName')} |
| 128 | + rules={[{ required: true, message: t('deployment.NameRequired') }]} |
| 129 | + > |
| 130 | + <Input placeholder={t('deployment.NamePlaceholder')} /> |
| 131 | + </Form.Item> |
| 132 | + <Form.Item name="tags" label={t('deployment.Tags')}> |
| 133 | + <Select |
| 134 | + mode="tags" |
| 135 | + placeholder={t('deployment.TagsPlaceholder')} |
| 136 | + tokenSeparators={[',', '\n']} |
| 137 | + open={false} |
| 138 | + /> |
| 139 | + </Form.Item> |
| 140 | + <Form.Item |
| 141 | + name="replicaCount" |
| 142 | + label={t('deployment.DesiredReplicas')} |
| 143 | + rules={[{ required: true }]} |
| 144 | + > |
| 145 | + <InputNumber min={1} style={{ width: '100%' }} /> |
| 146 | + </Form.Item> |
| 147 | + <Form.Item name="openToPublic" valuePropName="checked" required> |
| 148 | + <Checkbox>{t('deployment.OpenToPublic')}</Checkbox> |
| 149 | + </Form.Item> |
| 150 | + </Form> |
| 151 | + </BAIModal> |
| 152 | + ); |
| 153 | +}; |
| 154 | + |
| 155 | +export default DeploymentCreateModal; |
0 commit comments