1
1
import Button from "@material-ui/core/Button"
2
2
import Popover from "@material-ui/core/Popover"
3
3
import { makeStyles } from "@material-ui/core/styles"
4
- import { FC , ReactNode , useEffect , useRef , useState } from "react"
4
+ import { FC , ReactNode , useEffect , useMemo , useRef , useState } from "react"
5
5
import { Workspace } from "../../api/typesGenerated"
6
- import { getWorkspaceStatus } from "../../util/workspace"
6
+ import { getWorkspaceStatus , WorkspaceStatus } from "../../util/workspace"
7
7
import { CloseDropdown , OpenDropdown } from "../DropdownArrows/DropdownArrows"
8
8
import { CancelButton , DeleteButton , StartButton , StopButton , UpdateButton } from "./ActionCtas"
9
9
import { ButtonTypesEnum , WorkspaceStateActions , WorkspaceStateEnum } from "./constants"
10
10
11
+ /**
12
+ * Jobs submitted while another job is in progress will be discarded,
13
+ * so check whether workspace job status has reached completion (whether successful or not).
14
+ */
15
+ const canAcceptJobs = ( workspaceStatus : WorkspaceStatus ) =>
16
+ [ "started" , "stopped" , "deleted" , "error" , "canceled" ] . includes ( workspaceStatus )
17
+
11
18
export interface WorkspaceActionsProps {
12
19
workspace : Workspace
13
20
handleStart : ( ) => void
@@ -34,7 +41,23 @@ export const WorkspaceActions: FC<WorkspaceActionsProps> = ({
34
41
workspace . latest_build ,
35
42
)
36
43
const workspaceState = WorkspaceStateEnum [ workspaceStatus ]
37
- const actions = WorkspaceStateActions [ workspaceState ]
44
+
45
+ const canBeUpdated = workspace . outdated && canAcceptJobs ( workspaceStatus )
46
+
47
+ // actions are the primary and secondary CTAs that appear in the workspace actions dropdown
48
+ const actions = useMemo ( ( ) => {
49
+ if ( ! canBeUpdated ) {
50
+ return WorkspaceStateActions [ workspaceState ]
51
+ }
52
+
53
+ // if an update is available, we make the update button the primary CTA
54
+ // and move the former primary CTA to the secondary actions list
55
+ const updatedActions = { ...WorkspaceStateActions [ workspaceState ] }
56
+ updatedActions . secondary . unshift ( updatedActions . primary )
57
+ updatedActions . primary = ButtonTypesEnum . update
58
+
59
+ return updatedActions
60
+ } , [ canBeUpdated , workspaceState ] )
38
61
39
62
/**
40
63
* Ensures we close the popover before calling any action handler
@@ -58,16 +81,10 @@ export const WorkspaceActions: FC<WorkspaceActionsProps> = ({
58
81
59
82
// A mapping of button type to the corresponding React component
60
83
const buttonMapping : ButtonMapping = {
84
+ [ ButtonTypesEnum . update ] : < UpdateButton handleAction = { handleUpdate } /> ,
61
85
[ ButtonTypesEnum . start ] : < StartButton handleAction = { handleStart } /> ,
62
86
[ ButtonTypesEnum . stop ] : < StopButton handleAction = { handleStop } /> ,
63
87
[ ButtonTypesEnum . delete ] : < DeleteButton handleAction = { handleDelete } /> ,
64
- [ ButtonTypesEnum . update ] : (
65
- < UpdateButton
66
- handleAction = { handleUpdate }
67
- workspace = { workspace }
68
- workspaceStatus = { workspaceStatus }
69
- />
70
- ) ,
71
88
[ ButtonTypesEnum . cancel ] : < CancelButton handleAction = { handleCancel } /> ,
72
89
[ ButtonTypesEnum . canceling ] : disabledButton ,
73
90
[ ButtonTypesEnum . disabled ] : disabledButton ,
0 commit comments