1
- import IconButton from "@material-ui/core/IconButton"
2
1
import Link from "@material-ui/core/Link"
3
2
import { makeStyles } from "@material-ui/core/styles"
4
- import Tooltip from "@material-ui/core/Tooltip"
5
- import Typography from "@material-ui/core/Typography"
6
- import AddBoxIcon from "@material-ui/icons/AddBox"
7
- import IndeterminateCheckBoxIcon from "@material-ui/icons/IndeterminateCheckBox"
8
- import ScheduleIcon from "@material-ui/icons/Schedule"
9
- import cronstrue from "cronstrue"
10
3
import dayjs from "dayjs"
11
4
import advancedFormat from "dayjs/plugin/advancedFormat"
12
5
import duration from "dayjs/plugin/duration"
@@ -17,8 +10,12 @@ import { FC } from "react"
17
10
import { Link as RouterLink } from "react-router-dom"
18
11
import { Workspace } from "../../api/typesGenerated"
19
12
import { MONOSPACE_FONT_FAMILY } from "../../theme/constants"
20
- import { extractTimezone , stripTimezone } from "../../util/schedule"
21
- import { isWorkspaceOn } from "../../util/workspace"
13
+ import {
14
+ autoStartDisplay ,
15
+ autoStopDisplay ,
16
+ extractTimezone ,
17
+ Language as ScheduleLanguage ,
18
+ } from "../../util/schedule"
22
19
import { Stack } from "../Stack/Stack"
23
20
24
21
// REMARK: some plugins depend on utc, so it's listed first. Otherwise they're
@@ -30,133 +27,39 @@ dayjs.extend(relativeTime)
30
27
dayjs . extend ( timezone )
31
28
32
29
export const Language = {
33
- autoStartDisplay : ( schedule : string | undefined ) : string => {
34
- if ( schedule ) {
35
- return cronstrue . toString ( stripTimezone ( schedule ) , { throwExceptionOnParseError : false } )
36
- } else {
37
- return "Manual"
38
- }
39
- } ,
40
- autoStartLabel : "START" ,
41
- autoStopLabel : "SHUTDOWN" ,
42
- autoStopDisplay : ( workspace : Workspace ) : string => {
43
- const deadline = dayjs ( workspace . latest_build . deadline ) . utc ( )
44
- // a manual shutdown has a deadline of '"0001-01-01T00:00:00Z"'
45
- // SEE: #1834
46
- const hasDeadline = deadline . year ( ) > 1
47
- const ttl = workspace . ttl_ms
48
-
49
- if ( isWorkspaceOn ( workspace ) && hasDeadline ) {
50
- // Workspace is on --> derive from latest_build.deadline. Note that the
51
- // user may modify their workspace object (ttl) while the workspace is
52
- // running and depending on system semantics, the deadline may still
53
- // represent the previously defined ttl. Thus, we always derive from the
54
- // deadline as the source of truth.
55
- const now = dayjs ( ) . utc ( )
56
- if ( now . isAfter ( deadline ) ) {
57
- return "Workspace is shutting down"
58
- } else {
59
- return deadline . tz ( dayjs . tz . guess ( ) ) . format ( "MMM D, YYYY h:mm A" )
60
- }
61
- } else if ( ! ttl || ttl < 1 ) {
62
- // If the workspace is not on, and the ttl is 0 or undefined, then the
63
- // workspace is set to manually shutdown.
64
- return "Manual"
65
- } else {
66
- // The workspace has a ttl set, but is either in an unknown state or is
67
- // not running. Therefore, we derive from workspace.ttl.
68
- const duration = dayjs . duration ( ttl , "milliseconds" )
69
- return `${ duration . humanize ( ) } after start`
70
- }
71
- } ,
72
30
editScheduleLink : "Edit schedule" ,
73
- editDeadlineMinus : "Subtract one hour" ,
74
- editDeadlinePlus : "Add one hour" ,
75
- scheduleHeader : ( workspace : Workspace ) : string => {
76
- const tz = workspace . autostart_schedule
77
- ? extractTimezone ( workspace . autostart_schedule )
78
- : dayjs . tz . guess ( )
79
- return `Schedule (${ tz } )`
80
- } ,
31
+ timezoneLabel : "Timezone" ,
81
32
}
82
33
83
34
export interface WorkspaceScheduleProps {
84
- now ?: dayjs . Dayjs
85
35
workspace : Workspace
86
- onDeadlinePlus : ( ) => void
87
- onDeadlineMinus : ( ) => void
88
- }
89
-
90
- export const shouldDisplayPlusMinus = ( workspace : Workspace ) : boolean => {
91
- if ( ! isWorkspaceOn ( workspace ) ) {
92
- return false
93
- }
94
- const deadline = dayjs ( workspace . latest_build . deadline ) . utc ( )
95
- return deadline . year ( ) > 1
96
- }
97
-
98
- export const deadlineMinusDisabled = ( workspace : Workspace , now : dayjs . Dayjs ) : boolean => {
99
- const delta = dayjs ( workspace . latest_build . deadline ) . diff ( now )
100
- return delta <= 30 * 60 * 1000 // 30 minutes
101
- }
102
-
103
- export const deadlinePlusDisabled = ( workspace : Workspace , now : dayjs . Dayjs ) : boolean => {
104
- const delta = dayjs ( workspace . latest_build . deadline ) . diff ( now )
105
- return delta >= 24 * 60 * 60 * 1000 // 24 hours
106
36
}
107
37
108
- export const WorkspaceSchedule : FC < WorkspaceScheduleProps > = ( {
109
- now,
110
- workspace,
111
- onDeadlineMinus,
112
- onDeadlinePlus,
113
- } ) => {
38
+ export const WorkspaceSchedule : FC < WorkspaceScheduleProps > = ( { workspace } ) => {
114
39
const styles = useStyles ( )
115
- const editDeadlineButtons = shouldDisplayPlusMinus ( workspace ) ? (
116
- < Stack direction = "row" spacing = { 0 } >
117
- < IconButton
118
- size = "small"
119
- disabled = { deadlineMinusDisabled ( workspace , now ?? dayjs ( ) ) }
120
- className = { styles . editDeadline }
121
- onClick = { onDeadlineMinus }
122
- >
123
- < Tooltip title = { Language . editDeadlineMinus } >
124
- < IndeterminateCheckBoxIcon />
125
- </ Tooltip >
126
- </ IconButton >
127
- < IconButton
128
- size = "small"
129
- disabled = { deadlinePlusDisabled ( workspace , now ?? dayjs ( ) ) }
130
- className = { styles . editDeadline }
131
- onClick = { onDeadlinePlus }
132
- >
133
- < Tooltip title = { Language . editDeadlinePlus } >
134
- < AddBoxIcon />
135
- </ Tooltip >
136
- </ IconButton >
137
- </ Stack >
138
- ) : null
40
+ const timezone = workspace . autostart_schedule
41
+ ? extractTimezone ( workspace . autostart_schedule )
42
+ : dayjs . tz . guess ( )
139
43
140
44
return (
141
45
< div className = { styles . schedule } >
142
46
< Stack spacing = { 2 } >
143
- < Typography variant = "body1" className = { styles . title } >
144
- < ScheduleIcon className = { styles . scheduleIcon } />
145
- { Language . scheduleHeader ( workspace ) }
146
- </ Typography >
147
47
< div >
148
- < span className = { styles . scheduleLabel } > { Language . autoStartLabel } </ span >
48
+ < span className = { styles . scheduleLabel } > { Language . timezoneLabel } </ span >
49
+ < span className = { styles . scheduleValue } > { timezone } </ span >
50
+ </ div >
51
+ < div >
52
+ < span className = { styles . scheduleLabel } > { ScheduleLanguage . autoStartLabel } </ span >
149
53
< span className = { [ styles . scheduleValue , "chromatic-ignore" ] . join ( " " ) } >
150
- { Language . autoStartDisplay ( workspace . autostart_schedule ) }
54
+ { autoStartDisplay ( workspace . autostart_schedule ) }
151
55
</ span >
152
56
</ div >
153
57
< div >
154
- < span className = { styles . scheduleLabel } > { Language . autoStopLabel } </ span >
58
+ < span className = { styles . scheduleLabel } > { ScheduleLanguage . autoStopLabel } </ span >
155
59
< Stack direction = "row" >
156
60
< span className = { [ styles . scheduleValue , "chromatic-ignore" ] . join ( " " ) } >
157
- { Language . autoStopDisplay ( workspace ) }
61
+ { autoStopDisplay ( workspace ) }
158
62
</ span >
159
- { editDeadlineButtons }
160
63
</ Stack >
161
64
</ div >
162
65
< div >
@@ -177,18 +80,6 @@ const useStyles = makeStyles((theme) => ({
177
80
schedule : {
178
81
fontFamily : MONOSPACE_FONT_FAMILY ,
179
82
} ,
180
- title : {
181
- fontWeight : 600 ,
182
-
183
- fontFamily : "inherit" ,
184
- display : "flex" ,
185
- alignItems : "center" ,
186
- } ,
187
- scheduleIcon : {
188
- width : 16 ,
189
- height : 16 ,
190
- marginRight : theme . spacing ( 1 ) ,
191
- } ,
192
83
scheduleLabel : {
193
84
fontSize : 12 ,
194
85
textTransform : "uppercase" ,
@@ -198,14 +89,11 @@ const useStyles = makeStyles((theme) => ({
198
89
} ,
199
90
scheduleValue : {
200
91
fontSize : 14 ,
201
- marginTop : theme . spacing ( 0.75 ) ,
92
+ marginTop : theme . spacing ( 0.5 ) ,
202
93
display : "inline-block" ,
203
94
color : theme . palette . text . secondary ,
204
95
} ,
205
96
scheduleAction : {
206
97
cursor : "pointer" ,
207
98
} ,
208
- editDeadline : {
209
- color : theme . palette . text . secondary ,
210
- } ,
211
99
} ) )
0 commit comments