1
1
import TextField from "@mui/material/TextField"
2
- import {
3
- Template ,
4
- UpdateTemplateMeta ,
5
- WorkspaceStatus ,
6
- Workspace ,
7
- } from "api/typesGenerated"
2
+ import { Template , UpdateTemplateMeta } from "api/typesGenerated"
8
3
import { FormikTouched , useFormik } from "formik"
9
4
import { FC , ChangeEvent , useState } from "react"
10
5
import { getFormHelpers } from "utils/formUtils"
11
- import * as Yup from "yup"
12
- import i18next from "i18next"
13
6
import { useTranslation } from "react-i18next"
14
- import { Maybe } from "components/Conditionals/Maybe"
15
7
import {
16
8
FormSection ,
17
9
HorizontalForm ,
@@ -24,87 +16,16 @@ import Link from "@mui/material/Link"
24
16
import Checkbox from "@mui/material/Checkbox"
25
17
import FormControlLabel from "@mui/material/FormControlLabel"
26
18
import Switch from "@mui/material/Switch"
27
- import { ConfirmDialog } from "components/Dialogs/ConfirmDialog/ConfirmDialog"
28
- import { useQuery } from "@tanstack/react-query"
29
- import { getWorkspaces } from "api/api"
30
- import { compareAsc , add , endOfToday } from "date-fns"
31
-
32
- const TTLHelperText = ( {
33
- ttl,
34
- translationName,
35
- } : {
36
- ttl ?: number
37
- translationName : string
38
- } ) => {
39
- const { t } = useTranslation ( "templateSettingsPage" )
40
- const count = typeof ttl !== "number" ? 0 : ttl
41
- return (
42
- // no helper text if ttl is negative - error will show once field is considered touched
43
- < Maybe condition = { count >= 0 } >
44
- < span > { t ( translationName , { count } ) } </ span >
45
- </ Maybe >
46
- )
47
- }
19
+ import { InactivityDialog } from "./InactivityDialog"
20
+ import { useWorkspacesData } from "./useWorkspacesData"
21
+ import { TemplateScheduleFormValues , getValidationSchema } from "./formHelpers"
22
+ import { TTLHelperText } from "./TTLHelperText"
48
23
49
- const MAX_TTL_DAYS = 7
50
24
const MS_HOUR_CONVERSION = 3600000
51
25
const MS_DAY_CONVERSION = 86400000
52
26
const FAILURE_CLEANUP_DEFAULT = 7
53
27
const INACTIVITY_CLEANUP_DEFAULT = 180
54
28
55
- export interface TemplateScheduleFormValues extends UpdateTemplateMeta {
56
- failure_cleanup_enabled : boolean
57
- inactivity_cleanup_enabled : boolean
58
- }
59
-
60
- export const getValidationSchema = ( ) : Yup . AnyObjectSchema =>
61
- Yup . object ( {
62
- default_ttl_ms : Yup . number ( )
63
- . integer ( )
64
- . min ( 0 , i18next . t ( "defaultTTLMinError" , { ns : "templateSettingsPage" } ) )
65
- . max (
66
- 24 * MAX_TTL_DAYS /* 7 days in hours */ ,
67
- i18next . t ( "defaultTTLMaxError" , { ns : "templateSettingsPage" } ) ,
68
- ) ,
69
- max_ttl_ms : Yup . number ( )
70
- . integer ( )
71
- . min ( 0 , i18next . t ( "maxTTLMinError" , { ns : "templateSettingsPage" } ) )
72
- . max (
73
- 24 * MAX_TTL_DAYS /* 7 days in hours */ ,
74
- i18next . t ( "maxTTLMaxError" , { ns : "templateSettingsPage" } ) ,
75
- ) ,
76
- failure_ttl_ms : Yup . number ( )
77
- . min ( 0 , "Failure cleanup days must not be less than 0." )
78
- . test (
79
- "positive-if-enabled" ,
80
- "Failure cleanup days must be greater than zero when enabled." ,
81
- function ( value ) {
82
- const parent = this . parent as TemplateScheduleFormValues
83
- if ( parent . failure_cleanup_enabled ) {
84
- return Boolean ( value )
85
- } else {
86
- return true
87
- }
88
- } ,
89
- ) ,
90
- inactivity_ttl_ms : Yup . number ( )
91
- . min ( 0 , "Inactivity cleanup days must not be less than 0." )
92
- . test (
93
- "positive-if-enabled" ,
94
- "Inactivity cleanup days must be greater than zero when enabled." ,
95
- function ( value ) {
96
- const parent = this . parent as TemplateScheduleFormValues
97
- if ( parent . inactivity_cleanup_enabled ) {
98
- return Boolean ( value )
99
- } else {
100
- return true
101
- }
102
- } ,
103
- ) ,
104
- allow_user_autostart : Yup . boolean ( ) ,
105
- allow_user_autostop : Yup . boolean ( ) ,
106
- } )
107
-
108
29
export interface TemplateScheduleForm {
109
30
template : Template
110
31
onSubmit : ( data : UpdateTemplateMeta ) => void
@@ -172,6 +93,9 @@ export const TemplateScheduleForm: FC<TemplateScheduleForm> = ({
172
93
)
173
94
const { t } = useTranslation ( "templateSettingsPage" )
174
95
const styles = useStyles ( )
96
+
97
+ const workspacesToBeDeletedToday = useWorkspacesData ( form . values )
98
+
175
99
const [ isInactivityDialogOpen , setIsInactivityDialogOpen ] =
176
100
useState < boolean > ( false )
177
101
@@ -196,35 +120,6 @@ export const TemplateScheduleForm: FC<TemplateScheduleForm> = ({
196
120
} )
197
121
}
198
122
199
- const { data : workspacesData } = useQuery ( {
200
- queryKey : [ "workspaces" ] ,
201
- queryFn : ( ) => getWorkspaces ( { } ) ,
202
- enabled : form . values . inactivity_cleanup_enabled ,
203
- } )
204
-
205
- const inactiveStatuses : WorkspaceStatus [ ] = [
206
- "stopped" ,
207
- "canceled" ,
208
- "failed" ,
209
- "deleted" ,
210
- ]
211
-
212
- const workspacesToBeDeletedToday = workspacesData ?. workspaces ?. filter (
213
- ( workspace : Workspace ) => {
214
- const isInactive = inactiveStatuses . includes (
215
- workspace . latest_build . status ,
216
- )
217
-
218
- const proposedDeletion = add ( new Date ( workspace . last_used_at ) , {
219
- days : form . values . inactivity_ttl_ms ,
220
- } )
221
-
222
- if ( isInactive && compareAsc ( proposedDeletion , endOfToday ( ) ) < 1 ) {
223
- return workspace
224
- }
225
- } ,
226
- )
227
-
228
123
const handleToggleFailureCleanup = async ( e : ChangeEvent ) => {
229
124
form . handleChange ( e )
230
125
if ( ! form . values . failure_cleanup_enabled ) {
@@ -437,25 +332,17 @@ export const TemplateScheduleForm: FC<TemplateScheduleForm> = ({
437
332
</ FormSection >
438
333
</ >
439
334
) }
335
+ < InactivityDialog
336
+ formValues = { form . values }
337
+ submitValues = { submitValues }
338
+ isInactivityDialogOpen = { isInactivityDialogOpen }
339
+ setIsInactivityDialogOpen = { setIsInactivityDialogOpen }
340
+ />
440
341
< FormFooter
441
342
onCancel = { onCancel }
442
343
isLoading = { isSubmitting }
443
344
submitDisabled = { ! form . isValid || ! form . dirty }
444
345
/>
445
- < ConfirmDialog
446
- type = "delete"
447
- open = { isInactivityDialogOpen }
448
- onConfirm = { ( ) => {
449
- submitValues ( form . values )
450
- setIsInactivityDialogOpen ( false )
451
- } }
452
- onClose = { ( ) => setIsInactivityDialogOpen ( false ) }
453
- title = "Delete inactive workspaces"
454
- confirmText = "Delete Workspaces"
455
- description = { `There are ${
456
- workspacesToBeDeletedToday ?. length ?? ""
457
- } workspaces that already match this filter and will be deleted upon form submission. Are you sure you want to proceed?`}
458
- />
459
346
</ HorizontalForm >
460
347
)
461
348
}
0 commit comments