This repository was archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathInspectorDefinitionMenu.tsx
More file actions
340 lines (311 loc) · 10.8 KB
/
InspectorDefinitionMenu.tsx
File metadata and controls
340 lines (311 loc) · 10.8 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import { Form, Formik } from 'formik';
import EditIcon from '../../../icons/ui/EditIcon';
import InspectableMapping from '../../../mappings/InspectableMapping';
import {
DefinitionsModel,
DefinitionSubscriptions,
} from '../../../state/DefinitionStore';
import { useStoreActions, useStoreState } from '../../../state/Hooks';
import { DataModel, NavigationComponent } from '../../../state/Store';
import { Button } from '../../atoms/Button';
import { Footer } from '../../atoms/Footer';
import BreadCrumbs from '../../containers/BreadCrumbs';
import ParameterContainer from '../../containers/ParametersContainer';
import { SubTypeMenuPageProps } from '../SubTypeMenu';
import TabbedMenu from '../TabbedMenu';
type InspectorDefinitionProps = DataModel & {
values: Record<string, object>;
dependencies: Array<DefinitionSubscriptions>;
editing?: boolean;
passBackKey?: string;
activeTab?: number;
index: number;
source?: Array<any>;
toast: boolean;
// The source of generable
// modifying this would not be good
readonly data?: any;
} & SubTypeMenuPageProps<any>;
const getDependencies = (
store: DefinitionsModel,
mapping: InspectableMapping,
name: string,
): number => {
const definition = store[mapping.key][name];
if (!definition || !definition.observers) {
return 0;
}
let count = 0;
Object.values(definition.observers).forEach((observer) => {
count += Object.values(observer).reduce((acc, value) => acc + value);
});
return count;
};
/**
* The menu that allows for inspection (creation and editing) of a definition
* This is one of the more complex pieces of the VCE, as it handles
* the abstraction of all components registered with a InspectableMapping,
* also handles parameterized components.
*
* The inspector menu opens from "New" button in the Definition
* container, or from the "Definition" atom, which edits an
* existing definition.
*
* Related:
* For Orb definitions see OrbDefinitionsMenu.tsx
* For Step definitions see StepDefinitionMenu.tsx
*/
const InspectorDefinitionMenu = (props: InspectorDefinitionProps) => {
const definitions = useStoreState((state) => state.definitions);
const navigateBack = useStoreActions((actions) => actions.navigateBack);
const setGuideStep = useStoreActions((actions) => actions.setGuideStep);
const guideStep = useStoreState((state) => state.guideStep);
const dataMapping = props.dataType;
const submitToStore = useStoreActions(
(actions) =>
(props.editing
? dataMapping?.store.update(actions)
: dataMapping?.store.add(actions)) || actions.error,
);
const deleteDefinition = useStoreActions(
(actions) => dataMapping?.store.remove(actions) || actions.error,
);
// TODO: useMemo for these functions?
const getIcon = (className: string) => {
let iconComponent = dataMapping?.components.icon;
if (iconComponent) {
let Icon = iconComponent;
return <Icon className={className} type={props.subtype} />;
}
};
const getValues = () => {
if (props.values) {
return props.values;
}
return props.subtype
? dataMapping?.defaults[props.subtype]
: dataMapping?.defaults;
};
const tabs = ['PROPERTIES'];
const unpacked = getValues();
const subtype = props.subtype || dataMapping?.subtypes?.getSubtype(unpacked);
if (dataMapping?.parameters) {
tabs.push('PARAMETERS');
}
const updateConfirmation = useStoreActions(
(actions) => actions.triggerConfirmation,
);
return (
<div
aria-label="Inspector Definition Menu"
className="h-full flex flex-col"
>
<header>
<BreadCrumbs />
<div className="ml-6 py-3 flex">
{getIcon('w-8 h-8 p-1 pl-0 mr-1')}
<h1 className="text-2xl font-bold">
{props.editing ? 'Edit' : 'New'} {dataMapping?.name.singular}
</h1>
</div>
</header>
{dataMapping && (
<Formik
initialValues={{
...unpacked,
}}
validateOnBlur
validate={(values) => {
// TODO: define error type
const errors: any = {};
const source = props.source ?? definitions[dataMapping.key];
const dupIndex = Object.values(source).findIndex(
(d) =>
(typeof d === 'string' ? d : d.value.name) ===
values.name.trim(),
);
if (dupIndex !== -1 && dupIndex !== props.index) {
errors.name = 'Name is already in use';
}
return errors;
}}
enableReinitialize
onSubmit={(values) => {
if (!props.passBackKey) {
const newDefinition = dataMapping.transform(values, definitions);
const submitData = props.editing
? { old: unpacked, new: newDefinition }
: newDefinition;
submitToStore(submitData);
}
if (
!props.editing &&
guideStep &&
dataMapping.guide?.step === guideStep
) {
setGuideStep(guideStep + 1);
}
navigateBack({
toast: {
label: values.name.trim(),
content: 'saved',
status: 'success',
},
distance: 1,
applyValues: (parentValues) => {
if (props.passBackKey) {
const { name, ...args } = values;
const nestedValues = {
...parentValues[props.passBackKey],
[name]: args,
};
if (
name !== props.values?.name &&
typeof props.values?.name === 'string'
) {
delete nestedValues[props.values.name];
}
return {
...parentValues,
[props.passBackKey]: nestedValues,
};
}
},
});
}}
>
{(formikProps) => (
<Form className="flex flex-col flex-1">
<TabbedMenu tabs={tabs} activeTab={props.activeTab || 0}>
<div
className="p-6 overflow-y-auto"
style={{ height: 'calc(100vh - 220px)' }}
>
{dataMapping.subtypes &&
(props.editing ? (
<div className="p-4 mb-4 w-full border-circle-gray-300 border hover:border-circle-black rounded text-left">
<p className="font-medium">
{dataMapping.subtypes.definitions[subtype]?.text}
</p>
<p className="text-sm mt-1 leading-4 text-circle-gray-500">
{
dataMapping.subtypes.definitions[subtype]
?.description
}
</p>
</div>
) : (
<button
className="p-4 mb-4 w-full border-circle-gray-300 border hover:border-circle-black rounded text-left"
type="button"
onClick={() => {
props.selectSubtype();
}}
>
<div className="w-full flex flex-row">
<p className="font-medium">
{dataMapping.subtypes.definitions[subtype]?.text}
</p>
<EditIcon className="ml-auto flex w-4" />
</div>
<p className="text-sm mt-1 leading-4 text-circle-gray-500">
{
dataMapping.subtypes.definitions[subtype]
?.description
}
</p>
</button>
))}
{dataMapping.components.inspector({
...formikProps,
definitions,
subtype,
data: props.data,
})}
</div>
{dataMapping.parameters && (
<div
style={{ height: 'calc(100vh - 220px)' }}
className="overflow-y-auto"
>
<ParameterContainer
dataMapping={dataMapping}
values={formikProps.values}
/>
</div>
)}
</TabbedMenu>
<Footer>
{props.editing && (
<Button
variant="dangerous"
type="button"
onClick={() => {
updateConfirmation({
modalDialogue: 'delete',
labels: [
dataMapping.name.singular,
props.data.name,
getDependencies(
definitions,
dataMapping,
props.data.name,
),
],
onConfirm: () => {
deleteDefinition(props.data);
navigateBack({
distance: 1,
});
},
});
}}
>
Delete
</Button>
)}
<Button
variant="secondary"
type="button"
onClick={() => {
navigateBack({
distance: 1,
});
}}
>
Cancel
</Button>
<Button
variant="primary"
type="submit"
// disabled={!formikProps.dirty}
>
{props.editing ? 'Save' : 'Create'}
</Button>
</Footer>
</Form>
)}
</Formik>
)}
</div>
);
};
const InspectorDefinitionMenuNav: NavigationComponent = {
Component: InspectorDefinitionMenu,
Label: (props: InspectorDefinitionProps) => {
return (
<p>
{props.editing ? 'Edit' : 'New'} {props.dataType?.name.singular}
</p>
);
},
Icon: (props: InspectorDefinitionProps) => {
let iconComponent = props.dataType?.components.icon;
if (!iconComponent) {
return null;
}
let DefinitionIcon = iconComponent;
return <DefinitionIcon className="w-6 h-8 py-2" />;
},
};
export { InspectorDefinitionMenuNav, InspectorDefinitionMenu };