@@ -6,6 +6,7 @@ import { Alert } from "components/Alert/Alert";
6
6
import { ErrorAlert } from "components/Alert/ErrorAlert" ;
7
7
import { Avatar } from "components/Avatar/Avatar" ;
8
8
import { Button } from "components/Button/Button" ;
9
+ import { SelectFilter } from "components/Filter/SelectFilter" ;
9
10
import {
10
11
FormFields ,
11
12
FormFooter ,
@@ -64,6 +65,7 @@ export interface CreateWorkspacePageViewProps {
64
65
hasAllRequiredExternalAuth : boolean ;
65
66
parameters : TypesGen . TemplateVersionParameter [ ] ;
66
67
autofillParameters : AutofillBuildParameter [ ] ;
68
+ presets : TypesGen . Preset [ ] ;
67
69
permissions : CreateWSPermissions ;
68
70
creatingWorkspace : boolean ;
69
71
onCancel : ( ) => void ;
@@ -88,6 +90,7 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
88
90
hasAllRequiredExternalAuth,
89
91
parameters,
90
92
autofillParameters,
93
+ presets = [ ] ,
91
94
permissions,
92
95
creatingWorkspace,
93
96
onSubmit,
@@ -145,6 +148,68 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
145
148
[ autofillParameters ] ,
146
149
) ;
147
150
151
+ const presetOptions = useMemo ( ( ) => {
152
+ return [
153
+ { label : "None" , value : "" } ,
154
+ ...presets . map ( ( preset ) => ( {
155
+ label : preset . Name ,
156
+ value : preset . ID ,
157
+ } ) ) ,
158
+ ] ;
159
+ } , [ presets ] ) ;
160
+
161
+ const [ selectedPresetIndex , setSelectedPresetIndex ] = useState ( 0 ) ;
162
+ const [ presetParameterNames , setPresetParameterNames ] = useState < string [ ] > (
163
+ [ ] ,
164
+ ) ;
165
+
166
+ useEffect ( ( ) => {
167
+ // TODO (sasswart): test case: what if immutable parameters are used in the preset?
168
+ // TODO (sasswart): test case: what if presets are defined for a template version with no params?
169
+ // TODO (sasswart): test case: what if a non active version is selected?
170
+ // TODO (sasswart): test case: what if a preset is selected that has no parameters?
171
+ // TODO (sasswart): what if we have preset params and autofill params on the same param?
172
+ // TODO (sasswart): test case: if we move from preset to no preset, do we reset the params?
173
+ // If so, how should it behave? Reset to initial value? reset to last set value?
174
+ // TODO (sasswart): test case: rich parameters
175
+
176
+ const selectedPresetOption = presetOptions [ selectedPresetIndex ] ;
177
+ let selectedPreset : TypesGen . Preset | undefined ;
178
+ for ( const preset of presets ) {
179
+ if ( preset . ID === selectedPresetOption . value ) {
180
+ selectedPreset = preset ;
181
+ break ;
182
+ }
183
+ }
184
+
185
+ if ( ! selectedPreset || ! selectedPreset . Parameters ) {
186
+ setPresetParameterNames ( [ ] ) ;
187
+ return ;
188
+ }
189
+
190
+ setPresetParameterNames ( selectedPreset . Parameters . map ( ( p ) => p . Name ) ) ;
191
+
192
+ for ( const presetParameter of selectedPreset . Parameters ) {
193
+ const parameterIndex = parameters . findIndex (
194
+ ( p ) => p . name === presetParameter . Name ,
195
+ ) ;
196
+ if ( parameterIndex === - 1 ) continue ;
197
+
198
+ const parameterField = `rich_parameter_values.${ parameterIndex } ` ;
199
+
200
+ form . setFieldValue ( parameterField , {
201
+ name : presetParameter . Name ,
202
+ value : presetParameter . Value ,
203
+ } ) ;
204
+ }
205
+ } , [
206
+ presetOptions ,
207
+ selectedPresetIndex ,
208
+ presets ,
209
+ parameters ,
210
+ form . setFieldValue ,
211
+ ] ) ;
212
+
148
213
return (
149
214
< Margins size = "medium" >
150
215
< PageHeader
@@ -189,6 +254,31 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
189
254
</ Alert >
190
255
) }
191
256
257
+ { presets . length > 0 && (
258
+ < FormSection
259
+ title = "Presets"
260
+ description = "A list of preset workspace configurations to get you started."
261
+ >
262
+ < FormFields >
263
+ < Stack direction = "row" spacing = { 2 } >
264
+ < SelectFilter
265
+ label = "Preset"
266
+ options = { presetOptions }
267
+ onSelect = { ( option ) => {
268
+ setSelectedPresetIndex (
269
+ presetOptions . findIndex (
270
+ ( preset ) => preset . value === option ?. value ,
271
+ ) ,
272
+ ) ;
273
+ } }
274
+ placeholder = "Select a preset"
275
+ selectedOption = { presetOptions [ selectedPresetIndex ] }
276
+ />
277
+ </ Stack >
278
+ </ FormFields >
279
+ </ FormSection >
280
+ ) }
281
+
192
282
{ /* General info */ }
193
283
< FormSection
194
284
title = "General"
@@ -292,7 +382,9 @@ export const CreateWorkspacePageView: FC<CreateWorkspacePageViewProps> = ({
292
382
const isDisabled =
293
383
disabledParams ?. includes (
294
384
parameter . name . toLowerCase ( ) . replace ( / / g, "_" ) ,
295
- ) || creatingWorkspace ;
385
+ ) ||
386
+ creatingWorkspace ||
387
+ presetParameterNames . includes ( parameter . name ) ;
296
388
297
389
return (
298
390
< RichParameterInput
0 commit comments