8f16db70c422f975b43950fe87e822b004f0812e
[arvados-workbench2.git] / src / views / process-panel / process-information-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, WithStyles, withStyles, Card,
8     CardHeader, IconButton, CardContent, Grid, Chip, Typography, Tooltip
9 } from '@material-ui/core';
10 import { ArvadosTheme } from 'common/custom-theme';
11 import { CloseIcon, MoreOptionsIcon, ProcessIcon } from 'components/icon/icon';
12 import { DetailsAttribute } from 'components/details-attribute/details-attribute';
13 import { Process } from 'store/processes/process';
14 import { getProcessStatus, getProcessStatusColor } from 'store/processes/process';
15 import { formatDate } from 'common/formatters';
16 import classNames from 'classnames';
17 import { ContainerState } from 'models/container';
18 import { MPVPanelProps } from 'components/multi-panel-view/multi-panel-view';
19 import { ProcessRuntimeStatus } from 'views-components/process-runtime-status/process-runtime-status';
20
21 type CssRules = 'card' | 'iconHeader' | 'label' | 'value' | 'chip' | 'link' | 'content' | 'title' | 'avatar' | 'cancelButton' | 'header';
22
23 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
24     card: {
25         height: '100%'
26     },
27     header: {
28         paddingTop: theme.spacing.unit,
29         paddingBottom: theme.spacing.unit,
30     },
31     iconHeader: {
32         fontSize: '1.875rem',
33         color: theme.customs.colors.green700,
34     },
35     avatar: {
36         alignSelf: 'flex-start',
37         paddingTop: theme.spacing.unit * 0.5
38     },
39     label: {
40         display: 'flex',
41         justifyContent: 'flex-start',
42         fontSize: '0.875rem',
43         marginRight: theme.spacing.unit * 3,
44         paddingRight: theme.spacing.unit
45     },
46     value: {
47         textTransform: 'none',
48         fontSize: '0.875rem',
49     },
50     link: {
51         fontSize: '0.875rem',
52         color: theme.palette.primary.main,
53         '&:hover': {
54             cursor: 'pointer'
55         }
56     },
57     chip: {
58         height: theme.spacing.unit * 3,
59         width: theme.spacing.unit * 12,
60         color: theme.palette.common.white,
61         fontSize: '0.875rem',
62         borderRadius: theme.spacing.unit * 0.625,
63     },
64     content: {
65         paddingTop: '0px',
66         paddingLeft: theme.spacing.unit * 1,
67         paddingRight: theme.spacing.unit * 1,
68         '&:last-child': {
69             paddingBottom: theme.spacing.unit * 1,
70         }
71     },
72     title: {
73         overflow: 'hidden',
74         paddingTop: theme.spacing.unit * 0.5
75     },
76     cancelButton: {
77         paddingRight: theme.spacing.unit * 2,
78         fontSize: '14px',
79         color: theme.customs.colors.red900,
80         "&:hover": {
81             cursor: 'pointer'
82         }
83     }
84 });
85
86 export interface ProcessInformationCardDataProps {
87     process: Process;
88     onContextMenu: (event: React.MouseEvent<HTMLElement>) => void;
89     openProcessInputDialog: (uuid: string) => void;
90     navigateToOutput: (uuid: string) => void;
91     openWorkflow: (uuid: string) => void;
92     cancelProcess: (uuid: string) => void;
93 }
94
95 type ProcessInformationCardProps = ProcessInformationCardDataProps & WithStyles<CssRules, true> & MPVPanelProps;
96
97 export const ProcessInformationCard = withStyles(styles, { withTheme: true })(
98     ({ classes, process, onContextMenu, theme, openProcessInputDialog, navigateToOutput, openWorkflow, cancelProcess, doHidePanel, panelName }: ProcessInformationCardProps) => {
99         const { container } = process;
100         const startedAt = container ? formatDate(container.startedAt) : 'N/A';
101         const finishedAt = container ? formatDate(container.finishedAt) : 'N/A';
102         return <Card className={classes.card}>
103             <CardHeader
104                 className={classes.header}
105                 classes={{
106                     content: classes.title,
107                     avatar: classes.avatar
108                 }}
109                 avatar={<ProcessIcon className={classes.iconHeader} />}
110                 action={
111                     <div>
112                         {process.container && process.container.state === ContainerState.RUNNING &&
113                             <span className={classes.cancelButton} onClick={() => cancelProcess(process.containerRequest.uuid)}>Cancel</span>}
114                         <Chip label={getProcessStatus(process)}
115                             className={classes.chip}
116                             style={{ backgroundColor: getProcessStatusColor(getProcessStatus(process), theme as ArvadosTheme) }} />
117                         <Tooltip title="More options" disableFocusListener>
118                             <IconButton
119                                 aria-label="More options"
120                                 onClick={event => onContextMenu(event)}>
121                                 <MoreOptionsIcon />
122                             </IconButton>
123                         </Tooltip>
124                         { doHidePanel &&
125                         <Tooltip title={`Close ${panelName || 'panel'}`} disableFocusListener>
126                             <IconButton onClick={doHidePanel}><CloseIcon /></IconButton>
127                         </Tooltip> }
128                     </div>
129                 }
130                 title={ !!process.containerRequest.name &&
131                     <Typography noWrap variant='h6' color='inherit'>
132                         {process.containerRequest.name}
133                     </Typography>
134                 }
135                 subheader={
136                     <Typography noWrap variant='body1' color='inherit'>
137                         {process.containerRequest.description}
138                     </Typography>
139                 }
140             />
141             <CardContent className={classes.content}>
142                 <Grid container>
143                     <Grid item xs={12}>
144                         <ProcessRuntimeStatus runtimeStatus={process.container?.runtimeStatus} />
145                     </Grid>
146                     <Grid item xs={6}>
147                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
148                             label='Started at'
149                             value={startedAt} />
150                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
151                             label='Finished at'
152                             value={finishedAt} />
153                         {process.containerRequest.properties.workflowUuid &&
154                             <span onClick={() => openWorkflow(process.containerRequest.properties.workflowUuid)}>
155                                 <DetailsAttribute classLabel={classes.label} classValue={classNames(classes.value, classes.link)}
156                                     label='Workflow' value={process.containerRequest.properties.workflowName} />
157                             </span>}
158                     </Grid>
159                     <Grid item xs={6}>
160                         <span onClick={() => navigateToOutput(process.containerRequest.outputUuid!)}>
161                             <DetailsAttribute classLabel={classes.link} label='Outputs' />
162                         </span>
163                         <span onClick={() => openProcessInputDialog(process.containerRequest.uuid)}>
164                             <DetailsAttribute classLabel={classes.link} label='Inputs' />
165                         </span>
166                     </Grid>
167                 </Grid>
168             </CardContent>
169         </Card>;
170     }
171 );