Skip to content

Commit 60f4837

Browse files
authored
feat(FR-1636): add HuggingFace registry settings modal (#5546)
1 parent f8bffb5 commit 60f4837

46 files changed

Lines changed: 350 additions & 18 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/backend.ai-ui/src/components/BAILink.test.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import BAILink from './BAILink';
2-
import { fireEvent, render, screen } from '@testing-library/react';
2+
import { render, screen } from '@testing-library/react';
33
import userEvent from '@testing-library/user-event';
44
import React from 'react';
55
import { MemoryRouter } from 'react-router-dom';
@@ -244,11 +244,7 @@ describe('BAILink', () => {
244244
});
245245

246246
it('should have disabled state when type is disabled', () => {
247-
render(
248-
<BAILink type="disabled">
249-
Disabled Link
250-
</BAILink>,
251-
);
247+
render(<BAILink type="disabled">Disabled Link</BAILink>);
252248

253249
const link = screen.getByText('Disabled Link');
254250
// Ant Design adds ant-typography-disabled class for disabled Typography.Link
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import { BAIHuggingFaceRegistrySettingModalFragment$key } from '../../__generated__/BAIHuggingFaceRegistrySettingModalFragment.graphql';
2+
import { BAIHuggingFaceRegistrySettingModalMutation } from '../../__generated__/BAIHuggingFaceRegistrySettingModalMutation.graphql';
3+
import { toLocalId } from '../../helper';
4+
import BAIModal, { BAIModalProps } from '../BAIModal';
5+
import { App, Form, Input, FormInstance } from 'antd';
6+
import { useRef, useState } from 'react';
7+
import { useTranslation } from 'react-i18next';
8+
import { graphql, useFragment, useMutation } from 'react-relay';
9+
10+
export type BAIHuggingFaceRegistrySettingModalFragmentKey =
11+
BAIHuggingFaceRegistrySettingModalFragment$key;
12+
13+
export interface BAIHuggingFaceRegistrySettingModalProps extends BAIModalProps {
14+
huggingFaceRegistryFrgmt?: BAIHuggingFaceRegistrySettingModalFragmentKey | null;
15+
}
16+
17+
interface FormValues {
18+
token: string;
19+
}
20+
21+
const BAIHuggingFaceRegistrySettingModal: React.FC<
22+
BAIHuggingFaceRegistrySettingModalProps
23+
> = ({ huggingFaceRegistryFrgmt = null, onOk, ...modalProps }) => {
24+
'use memo';
25+
const { t } = useTranslation();
26+
const { message } = App.useApp();
27+
const formRef = useRef<FormInstance<FormValues>>(null);
28+
const [isEditing, setIsEditing] = useState(false);
29+
30+
const huggingFaceRegistry = useFragment(
31+
graphql`
32+
fragment BAIHuggingFaceRegistrySettingModalFragment on HuggingFaceRegistry {
33+
id
34+
token
35+
}
36+
`,
37+
huggingFaceRegistryFrgmt,
38+
);
39+
40+
const [commitUpdateRegistry, isInflightUpdate] =
41+
useMutation<BAIHuggingFaceRegistrySettingModalMutation>(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 hasExistingToken = !!huggingFaceRegistry?.token;
55+
56+
const handleOk = (e: React.MouseEvent<HTMLButtonElement>) => {
57+
formRef.current
58+
?.validateFields()
59+
.then((values) => {
60+
if (!huggingFaceRegistry) return;
61+
commitUpdateRegistry({
62+
variables: {
63+
input: {
64+
id: toLocalId(huggingFaceRegistry.id),
65+
token: values.token || null,
66+
},
67+
},
68+
onCompleted: () => {
69+
message.success(
70+
t(
71+
'comp:BAIHuggingFaceRegistrySettingModal.TokenUpdatedSuccessfully',
72+
),
73+
);
74+
setIsEditing(false);
75+
onOk?.(e);
76+
},
77+
onError: (error) => {
78+
message.error(error.message);
79+
},
80+
});
81+
})
82+
.catch(() => {
83+
// validation error
84+
});
85+
};
86+
87+
return (
88+
<BAIModal
89+
title={t('comp:BAIHuggingFaceRegistrySettingModal.HuggingFaceSettings')}
90+
destroyOnHidden
91+
confirmLoading={isInflightUpdate}
92+
onOk={handleOk}
93+
afterClose={() => {
94+
formRef.current?.resetFields();
95+
setIsEditing(false);
96+
}}
97+
{...modalProps}
98+
>
99+
<Form ref={formRef} layout="vertical" preserve={false}>
100+
<Form.Item
101+
name="token"
102+
label={t('comp:BAIHuggingFaceRegistrySettingModal.Token')}
103+
>
104+
{hasExistingToken && !isEditing ? (
105+
<Input.Password
106+
value="••••••••••••"
107+
disabled
108+
addonAfter={
109+
<a onClick={() => setIsEditing(true)}>
110+
{t('general.button.Edit')}
111+
</a>
112+
}
113+
/>
114+
) : (
115+
<Input.Password
116+
placeholder={t(
117+
'comp:BAIHuggingFaceRegistrySettingModal.EnterToken',
118+
)}
119+
autoFocus
120+
/>
121+
)}
122+
</Form.Item>
123+
</Form>
124+
</BAIModal>
125+
);
126+
};
127+
128+
export default BAIHuggingFaceRegistrySettingModal;

packages/backend.ai-ui/src/components/fragments/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,8 @@ export type {
8787
ProjectNode,
8888
BAIProjectSelectRef,
8989
} from './BAIProjectSelect';
90+
export { default as BAIHuggingFaceRegistrySettingModal } from './BAIHuggingFaceRegistrySettingModal';
91+
export type {
92+
BAIHuggingFaceRegistrySettingModalProps,
93+
BAIHuggingFaceRegistrySettingModalFragmentKey,
94+
} from './BAIHuggingFaceRegistrySettingModal';

packages/backend.ai-ui/src/locale/de.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@
106106
"NotIn": "nicht enthalten in"
107107
}
108108
},
109+
"comp:BAIHuggingFaceRegistrySettingModal": {
110+
"EnterToken": "Enter your HuggingFace token",
111+
"HuggingFaceSettings": "Hugging Face Settings",
112+
"Token": "Token",
113+
"TokenUpdatedSuccessfully": "Token updated successfully."
114+
},
109115
"comp:BAIImportArtifactModal": {
110116
"ExcludedVersions": "{{count}} Versionen sind ausgeschlossen.",
111117
"FailedToPullVersions": "Versäumte, Versionen zu ziehen.",
@@ -294,6 +300,7 @@
294300
"CopyAll": "Alle kopieren",
295301
"Create": "Erstellen",
296302
"Delete": "Löschen",
303+
"Edit": "Bearbeiten",
297304
"Expand": "Expandieren",
298305
"Remove": "Entfernen",
299306
"Save": "Speichern",

packages/backend.ai-ui/src/locale/el.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@
106106
"NotIn": "όχι σε"
107107
}
108108
},
109+
"comp:BAIHuggingFaceRegistrySettingModal": {
110+
"EnterToken": "Enter your HuggingFace token",
111+
"HuggingFaceSettings": "Hugging Face Settings",
112+
"Token": "Token",
113+
"TokenUpdatedSuccessfully": "Token updated successfully."
114+
},
109115
"comp:BAIImportArtifactModal": {
110116
"ExcludedVersions": "{{count}} Οι εκδόσεις εξαιρούνται.",
111117
"FailedToPullVersions": "Απέτυχε να τραβήξει εκδόσεις.",
@@ -294,6 +300,7 @@
294300
"CopyAll": "Αντιγράψτε όλα",
295301
"Create": "Δημιουργώ",
296302
"Delete": "Διαγράφω",
303+
"Edit": "Επεξεργασία",
297304
"Expand": "Διαστέλλω",
298305
"Remove": "Αφαιρώ",
299306
"Save": "Αποθήκευση",

packages/backend.ai-ui/src/locale/en.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@
106106
"NotIn": "not in"
107107
}
108108
},
109+
"comp:BAIHuggingFaceRegistrySettingModal": {
110+
"EnterToken": "Enter your HuggingFace token",
111+
"HuggingFaceSettings": "Hugging Face Settings",
112+
"Token": "Token",
113+
"TokenUpdatedSuccessfully": "Token updated successfully."
114+
},
109115
"comp:BAIImportArtifactModal": {
110116
"ExcludedVersions": "{{count}} versions are excluded.",
111117
"FailedToPullVersions": "Failed to pull versions.",
@@ -297,6 +303,7 @@
297303
"CopyAll": "Copy All",
298304
"Create": "Create",
299305
"Delete": "Delete",
306+
"Edit": "Edit",
300307
"Expand": "Expand",
301308
"Remove": "Remove",
302309
"Save": "Save",

packages/backend.ai-ui/src/locale/es.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@
106106
"NotIn": "no en"
107107
}
108108
},
109+
"comp:BAIHuggingFaceRegistrySettingModal": {
110+
"EnterToken": "Enter your HuggingFace token",
111+
"HuggingFaceSettings": "Hugging Face Settings",
112+
"Token": "Token",
113+
"TokenUpdatedSuccessfully": "Token updated successfully."
114+
},
109115
"comp:BAIImportArtifactModal": {
110116
"ExcludedVersions": "Las versiones {{count}} están excluidas.",
111117
"FailedToPullVersions": "No se pudo tirar de versiones.",
@@ -294,6 +300,7 @@
294300
"CopyAll": "Copiar todo",
295301
"Create": "Crear",
296302
"Delete": "Borrar",
303+
"Edit": "Editar",
297304
"Expand": "Expandir",
298305
"Remove": "Eliminar",
299306
"Save": "Guardar",

packages/backend.ai-ui/src/locale/fi.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@
106106
"NotIn": "ei sisällä"
107107
}
108108
},
109+
"comp:BAIHuggingFaceRegistrySettingModal": {
110+
"EnterToken": "Enter your HuggingFace token",
111+
"HuggingFaceSettings": "Hugging Face Settings",
112+
"Token": "Token",
113+
"TokenUpdatedSuccessfully": "Token updated successfully."
114+
},
109115
"comp:BAIImportArtifactModal": {
110116
"ExcludedVersions": "{{count}} versiot jätetään pois.",
111117
"FailedToPullVersions": "Versioiden vetäminen epäonnistui.",
@@ -294,6 +300,7 @@
294300
"CopyAll": "Kopioida kaikki",
295301
"Create": "Luoda",
296302
"Delete": "Poistaa",
303+
"Edit": "Muokkaa",
297304
"Expand": "Laajentaa",
298305
"Remove": "Poistaa",
299306
"Save": "Tallenna",

packages/backend.ai-ui/src/locale/fr.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@
106106
"NotIn": "pas dans"
107107
}
108108
},
109+
"comp:BAIHuggingFaceRegistrySettingModal": {
110+
"EnterToken": "Enter your HuggingFace token",
111+
"HuggingFaceSettings": "Hugging Face Settings",
112+
"Token": "Token",
113+
"TokenUpdatedSuccessfully": "Token updated successfully."
114+
},
109115
"comp:BAIImportArtifactModal": {
110116
"ExcludedVersions": "Les versions {{count}} sont exclues.",
111117
"FailedToPullVersions": "N'a pas réussi à tirer des versions.",
@@ -294,6 +300,7 @@
294300
"CopyAll": "Copier tout",
295301
"Create": "Créer",
296302
"Delete": "Supprimer",
303+
"Edit": "Modifier",
297304
"Expand": "Développer",
298305
"Remove": "Retirer",
299306
"Save": "Enregistrer",

packages/backend.ai-ui/src/locale/id.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@
106106
"NotIn": "tidak dalam"
107107
}
108108
},
109+
"comp:BAIHuggingFaceRegistrySettingModal": {
110+
"EnterToken": "Enter your HuggingFace token",
111+
"HuggingFaceSettings": "Hugging Face Settings",
112+
"Token": "Token",
113+
"TokenUpdatedSuccessfully": "Token updated successfully."
114+
},
109115
"comp:BAIImportArtifactModal": {
110116
"ExcludedVersions": "{{count}} versi dikecualikan.",
111117
"FailedToPullVersions": "Gagal menarik versi.",
@@ -294,6 +300,7 @@
294300
"CopyAll": "Salin semua",
295301
"Create": "Membuat",
296302
"Delete": "Menghapus",
303+
"Edit": "Edit",
297304
"Expand": "Memperluas",
298305
"Remove": "Menghapus",
299306
"Save": "Simpan",

0 commit comments

Comments
 (0)