@@ -6,7 +6,6 @@ import Tooltip from "@material-ui/core/Tooltip"
6
6
import AddIcon from "@material-ui/icons/Add"
7
7
import RemoveIcon from "@material-ui/icons/Remove"
8
8
import ScheduleIcon from "@material-ui/icons/Schedule"
9
- import cronstrue from "cronstrue"
10
9
import dayjs from "dayjs"
11
10
import advancedFormat from "dayjs/plugin/advancedFormat"
12
11
import duration from "dayjs/plugin/duration"
@@ -15,10 +14,10 @@ import timezone from "dayjs/plugin/timezone"
15
14
import utc from "dayjs/plugin/utc"
16
15
import { useRef , useState } from "react"
17
16
import { Workspace } from "../../api/typesGenerated"
18
- import { extractTimezone , stripTimezone } from "../../util/schedule"
19
17
import { isWorkspaceOn } from "../../util/workspace"
20
18
import { Stack } from "../Stack/Stack"
21
19
import { WorkspaceSchedule } from "../WorkspaceSchedule/WorkspaceSchedule"
20
+ import { WorkspaceScheduleLabel } from "./WorkspaceScheduleLabel"
22
21
23
22
// REMARK: some plugins depend on utc, so it's listed first. Otherwise they're
24
23
// sorted alphabetically.
@@ -29,60 +28,8 @@ dayjs.extend(relativeTime)
29
28
dayjs . extend ( timezone )
30
29
31
30
export const Language = {
32
- autoStartDisplay : ( schedule : string | undefined ) : string => {
33
- if ( schedule ) {
34
- return (
35
- cronstrue
36
- . toString ( stripTimezone ( schedule ) , { throwExceptionOnParseError : false } )
37
- // We don't want to keep the At because it is on the label
38
- . replace ( "At" , "" )
39
- )
40
- } else {
41
- return "Manual"
42
- }
43
- } ,
44
- autoStartLabel : "Starts at" ,
45
- autoStopLabel : "Stops at" ,
46
- workspaceShuttingDownLabel : "Workspace is shutting down" ,
47
- autoStopDisplay : ( workspace : Workspace ) : string => {
48
- const deadline = dayjs ( workspace . latest_build . deadline ) . utc ( )
49
- // a manual shutdown has a deadline of '"0001-01-01T00:00:00Z"'
50
- // SEE: #1834
51
- const hasDeadline = deadline . year ( ) > 1
52
- const ttl = workspace . ttl_ms
53
-
54
- if ( isWorkspaceOn ( workspace ) && hasDeadline ) {
55
- // Workspace is on --> derive from latest_build.deadline. Note that the
56
- // user may modify their workspace object (ttl) while the workspace is
57
- // running and depending on system semantics, the deadline may still
58
- // represent the previously defined ttl. Thus, we always derive from the
59
- // deadline as the source of truth.
60
- const now = dayjs ( ) . utc ( )
61
- if ( now . isAfter ( deadline ) ) {
62
- return Language . workspaceShuttingDownLabel
63
- } else {
64
- return deadline . tz ( dayjs . tz . guess ( ) ) . format ( "MMM D, YYYY h:mm A" )
65
- }
66
- } else if ( ! ttl || ttl < 1 ) {
67
- // If the workspace is not on, and the ttl is 0 or undefined, then the
68
- // workspace is set to manually shutdown.
69
- return "Manual"
70
- } else {
71
- // The workspace has a ttl set, but is either in an unknown state or is
72
- // not running. Therefore, we derive from workspace.ttl.
73
- const duration = dayjs . duration ( ttl , "milliseconds" )
74
- return `${ duration . humanize ( ) } after start`
75
- }
76
- } ,
77
- editScheduleLink : "Edit schedule" ,
78
31
editDeadlineMinus : "Subtract one hour" ,
79
32
editDeadlinePlus : "Add one hour" ,
80
- scheduleHeader : ( workspace : Workspace ) : string => {
81
- const tz = workspace . autostart_schedule
82
- ? extractTimezone ( workspace . autostart_schedule )
83
- : dayjs . tz . guess ( )
84
- return `Schedule (${ tz } )`
85
- } ,
86
33
}
87
34
88
35
export const shouldDisplayPlusMinus = ( workspace : Workspace ) : boolean => {
@@ -103,44 +50,16 @@ export const deadlinePlusDisabled = (workspace: Workspace, now: dayjs.Dayjs): bo
103
50
return delta >= 24 * 60 * 60 * 1000 // 24 hours
104
51
}
105
52
106
- const WorkspaceScheduleLabel : React . FC < { workspace : Workspace } > = ( { workspace } ) => {
107
- const styles = useStyles ( )
108
-
109
- if ( isWorkspaceOn ( workspace ) ) {
110
- const stopLabel = Language . autoStopDisplay ( workspace )
111
- const isShuttingDown = stopLabel === Language . workspaceShuttingDownLabel
112
-
113
- // If it is shutting down, we don't need to display the auto stop label
114
- return (
115
- < span className = { styles . labelText } >
116
- { ! isShuttingDown && (
117
- < strong className = { styles . labelStrong } > { Language . autoStopLabel } </ strong >
118
- ) }
119
- { stopLabel }
120
- </ span >
121
- )
122
- }
123
-
124
- return (
125
- < span className = { styles . labelText } >
126
- < strong className = { styles . labelStrong } > { Language . autoStartLabel } </ strong >
127
- { Language . autoStartDisplay ( workspace . autostart_schedule ) }
128
- </ span >
129
- )
130
- }
131
-
132
53
export interface WorkspaceScheduleButtonProps {
133
54
workspace : Workspace
134
55
onDeadlinePlus : ( ) => void
135
56
onDeadlineMinus : ( ) => void
136
- now ?: dayjs . Dayjs
137
57
}
138
58
139
59
export const WorkspaceScheduleButton : React . FC < WorkspaceScheduleButtonProps > = ( {
140
60
workspace,
141
61
onDeadlinePlus,
142
62
onDeadlineMinus,
143
- now,
144
63
} ) => {
145
64
const anchorRef = useRef < HTMLButtonElement > ( null )
146
65
const [ isOpen , setIsOpen ] = useState ( false )
@@ -160,7 +79,7 @@ export const WorkspaceScheduleButton: React.FC<WorkspaceScheduleButtonProps> = (
160
79
< IconButton
161
80
className = { styles . iconButton }
162
81
size = "small"
163
- disabled = { deadlineMinusDisabled ( workspace , now ?? dayjs ( ) ) }
82
+ disabled = { deadlineMinusDisabled ( workspace , dayjs ( ) ) }
164
83
onClick = { onDeadlineMinus }
165
84
>
166
85
< Tooltip title = { Language . editDeadlineMinus } >
@@ -170,7 +89,7 @@ export const WorkspaceScheduleButton: React.FC<WorkspaceScheduleButtonProps> = (
170
89
< IconButton
171
90
className = { styles . iconButton }
172
91
size = "small"
173
- disabled = { deadlinePlusDisabled ( workspace , now ?? dayjs ( ) ) }
92
+ disabled = { deadlinePlusDisabled ( workspace , dayjs ( ) ) }
174
93
onClick = { onDeadlinePlus }
175
94
>
176
95
< Tooltip title = { Language . editDeadlinePlus } >
@@ -231,14 +150,6 @@ const useStyles = makeStyles((theme) => ({
231
150
minHeight : 42 ,
232
151
} ,
233
152
234
- labelText : {
235
- marginRight : theme . spacing ( 2 ) ,
236
- } ,
237
-
238
- labelStrong : {
239
- marginRight : theme . spacing ( 0.5 ) ,
240
- } ,
241
-
242
153
iconButton : {
243
154
borderRadius : 2 ,
244
155
} ,
0 commit comments