|
| 1 | +import { BAIHuggingFaceRegistrySettingModalFragment$key } from '../../__generated__/BAIHuggingFaceRegistrySettingModalFragment.graphql'; |
| 2 | +import { toLocalId } from '../../helper'; |
| 3 | +import BAIFlex from '../BAIFlex'; |
| 4 | +import BAIModal, { BAIModalProps } from '../BAIModal'; |
| 5 | +import BAIUnmountAfterClose from '../BAIUnmountAfterClose'; |
| 6 | +import { EditOutlined } from '@ant-design/icons'; |
| 7 | +import { App, Button, Form, FormInstance, Input } from 'antd'; |
| 8 | +import { useState, useRef } from 'react'; |
| 9 | +import { useTranslation } from 'react-i18next'; |
| 10 | +import { graphql, useFragment, useMutation } from 'react-relay'; |
| 11 | + |
| 12 | +export type BAIHuggingFaceRegistrySettingModalFragmentKey = |
| 13 | + BAIHuggingFaceRegistrySettingModalFragment$key; |
| 14 | + |
| 15 | +export interface BAIHuggingFaceRegistrySettingModalProps extends BAIModalProps { |
| 16 | + huggingFaceRegistryFragment?: BAIHuggingFaceRegistrySettingModalFragmentKey; |
| 17 | + onRequestClose?: (success?: boolean) => void; |
| 18 | +} |
| 19 | + |
| 20 | +const BAIHuggingFaceRegistrySettingModal = ({ |
| 21 | + huggingFaceRegistryFragment, |
| 22 | + onRequestClose, |
| 23 | + ...baiModalProps |
| 24 | +}: BAIHuggingFaceRegistrySettingModalProps) => { |
| 25 | + 'use memo'; |
| 26 | + const { t } = useTranslation(); |
| 27 | + const { message, modal } = App.useApp(); |
| 28 | + const formRef = useRef<FormInstance>(null); |
| 29 | + |
| 30 | + const huggingFaceRegistry = useFragment( |
| 31 | + graphql` |
| 32 | + fragment BAIHuggingFaceRegistrySettingModalFragment on HuggingFaceRegistry { |
| 33 | + id |
| 34 | + token |
| 35 | + } |
| 36 | + `, |
| 37 | + huggingFaceRegistryFragment, |
| 38 | + ); |
| 39 | + |
| 40 | + const [updateHuggingFaceRegistry, isInflightUpdateHuggingFaceRegistry] = |
| 41 | + useMutation(graphql` |
| 42 | + mutation BAIHuggingFaceRegistrySettingModalMutation( |
| 43 | + $input: UpdateHuggingFaceRegistryInput! |
| 44 | + ) { |
| 45 | + updateHuggingfaceRegistry(input: $input) { |
| 46 | + huggingfaceRegistry { |
| 47 | + id |
| 48 | + token |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + `); |
| 53 | + |
| 54 | + const initialToken = huggingFaceRegistry?.token; |
| 55 | + const [isEditing, setIsEditing] = useState(false); |
| 56 | + |
| 57 | + const hasToken = initialToken && initialToken.length > 0; |
| 58 | + |
| 59 | + const executeUpdate = (values: any) => { |
| 60 | + updateHuggingFaceRegistry({ |
| 61 | + variables: { |
| 62 | + input: { |
| 63 | + id: toLocalId(huggingFaceRegistry!.id), |
| 64 | + token: values.token, |
| 65 | + }, |
| 66 | + }, |
| 67 | + onCompleted: (_res, errors) => { |
| 68 | + if (errors && errors.length > 0) { |
| 69 | + errors.forEach((err) => |
| 70 | + message.error( |
| 71 | + err.message ?? |
| 72 | + t('comp:HuggingFaceRegistrySettingModal.FailedToUpdateToken'), |
| 73 | + ), |
| 74 | + ); |
| 75 | + return; |
| 76 | + } |
| 77 | + message.success( |
| 78 | + t('comp:HuggingFaceRegistrySettingModal.TokenUpdatedSuccessfully'), |
| 79 | + ); |
| 80 | + onRequestClose?.(true); |
| 81 | + }, |
| 82 | + onError: (err) => { |
| 83 | + message.error( |
| 84 | + err.message ?? |
| 85 | + t('comp:HuggingFaceRegistrySettingModal.FailedToUpdateToken'), |
| 86 | + ); |
| 87 | + }, |
| 88 | + }); |
| 89 | + }; |
| 90 | + |
| 91 | + const handleOk = () => { |
| 92 | + if (!huggingFaceRegistry?.id) { |
| 93 | + message.error( |
| 94 | + t('comp:HuggingFaceRegistrySettingModal.HuggingFaceRegistryNotFound'), |
| 95 | + ); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + formRef.current |
| 100 | + ?.validateFields() |
| 101 | + .then((values) => { |
| 102 | + // Check if current token exists and is empty string, but new token is not empty |
| 103 | + |
| 104 | + if (hasToken && !values.token) { |
| 105 | + modal.confirm({ |
| 106 | + title: t('comp:HuggingFaceRegistrySettingModal.ResetTokenConfirm'), |
| 107 | + content: t( |
| 108 | + 'comp:HuggingFaceRegistrySettingModal.ResetTokenConfirmMessage', |
| 109 | + ), |
| 110 | + onOk() { |
| 111 | + executeUpdate(values); |
| 112 | + }, |
| 113 | + onCancel() { |
| 114 | + // Don't proceed with update |
| 115 | + }, |
| 116 | + okButtonProps: { danger: true }, |
| 117 | + okText: t('button.Reset'), |
| 118 | + }); |
| 119 | + } else { |
| 120 | + executeUpdate(values); |
| 121 | + } |
| 122 | + }) |
| 123 | + .catch(() => {}); |
| 124 | + }; |
| 125 | + |
| 126 | + return ( |
| 127 | + <BAIUnmountAfterClose> |
| 128 | + <BAIModal |
| 129 | + destroyOnHidden |
| 130 | + afterClose={() => setIsEditing(false)} |
| 131 | + title={t('comp:HuggingFaceRegistrySettingModal.HuggingFaceSettings')} |
| 132 | + centered |
| 133 | + okText={t('button.Save')} |
| 134 | + onOk={handleOk} |
| 135 | + onCancel={() => onRequestClose?.(false)} |
| 136 | + okButtonProps={{ |
| 137 | + loading: isInflightUpdateHuggingFaceRegistry, |
| 138 | + }} |
| 139 | + {...baiModalProps} |
| 140 | + > |
| 141 | + <Form ref={formRef} layout="vertical"> |
| 142 | + <Form.Item |
| 143 | + label={t('comp:HuggingFaceRegistrySettingModal.Token')} |
| 144 | + name="token" |
| 145 | + > |
| 146 | + {hasToken && !isEditing ? ( |
| 147 | + <BAIFlex gap={'xs'}> |
| 148 | + {/* For security, we display a masked token instead of initial value. */} |
| 149 | + <Input type="password" disabled value="••••••••••••••••" /> |
| 150 | + <Button |
| 151 | + icon={<EditOutlined />} |
| 152 | + onClick={() => { |
| 153 | + setIsEditing(true); |
| 154 | + }} |
| 155 | + type="text" |
| 156 | + /> |
| 157 | + </BAIFlex> |
| 158 | + ) : ( |
| 159 | + <Input.Password |
| 160 | + placeholder={t( |
| 161 | + 'comp:HuggingFaceRegistrySettingModal.EnterToken', |
| 162 | + )} |
| 163 | + autoFocus={isEditing} |
| 164 | + /> |
| 165 | + )} |
| 166 | + </Form.Item> |
| 167 | + </Form> |
| 168 | + </BAIModal> |
| 169 | + </BAIUnmountAfterClose> |
| 170 | + ); |
| 171 | +}; |
| 172 | + |
| 173 | +export default BAIHuggingFaceRegistrySettingModal; |
0 commit comments