-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathCreateDomainModal.tsx
More file actions
286 lines (270 loc) · 12.2 KB
/
Copy pathCreateDomainModal.tsx
File metadata and controls
286 lines (270 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// antd `Form` and `Collapse` are retained because alchemy does not currently provide
// equivalents for `Form` (with field-level rules / Form.useForm) or collapsible panels.
import { toast } from '@components';
import { Collapse, Form } from 'antd';
import React, { useCallback, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import styled, { useTheme } from 'styled-components';
import { Label } from '@components/components/TextArea/components';
import analytics, { EventType } from '@app/analytics';
import { useUserContext } from '@app/context/useUserContext';
import { UpdatedDomain, useDomainsContext as useDomainsContextV2 } from '@app/domainV2/DomainsContext';
import OwnersSection from '@app/domainV2/OwnersSection';
import DomainSelector from '@app/entityV2/shared/DomainSelector/DomainSelector';
import { createOwnerInputs } from '@app/entityV2/shared/utils/selectorUtils';
import { validateCustomUrnId } from '@app/shared/textUtil';
import { useEnterKeyListener } from '@app/shared/useEnterKeyListener';
import { useReloadableContext } from '@app/sharedV2/reloadableContext/hooks/useReloadableContext';
import { ReloadableKeyTypeNamespace } from '@app/sharedV2/reloadableContext/types';
import { getReloadableKeyType } from '@app/sharedV2/reloadableContext/utils';
import { useIsNestedDomainsEnabled } from '@app/useAppConfig';
import { ColorPicker, Input, Modal, TextArea } from '@src/alchemy-components';
import { useCreateDomainMutation } from '@graphql/domain.generated';
import { useUpdateDisplayPropertiesMutation } from '@graphql/mutations.generated';
import { DataHubPageModuleType, EntityType } from '@types';
const FormItem = styled(Form.Item)`
.ant-form-item-label {
padding-bottom: 2px;
}
`;
const FormItemWithMargin = styled(FormItem)`
margin-bottom: 16px;
`;
const FormItemNoMargin = styled(FormItem)`
margin-bottom: 0px;
`;
type Props = {
onClose: () => void;
onCreate?: (
urn: string,
id: string | undefined,
name: string,
description: string | undefined,
parentDomain?: string,
) => void;
};
const ID_FIELD_NAME = 'id';
const NAME_FIELD_NAME = 'name';
const DESCRIPTION_FIELD_NAME = 'description';
export default function CreateDomainModal({ onClose, onCreate }: Props) {
const { t } = useTranslation('governance.domain');
const { t: tc } = useTranslation('common.actions');
const { t: tl } = useTranslation('common.labels');
const isNestedDomainsEnabled = useIsNestedDomainsEnabled();
const [createDomainMutation] = useCreateDomainMutation();
const [updateDisplayPropertiesMutation] = useUpdateDisplayPropertiesMutation();
const { entityData, setNewDomain } = useDomainsContextV2();
const theme = useTheme();
const [selectedParentUrn, setSelectedParentUrn] = useState<string>(
(isNestedDomainsEnabled && entityData?.urn) || '',
);
const [createButtonEnabled, setCreateButtonEnabled] = useState(false);
const [selectedColor, setSelectedColor] = useState<string>(theme.colors.colorPickerDefault);
// Whether the user has explicitly picked a color. If false, we let the backend fall back to
// the deterministic palette color generated from the URN instead of persisting the default
// gray placeholder and overriding it.
const [colorWasPicked, setColorWasPicked] = useState(false);
const [form] = Form.useForm();
const { loaded: userLoaded, user } = useUserContext();
const [selectedOwnerUrns, setSelectedOwnerUrns] = useState<string[]>([]);
const [hasInitializedDefaultOwner, setHasInitializedDefaultOwner] = useState(false);
useEffect(() => {
if (!hasInitializedDefaultOwner && userLoaded) {
setSelectedOwnerUrns(user?.urn ? [user.urn] : []);
setHasInitializedDefaultOwner(true);
}
}, [hasInitializedDefaultOwner, user?.urn, userLoaded]);
// Stable callback for setting owner URNs
const handleSetSelectedOwnerUrns = useCallback((ownerUrns: string[]) => {
setSelectedOwnerUrns(ownerUrns);
}, []);
const { reloadByKeyType } = useReloadableContext();
const onCreateDomain = () => {
// Create owner input objects from selected owner URNs using utility
const ownerInputs = createOwnerInputs(selectedOwnerUrns);
createDomainMutation({
variables: {
input: {
id: form.getFieldValue(ID_FIELD_NAME),
name: form.getFieldValue(NAME_FIELD_NAME),
description: form.getFieldValue(DESCRIPTION_FIELD_NAME),
parentDomain: selectedParentUrn || undefined,
owners: ownerInputs,
},
},
})
.then(({ data, errors }) => {
if (!errors) {
analytics.event({
type: EventType.CreateDomainEvent,
parentDomainUrn: selectedParentUrn || undefined,
});
toast.success(t('create.success'), { duration: 3 });
const newDomainUrn = data?.createDomain || '';
// Only persist the color if the user actually picked one. Otherwise we'd
// save the gray placeholder default and override the deterministic palette
// color the UI would have generated from the URN. Best-effort follow-up so
// a color failure doesn't block creation.
if (newDomainUrn && colorWasPicked) {
updateDisplayPropertiesMutation({
variables: {
urn: newDomainUrn,
input: { colorHex: selectedColor },
},
}).catch((e) => {
console.error('Failed to set domain color after creation', e);
});
}
onCreate?.(
newDomainUrn,
form.getFieldValue(ID_FIELD_NAME),
form.getFieldValue(NAME_FIELD_NAME),
form.getFieldValue(DESCRIPTION_FIELD_NAME),
selectedParentUrn || undefined,
);
const newDomain: UpdatedDomain = {
urn: newDomainUrn,
type: EntityType.Domain,
id: form.getFieldValue(ID_FIELD_NAME),
properties: {
name: form.getFieldValue(NAME_FIELD_NAME),
description: form.getFieldValue(DESCRIPTION_FIELD_NAME),
},
parentDomain: selectedParentUrn || undefined,
};
setNewDomain(newDomain);
form.resetFields();
// Reload modules
// ChildHierarchy - to reload shown child domains on asset summary tab
reloadByKeyType(
[getReloadableKeyType(ReloadableKeyTypeNamespace.MODULE, DataHubPageModuleType.ChildHierarchy)],
3000,
);
}
})
.catch((e) => {
toast.error(t('create.error', { errorMessage: e.message || '' }), { duration: 3 });
})
.finally(() => {
onClose();
});
};
// Handle the Enter press
useEnterKeyListener({
querySelectorToExecuteClick: '#createDomainButton',
});
return (
<Modal
title={t('create.title')}
open
onCancel={onClose}
buttons={[
{
text: tc('cancel'),
variant: 'text',
onClick: onClose,
},
{
text: tc('save'),
id: 'createDomainButton',
buttonDataTestId: 'create-domain-button',
onClick: onCreateDomain,
disabled: !createButtonEnabled || !hasInitializedDefaultOwner,
},
]}
>
<Form
form={form}
initialValues={{}}
layout="vertical"
onFieldsChange={() => {
setCreateButtonEnabled(!form.getFieldsError().some((field) => field.errors.length > 0));
}}
>
<FormItemWithMargin
name={NAME_FIELD_NAME}
rules={[
{
required: true,
message: t('create.nameRequired'),
},
{ whitespace: true },
{ min: 1, max: 150 },
]}
hasFeedback
>
<Input
label={tl('name')}
data-testid="create-domain-name"
placeholder={t('create.namePlaceholder')}
/>
</FormItemWithMargin>
<FormItemWithMargin
name={DESCRIPTION_FIELD_NAME}
rules={[{ whitespace: true }, { min: 1, max: 500 }]}
hasFeedback
>
<TextArea
label={tl('description')}
placeholder={t('create.descriptionPlaceholder')}
data-testid="create-domain-description"
/>
</FormItemWithMargin>
<FormItemWithMargin>
<Label>{tl('color')}</Label>
<ColorPicker
initialColor={selectedColor}
onChange={(c) => {
setSelectedColor(c);
setColorWasPicked(true);
}}
/>
</FormItemWithMargin>
{isNestedDomainsEnabled && (
<FormItemWithMargin>
<Label>{t('create.parentLabel')}</Label>
<DomainSelector
selectedDomains={selectedParentUrn ? [selectedParentUrn] : []}
onDomainsChange={(selectedDomainUrns) => setSelectedParentUrn(selectedDomainUrns[0] || '')}
placeholder={t('create.parentPlaceholder')}
label=""
isMultiSelect={false}
/>
</FormItemWithMargin>
)}
{/* Owners Section */}
<FormItemNoMargin>
<OwnersSection
selectedOwnerUrns={selectedOwnerUrns}
setSelectedOwnerUrns={handleSetSelectedOwnerUrns}
isDisabled={!hasInitializedDefaultOwner}
isLoading={!hasInitializedDefaultOwner}
/>
</FormItemNoMargin>
<Collapse ghost>
<Collapse.Panel header={<Label>{t('create.advancedOptions')}</Label>} key="1">
<FormItemWithMargin
name={ID_FIELD_NAME}
rules={[
() => ({
validator(_, value) {
if (value && validateCustomUrnId(value)) {
return Promise.resolve();
}
return Promise.reject(new Error(t('create.idInvalid')));
},
}),
]}
>
<Input
label={t('create.idLabel')}
data-testid="create-domain-id"
placeholder={t('create.idPlaceholder')}
/>
</FormItemWithMargin>
</Collapse.Panel>
</Collapse>
</Form>
</Modal>
);
}