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