@@ -32,6 +32,8 @@ import { timeZones } from "utils/timeZones";
32
32
import Tooltip from "@mui/material/Tooltip" ;
33
33
import { formatDuration , intervalToDuration } from "date-fns" ;
34
34
import { DisabledBadge } from "components/Badges/Badges" ;
35
+ import { TemplateAutostartRequirement } from "api/typesGenerated" ;
36
+ import { PropsWithChildren } from "react" ;
35
37
36
38
// REMARK: some plugins depend on utc, so it's listed first. Otherwise they're
37
39
// sorted alphabetically.
@@ -73,8 +75,9 @@ export interface WorkspaceScheduleFormProps {
73
75
submitScheduleError ?: unknown ;
74
76
initialValues : WorkspaceScheduleFormValues ;
75
77
isLoading : boolean ;
76
- enableAutoStop : boolean ;
77
- enableAutoStart : boolean ;
78
+ allowedTemplateAutoStartDays : TemplateAutostartRequirement [ "days_of_week" ] ;
79
+ allowTemplateAutoStop : boolean ;
80
+ allowTemplateAutoStart : boolean ;
78
81
onCancel : ( ) => void ;
79
82
onSubmit : ( values : WorkspaceScheduleFormValues ) => void ;
80
83
// for storybook
@@ -182,7 +185,7 @@ export const validationSchema = Yup.object({
182
185
} ) ;
183
186
184
187
export const WorkspaceScheduleForm : FC <
185
- React . PropsWithChildren < WorkspaceScheduleFormProps >
188
+ PropsWithChildren < WorkspaceScheduleFormProps >
186
189
> = ( {
187
190
submitScheduleError,
188
191
initialValues,
@@ -191,8 +194,9 @@ export const WorkspaceScheduleForm: FC<
191
194
onSubmit,
192
195
initialTouched,
193
196
defaultTTL,
194
- enableAutoStop,
195
- enableAutoStart,
197
+ allowedTemplateAutoStartDays,
198
+ allowTemplateAutoStop,
199
+ allowTemplateAutoStart,
196
200
} ) => {
197
201
const form = useFormik < WorkspaceScheduleFormValues > ( {
198
202
initialValues,
@@ -207,11 +211,6 @@ export const WorkspaceScheduleForm: FC<
207
211
) ;
208
212
209
213
const checkboxes : Array < { value : boolean ; name : string ; label : string } > = [
210
- {
211
- value : form . values . sunday ,
212
- name : "sunday" ,
213
- label : Language . daySundayLabel ,
214
- } ,
215
214
{
216
215
value : form . values . monday ,
217
216
name : "monday" ,
@@ -242,6 +241,11 @@ export const WorkspaceScheduleForm: FC<
242
241
name : "saturday" ,
243
242
label : Language . daySaturdayLabel ,
244
243
} ,
244
+ {
245
+ value : form . values . sunday ,
246
+ name : "sunday" ,
247
+ label : Language . daySundayLabel ,
248
+ } ,
245
249
] ;
246
250
247
251
const handleToggleAutostart = async ( e : ChangeEvent ) => {
@@ -288,7 +292,7 @@ export const WorkspaceScheduleForm: FC<
288
292
Select the time and days of week on which you want the workspace
289
293
starting automatically.
290
294
</ div >
291
- { ! enableAutoStart && (
295
+ { ! allowTemplateAutoStart && (
292
296
< Tooltip title = "This option can be enabled in the template settings" >
293
297
< DisabledBadge />
294
298
</ Tooltip >
@@ -300,7 +304,7 @@ export const WorkspaceScheduleForm: FC<
300
304
< FormControlLabel
301
305
control = {
302
306
< Switch
303
- disabled = { ! enableAutoStart }
307
+ disabled = { ! allowTemplateAutoStart }
304
308
name = "autostartEnabled"
305
309
checked = { form . values . autostartEnabled }
306
310
onChange = { handleToggleAutostart }
@@ -311,14 +315,26 @@ export const WorkspaceScheduleForm: FC<
311
315
< Stack direction = "row" >
312
316
< TextField
313
317
{ ...formHelpers ( "startTime" ) }
314
- disabled = { isLoading || ! form . values . autostartEnabled }
318
+ // disabled if template does not allow autostart
319
+ // or if primary feature is toggled off via the switch above
320
+ disabled = {
321
+ isLoading ||
322
+ ! allowTemplateAutoStart ||
323
+ ! form . values . autostartEnabled
324
+ }
315
325
label = { Language . startTimeLabel }
316
326
type = "time"
317
327
fullWidth
318
328
/>
319
329
< TextField
320
330
{ ...formHelpers ( "timezone" ) }
321
- disabled = { isLoading || ! form . values . autostartEnabled }
331
+ // disabled if template does not allow autostart
332
+ // or if primary feature is toggled off via the switch above
333
+ disabled = {
334
+ isLoading ||
335
+ ! allowTemplateAutoStart ||
336
+ ! form . values . autostartEnabled
337
+ }
322
338
label = { Language . timezoneLabel }
323
339
select
324
340
fullWidth
@@ -349,7 +365,15 @@ export const WorkspaceScheduleForm: FC<
349
365
control = {
350
366
< Checkbox
351
367
checked = { checkbox . value }
352
- disabled = { isLoading || ! form . values . autostartEnabled }
368
+ // template admins can disable the autostart feature in general,
369
+ // or they can disallow autostart on specific days of the week.
370
+ // also disabled if primary feature switch (above) is toggled off
371
+ disabled = {
372
+ isLoading ||
373
+ ! allowTemplateAutoStart ||
374
+ ! allowedTemplateAutoStartDays . includes ( checkbox . name ) ||
375
+ ! form . values . autostartEnabled
376
+ }
353
377
onChange = { form . handleChange }
354
378
name = { checkbox . name }
355
379
size = "small"
@@ -378,7 +402,7 @@ export const WorkspaceScheduleForm: FC<
378
402
extended by 1 hour after last activity in the workspace was
379
403
detected.
380
404
</ div >
381
- { ! enableAutoStop && (
405
+ { ! allowTemplateAutoStop && (
382
406
< Tooltip title = "This option can be enabled in the template settings" >
383
407
< DisabledBadge />
384
408
</ Tooltip >
@@ -393,7 +417,7 @@ export const WorkspaceScheduleForm: FC<
393
417
name = "autostopEnabled"
394
418
checked = { form . values . autostopEnabled }
395
419
onChange = { handleToggleAutostop }
396
- disabled = { ! enableAutoStop }
420
+ disabled = { ! allowTemplateAutoStop }
397
421
/>
398
422
}
399
423
label = { Language . stopSwitch }
@@ -403,15 +427,28 @@ export const WorkspaceScheduleForm: FC<
403
427
helperText : ttlShutdownAt ( form . values . ttl ) ,
404
428
backendFieldName : "ttl_ms" ,
405
429
} ) }
406
- disabled = { isLoading || ! form . values . autostopEnabled }
430
+ // disabled if autostop disabled at template level or
431
+ // if autostop feature is toggled off via the switch above
432
+ disabled = {
433
+ isLoading ||
434
+ ! allowTemplateAutoStop ||
435
+ ! form . values . autostopEnabled
436
+ }
407
437
inputProps = { { min : 0 , step : "any" } }
408
438
label = { Language . ttlLabel }
409
439
type = "number"
410
440
fullWidth
411
441
/>
412
442
</ FormFields >
413
443
</ FormSection >
414
- < FormFooter onCancel = { onCancel } isLoading = { isLoading } />
444
+ < FormFooter
445
+ onCancel = { onCancel }
446
+ isLoading = { isLoading }
447
+ submitDisabled = {
448
+ ( ! allowTemplateAutoStart && ! allowTemplateAutoStop ) ||
449
+ ( ! form . values . autostartEnabled && ! form . values . autostopEnabled )
450
+ }
451
+ />
415
452
</ HorizontalForm >
416
453
) ;
417
454
} ;
0 commit comments