11import Button from "@material-ui/core/Button"
22import Link from "@material-ui/core/Link"
33import { makeStyles } from "@material-ui/core/styles"
4+ import CancelIcon from "@material-ui/icons/Cancel"
45import CloudDownloadIcon from "@material-ui/icons/CloudDownload"
56import PlayArrowRoundedIcon from "@material-ui/icons/PlayArrowRounded"
6- import ReplayIcon from "@material-ui/icons/Replay"
77import StopIcon from "@material-ui/icons/Stop"
88import React from "react"
99import { Link as RouterLink } from "react-router-dom"
@@ -17,7 +17,7 @@ export const Language = {
1717 stopping : "Stopping workspace" ,
1818 start : "Start workspace" ,
1919 starting : "Starting workspace" ,
20- retry : "Retry " ,
20+ cancel : "Cancel action " ,
2121 update : "Update workspace" ,
2222}
2323
@@ -28,20 +28,32 @@ export const Language = {
2828const canAcceptJobs = ( workspaceStatus : WorkspaceStatus ) =>
2929 [ "started" , "stopped" , "deleted" , "error" , "canceled" ] . includes ( workspaceStatus )
3030
31+ /**
32+ * Jobs that are in progress (queued or pending) can be canceled.
33+ * @param workspaceStatus WorkspaceStatus
34+ * @returns boolean
35+ */
36+ const canCancelJobs = ( workspaceStatus : WorkspaceStatus ) =>
37+ [ "starting" , "stopping" , "deleting" ] . includes ( workspaceStatus )
38+
39+ const canStart = ( workspaceStatus : WorkspaceStatus ) => [ "stopped" , "canceled" , "error" ] . includes ( workspaceStatus )
40+
41+ const canStop = ( workspaceStatus : WorkspaceStatus ) => [ "started" , "canceled" , "error" ] . includes ( workspaceStatus )
42+
3143export interface WorkspaceActionsProps {
3244 workspace : Workspace
3345 handleStart : ( ) => void
3446 handleStop : ( ) => void
35- handleRetry : ( ) => void
3647 handleUpdate : ( ) => void
48+ handleCancel : ( ) => void
3749}
3850
3951export const WorkspaceActions : React . FC < WorkspaceActionsProps > = ( {
4052 workspace,
4153 handleStart,
4254 handleStop,
43- handleRetry,
4455 handleUpdate,
56+ handleCancel,
4557} ) => {
4658 const styles = useStyles ( )
4759 const workspaceStatus = getWorkspaceStatus ( workspace . latest_build )
@@ -51,31 +63,30 @@ export const WorkspaceActions: React.FC<WorkspaceActionsProps> = ({
5163 < Link underline = "none" component = { RouterLink } to = "edit" >
5264 < Button variant = "outlined" > Settings</ Button >
5365 </ Link >
54- { ( workspaceStatus === "started" || workspaceStatus === "stopping" ) && (
66+ { canStart ( workspaceStatus ) && (
67+ < WorkspaceActionButton
68+ className = { styles . actionButton }
69+ icon = { < PlayArrowRoundedIcon /> }
70+ onClick = { handleStart }
71+ label = { Language . start }
72+ />
73+ ) }
74+ { canStop ( workspaceStatus ) && (
5575 < WorkspaceActionButton
5676 className = { styles . actionButton }
5777 icon = { < StopIcon /> }
5878 onClick = { handleStop }
5979 label = { Language . stop }
60- loadingLabel = { Language . stopping }
61- isLoading = { workspaceStatus === "stopping" }
6280 />
6381 ) }
64- { ( workspaceStatus === "stopped" || workspaceStatus === "starting" ) && (
82+ { canCancelJobs ( workspaceStatus ) && (
6583 < WorkspaceActionButton
6684 className = { styles . actionButton }
67- icon = { < PlayArrowRoundedIcon /> }
68- onClick = { handleStart }
69- label = { Language . start }
70- loadingLabel = { Language . starting }
71- isLoading = { workspaceStatus === "starting" }
85+ icon = { < CancelIcon /> }
86+ onClick = { handleCancel }
87+ label = { Language . cancel }
7288 />
7389 ) }
74- { workspaceStatus === "error" && (
75- < Button className = { styles . actionButton } startIcon = { < ReplayIcon /> } onClick = { handleRetry } >
76- { Language . retry }
77- </ Button >
78- ) }
7990 { workspace . outdated && canAcceptJobs ( workspaceStatus ) && (
8091 < Button className = { styles . actionButton } startIcon = { < CloudDownloadIcon /> } onClick = { handleUpdate } >
8192 { Language . update }
@@ -89,6 +100,6 @@ const useStyles = makeStyles((theme) => ({
89100 actionButton : {
90101 // Set fixed width for the action buttons so they will not change the size
91102 // during the transitions
92- width : theme . spacing ( 30 ) ,
103+ width : theme . spacing ( 27 ) ,
93104 } ,
94105} ) )
0 commit comments