File tree 3 files changed +35
-39
lines changed
site/src/pages/TemplateSettingsPage/TemplateSchedulePage/TemplateScheduleForm 3 files changed +35
-39
lines changed Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ import Checkbox from "@mui/material/Checkbox"
17
17
import FormControlLabel from "@mui/material/FormControlLabel"
18
18
import Switch from "@mui/material/Switch"
19
19
import { InactivityDialog } from "./InactivityDialog"
20
- import { useWorkspacesData } from "./useWorkspacesData "
20
+ import { useWorkspacesToBeDeleted } from "./useWorkspacesToBeDeleted "
21
21
import { TemplateScheduleFormValues , getValidationSchema } from "./formHelpers"
22
22
import { TTLHelperText } from "./TTLHelperText"
23
23
@@ -94,7 +94,7 @@ export const TemplateScheduleForm: FC<TemplateScheduleForm> = ({
94
94
const { t } = useTranslation ( "templateSettingsPage" )
95
95
const styles = useStyles ( )
96
96
97
- const workspacesToBeDeletedToday = useWorkspacesData ( form . values )
97
+ const workspacesToBeDeletedToday = useWorkspacesToBeDeleted ( form . values )
98
98
99
99
const [ isInactivityDialogOpen , setIsInactivityDialogOpen ] =
100
100
useState < boolean > ( false )
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ import { useQuery } from "@tanstack/react-query"
2
+ import { getWorkspaces } from "api/api"
3
+ import { compareAsc , add , endOfToday } from "date-fns"
4
+ import { WorkspaceStatus , Workspace } from "api/typesGenerated"
5
+ import { TemplateScheduleFormValues } from "./formHelpers"
6
+
7
+ const inactiveStatuses : WorkspaceStatus [ ] = [
8
+ "stopped" ,
9
+ "canceled" ,
10
+ "failed" ,
11
+ "deleted" ,
12
+ ]
13
+
14
+ export const useWorkspacesToBeDeleted = (
15
+ formValues : TemplateScheduleFormValues ,
16
+ ) => {
17
+ const { data : workspacesData } = useQuery ( {
18
+ queryKey : [ "workspaces" ] ,
19
+ queryFn : ( ) => getWorkspaces ( { } ) ,
20
+ enabled : formValues . inactivity_cleanup_enabled ,
21
+ } )
22
+ return workspacesData ?. workspaces ?. filter ( ( workspace : Workspace ) => {
23
+ const isInactive = inactiveStatuses . includes ( workspace . latest_build . status )
24
+
25
+ const proposedDeletion = add ( new Date ( workspace . last_used_at ) , {
26
+ days : formValues . inactivity_ttl_ms ,
27
+ } )
28
+
29
+ if ( isInactive && compareAsc ( proposedDeletion , endOfToday ( ) ) < 1 ) {
30
+ return workspace
31
+ }
32
+ } )
33
+ }
You can’t perform that action at this time.
0 commit comments