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