20000: Correctly show process cancel on locked containers
[arvados-workbench2.git] / src / views / process-panel / process-details-card.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from 'react';
6 import {
7     StyleRulesCallback,
8     WithStyles,
9     withStyles,
10     Card,
11     CardHeader,
12     IconButton,
13     CardContent,
14     Tooltip,
15     Typography,
16     Button,
17 } from '@material-ui/core';
18 import { ArvadosTheme } from 'common/custom-theme';
19 import { CloseIcon, MoreOptionsIcon, ProcessIcon, StartIcon } from 'components/icon/icon';
20 import { Process } from 'store/processes/process';
21 import { MPVPanelProps } from 'components/multi-panel-view/multi-panel-view';
22 import { ProcessDetailsAttributes } from './process-details-attributes';
23 import { ProcessStatus } from 'views-components/data-explorer/renderers';
24 import { ContainerState } from 'models/container';
25 import { ContainerRequestState } from 'models/container-request';
26
27 type CssRules = 'card' | 'content' | 'title' | 'header' | 'cancelButton' | 'avatar' | 'iconHeader' | 'runButton';
28
29 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
30     card: {
31         height: '100%'
32     },
33     header: {
34         paddingTop: theme.spacing.unit,
35         paddingBottom: theme.spacing.unit,
36     },
37     iconHeader: {
38         fontSize: '1.875rem',
39         color: theme.customs.colors.greyL,
40     },
41     avatar: {
42         alignSelf: 'flex-start',
43         paddingTop: theme.spacing.unit * 0.5
44     },
45     content: {
46         padding: theme.spacing.unit * 1.0,
47         paddingTop: theme.spacing.unit * 0.5,
48         '&:last-child': {
49             paddingBottom: theme.spacing.unit * 1,
50         }
51     },
52     title: {
53         overflow: 'hidden',
54         paddingTop: theme.spacing.unit * 0.5,
55         color: theme.customs.colors.green700,
56     },
57     cancelButton: {
58         paddingRight: theme.spacing.unit * 2,
59         fontSize: '14px',
60         color: theme.customs.colors.red900,
61         "&:hover": {
62             cursor: 'pointer'
63         }
64     },
65     runButton: {
66         backgroundColor: theme.customs.colors.green700,
67         '&:hover': {
68             backgroundColor: theme.customs.colors.green800,
69         },
70         padding: "0px 5px 0 0",
71         marginRight: "5px",
72     },
73 });
74
75 export interface ProcessDetailsCardDataProps {
76     process: Process;
77     cancelProcess: (uuid: string) => void;
78     startProcess: (uuid: string) => void;
79     onContextMenu: (event: React.MouseEvent<HTMLElement>) => void;
80 }
81
82 type ProcessDetailsCardProps = ProcessDetailsCardDataProps & WithStyles<CssRules> & MPVPanelProps;
83
84 export const ProcessDetailsCard = withStyles(styles)(
85     ({ cancelProcess, startProcess, onContextMenu, classes, process, doHidePanel, panelName }: ProcessDetailsCardProps) => {
86         return <Card className={classes.card}>
87             <CardHeader
88                 className={classes.header}
89                 classes={{
90                     content: classes.title,
91                     avatar: classes.avatar,
92                 }}
93                 avatar={<ProcessIcon className={classes.iconHeader} />}
94                 title={
95                     <Tooltip title={process.containerRequest.name} placement="bottom-start">
96                         <Typography noWrap variant='h6'>
97                             {process.containerRequest.name}
98                         </Typography>
99                     </Tooltip>
100                 }
101                 subheader={
102                     <Tooltip title={getDescription(process)} placement="bottom-start">
103                         <Typography noWrap variant='body1' color='inherit'>
104                             {getDescription(process)}
105                         </Typography>
106                     </Tooltip>}
107                 action={
108                     <div>
109                         {process.containerRequest.state === ContainerRequestState.UNCOMMITTED &&
110                             <Button
111                                 variant="contained"
112                                 size="small"
113                                 color="primary"
114                                 className={classes.runButton}
115                                 onClick={() => startProcess(process.containerRequest.uuid)}>
116                                 <StartIcon />
117                                 Run Process
118                             </Button>}
119                         {process.container &&
120                             (process.container.state === ContainerState.QUEUED ||
121                             process.container.state === ContainerState.LOCKED ||
122                             process.container.state === ContainerState.RUNNING) &&
123                             process.containerRequest.priority !== null &&
124                             process.containerRequest.priority > 0 &&
125                             <span data-cy="process-cancel" className={classes.cancelButton} onClick={() => cancelProcess(process.containerRequest.uuid)}>Cancel</span>}
126                         <ProcessStatus uuid={process.containerRequest.uuid} />
127                         <Tooltip title="More options" disableFocusListener>
128                             <IconButton
129                                 aria-label="More options"
130                                 onClick={event => onContextMenu(event)}>
131                                 <MoreOptionsIcon />
132                             </IconButton>
133                         </Tooltip>
134                         { doHidePanel &&
135                         <Tooltip title={`Close ${panelName || 'panel'}`} disableFocusListener>
136                             <IconButton onClick={doHidePanel}><CloseIcon /></IconButton>
137                         </Tooltip> }
138                     </div>
139                 } />
140             <CardContent className={classes.content}>
141                 <ProcessDetailsAttributes request={process.containerRequest} twoCol hideProcessPanelRedundantFields />
142             </CardContent>
143         </Card>;
144     }
145 );
146
147 const getDescription = (process: Process) =>
148     process.containerRequest.description || '(no-description)';