1
- import type { Workspace , WorkspaceStatus } from "api/typesGenerated" ;
1
+ import type { Workspace } from "api/typesGenerated" ;
2
2
3
3
/**
4
4
* An iterable of all action types supported by the workspace UI
@@ -23,6 +23,10 @@ export const actionTypes = [
23
23
"retry" ,
24
24
"debug" ,
25
25
26
+ // When a template requires updates, we aim to display a distinct update
27
+ // button that clearly indicates a mandatory update.
28
+ "updateAndStart" ,
29
+
26
30
// These are buttons that should be used with disabled UI elements
27
31
"canceling" ,
28
32
"deleted" ,
@@ -52,67 +56,105 @@ export const abilitiesByWorkspaceStatus = (
52
56
const status = workspace . latest_build . status ;
53
57
if ( status === "failed" && canDebug ) {
54
58
return {
55
- ...statusToAbility . failed ,
56
59
actions : [ "retry" , "debug" ] ,
60
+ canCancel : false ,
61
+ canAcceptJobs : true ,
57
62
} ;
58
63
}
59
64
60
- return statusToAbility [ status ] ;
61
- } ;
65
+ switch ( status ) {
66
+ case "starting" : {
67
+ return {
68
+ actions : [ "starting" ] ,
69
+ canCancel : true ,
70
+ canAcceptJobs : false ,
71
+ } ;
72
+ }
73
+ case "running" : {
74
+ const actions : ActionType [ ] = [ "stop" ] ;
62
75
63
- const statusToAbility : Record < WorkspaceStatus , WorkspaceAbilities > = {
64
- starting : {
65
- actions : [ "starting" ] ,
66
- canCancel : true ,
67
- canAcceptJobs : false ,
68
- } ,
69
- running : {
70
- actions : [ "stop" , "restart" ] ,
71
- canCancel : false ,
72
- canAcceptJobs : true ,
73
- } ,
74
- stopping : {
75
- actions : [ "stopping" ] ,
76
- canCancel : true ,
77
- canAcceptJobs : false ,
78
- } ,
79
- stopped : {
80
- actions : [ "start" ] ,
81
- canCancel : false ,
82
- canAcceptJobs : true ,
83
- } ,
84
- canceled : {
85
- actions : [ "start" , "stop" ] ,
86
- canCancel : false ,
87
- canAcceptJobs : true ,
88
- } ,
76
+ // If the template requires the latest version, we prevent the user from
77
+ // restarting the workspace without updating it first. In the Buttons
78
+ // component, we display an UpdateAndStart component to facilitate this.
79
+ if ( ! workspace . template_require_active_version ) {
80
+ actions . push ( "restart" ) ;
81
+ }
89
82
90
- // in the case of an error
91
- failed : {
92
- actions : [ "retry" ] ,
93
- canCancel : false ,
94
- canAcceptJobs : true ,
95
- } ,
83
+ return {
84
+ actions,
85
+ canCancel : false ,
86
+ canAcceptJobs : true ,
87
+ } ;
88
+ }
89
+ case "stopping" : {
90
+ return {
91
+ actions : [ "stopping" ] ,
92
+ canCancel : true ,
93
+ canAcceptJobs : false ,
94
+ } ;
95
+ }
96
+ case "stopped" : {
97
+ const actions : ActionType [ ] = [ ] ;
96
98
97
- // Disabled states
98
- canceling : {
99
- actions : [ "canceling" ] ,
100
- canCancel : false ,
101
- canAcceptJobs : false ,
102
- } ,
103
- deleting : {
104
- actions : [ "deleting" ] ,
105
- canCancel : true ,
106
- canAcceptJobs : false ,
107
- } ,
108
- deleted : {
109
- actions : [ "deleted" ] ,
110
- canCancel : false ,
111
- canAcceptJobs : false ,
112
- } ,
113
- pending : {
114
- actions : [ "pending" ] ,
115
- canCancel : false ,
116
- canAcceptJobs : false ,
117
- } ,
99
+ // If the template requires the latest version, we prevent the user from
100
+ // starting the workspace without updating it first. In the Buttons
101
+ // component, we display an UpdateAndStart component to facilitate this.
102
+ if ( ! workspace . template_require_active_version ) {
103
+ actions . push ( "start" ) ;
104
+ }
105
+
106
+ return {
107
+ actions,
108
+ canCancel : false ,
109
+ canAcceptJobs : true ,
110
+ } ;
111
+ }
112
+ case "canceled" : {
113
+ return {
114
+ actions : [ "start" , "stop" ] ,
115
+ canCancel : false ,
116
+ canAcceptJobs : true ,
117
+ } ;
118
+ }
119
+ case "failed" : {
120
+ return {
121
+ actions : [ "retry" ] ,
122
+ canCancel : false ,
123
+ canAcceptJobs : true ,
124
+ } ;
125
+ }
126
+
127
+ // Disabled states
128
+ case "canceling" : {
129
+ return {
130
+ actions : [ "canceling" ] ,
131
+ canCancel : false ,
132
+ canAcceptJobs : false ,
133
+ } ;
134
+ }
135
+ case "deleting" : {
136
+ return {
137
+ actions : [ "deleting" ] ,
138
+ canCancel : true ,
139
+ canAcceptJobs : false ,
140
+ } ;
141
+ }
142
+ case "deleted" : {
143
+ return {
144
+ actions : [ "deleted" ] ,
145
+ canCancel : false ,
146
+ canAcceptJobs : false ,
147
+ } ;
148
+ }
149
+ case "pending" : {
150
+ return {
151
+ actions : [ "pending" ] ,
152
+ canCancel : false ,
153
+ canAcceptJobs : false ,
154
+ } ;
155
+ }
156
+ default : {
157
+ throw new Error ( `Unknown workspace status: ${ status } ` ) ;
158
+ }
159
+ }
118
160
} ;
0 commit comments