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 pathStepDefinitionMenu.tsx
More file actions
167 lines (152 loc) · 5.6 KB
/
StepDefinitionMenu.tsx
File metadata and controls
167 lines (152 loc) · 5.6 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
import { reusable } from '@circleci/circleci-config-sdk';
import { Form, Formik } from 'formik';
import CommandIcon from '../../../icons/components/CommandIcon';
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 { commandSubtypes } from '../../containers/inspector/subtypes/CommandSubtypes';
import ParamListContainer from '../../containers/ParamListContainer';
import { SubTypeMenuPageProps } from '../SubTypeMenu';
import TabbedMenu from '../TabbedMenu';
export type StepDefinitionProps = DataModel & {
values?: Record<string, object>;
editing?: boolean;
index?: number;
getter?: (values: any) => Array<Record<string, unknown>>;
setter?: (values: any, value: any) => void;
} & SubTypeMenuPageProps<any>;
const defaultGetter = (values: any) => values.steps;
const defaultSetter = (values: any, value: any) => (values.steps = value);
const StepDefinitionMenu = (props: StepDefinitionProps) => {
const navigateBack = useStoreActions((actions) => actions.navigateBack);
const definitions = useStoreState((state) => state.definitions);
const subtype = props.subtype || props.values?.name;
const valueGetter = props.getter || defaultGetter;
const valueSetter = props.setter || defaultSetter;
const isName = typeof subtype === 'string';
const builtIn = isName && subtype in commandSubtypes;
const builtInSubtype = builtIn
? commandSubtypes[subtype as string]
: undefined;
let customCommand: reusable.ReusableCommand | undefined;
if (!builtIn) {
customCommand = isName
? Object.values(definitions.commands).find(
(command) => (command.value.name = subtype),
)?.value
: (subtype as reusable.ReusableCommand);
}
return (
<div className="h-full flex flex-col">
<header>
<BreadCrumbs />
<h1 className="ml-6 text-2xl py-2 font-bold">New Step</h1>
</header>
<Formik
initialValues={props.values || { parameters: undefined }}
enableReinitialize={true}
onSubmit={(step) => {
const name = builtIn ? subtype : customCommand?.name;
const parameters = builtIn ? step.parameters : step;
navigateBack({
toast: {
label: name ?? '',
content: 'saved',
status: 'success',
},
distance: 1,
applyValues: (values: any) => {
const newStep = {
[name as string]: parameters,
};
const value = valueGetter(values) || [];
if (!props.editing) {
valueSetter(values, [...value, newStep]);
} else {
if (props.index === undefined) {
console.error('Step index was undefined when editing step.');
return values;
}
values.steps[props.index] = newStep;
value[props.index] = newStep;
}
return values;
},
});
}}
>
{(formikProps) => (
<Form className="flex flex-col flex-1">
<TabbedMenu tabs={['PROPERTIES']}>
<div
className="p-6 overflow-y-auto"
style={{ height: 'calc(100vh - 200px)' }}
>
<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();
}}
disabled={props.editing}
>
<p className="font-bold">
{builtInSubtype
? builtInSubtype?.name
: customCommand?.name}
</p>
<p className="text-sm mt-1 leading-4 text-circle-gray-500">
{builtInSubtype
? builtInSubtype?.description
: customCommand?.description}
</p>
</button>
{builtInSubtype
? builtInSubtype?.fields
: customCommand?.parameters && (
<ParamListContainer
props={formikProps}
paramList={customCommand.parameters}
/>
)}
</div>
</TabbedMenu>
<span className="border-b border-circle-gray-300 mt-auto" />
<Footer>
<Button
variant="secondary"
type="button"
onClick={() => {
navigateBack({
distance: 1,
});
}}
>
Cancel
</Button>
<Button
variant="primary"
type="submit"
// disabled={!formikProps.dirty}
>
{props.editing ? 'Save Step' : 'Create Step'}
</Button>
</Footer>
</Form>
)}
</Formik>
</div>
);
};
const StepDefinitionMenuNav: NavigationComponent = {
Component: StepDefinitionMenu,
Label: (props: StepDefinitionProps) => {
return <p>{props.editing ? 'Edit' : 'New'} Step</p>;
},
Icon: (props: StepDefinitionProps) => {
return <CommandIcon className="w-6 h-8 py-2" />;
},
};
export { StepDefinitionMenu, StepDefinitionMenuNav };