Merge branch '16314-test-user-db'
[arvados.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 * as 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 { 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 * as 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 }
85
86 type ProcessInformationCardProps = ProcessInformationCardDataProps & WithStyles<CssRules, true>;
87
88 export const ProcessInformationCard = withStyles(styles, { withTheme: true })(
89     ({ classes, process, onContextMenu, theme, openProcessInputDialog, navigateToOutput, openWorkflow, cancelProcess }: ProcessInformationCardProps) => {
90         const { container } = process;
91         const startedAt = container ? formatDate(container.startedAt) : 'N/A';
92         const finishedAt = container ? formatDate(container.finishedAt) : 'N/A';
93         return <Card className={classes.card}>
94             <CardHeader
95                 classes={{
96                     content: classes.title,
97                     avatar: classes.avatar
98                 }}
99                 avatar={<ProcessIcon className={classes.iconHeader} />}
100                 action={
101                     <div>
102                         {process.container && process.container.state === ContainerState.RUNNING &&
103                             <span className={classes.cancelButton} onClick={() => cancelProcess(process.containerRequest.uuid)}>Cancel</span>}
104                         <Chip label={getProcessStatus(process)}
105                             className={classes.chip}
106                             style={{ backgroundColor: getProcessStatusColor(getProcessStatus(process), theme as ArvadosTheme) }} />
107                         <Tooltip title="More options" disableFocusListener>
108                             <IconButton
109                                 aria-label="More options"
110                                 onClick={event => onContextMenu(event)}>
111                                 <MoreOptionsIcon />
112                             </IconButton>
113                         </Tooltip>
114                     </div>
115                 }
116                 title={
117                     <Tooltip title={process.containerRequest.name} placement="bottom-start">
118                         <Typography noWrap variant='h6' color='inherit'>
119                             {process.containerRequest.name}
120                         </Typography>
121                     </Tooltip>
122                 }
123                 subheader={
124                     <Tooltip title={getDescription(process)} placement="bottom-start">
125                         <Typography noWrap variant='body1' color='inherit'>
126                             {getDescription(process)}
127                         </Typography>
128                     </Tooltip>} />
129             <CardContent className={classes.content}>
130                 <Grid container>
131                     <Grid item xs={6}>
132                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
133                             label='From'
134                             value={startedAt} />
135                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
136                             label='To'
137                             value={finishedAt} />
138                         {process.containerRequest.properties.workflowUuid &&
139                             <span onClick={() => openWorkflow(process.containerRequest.properties.workflowUuid)}>
140                                 <DetailsAttribute classLabel={classes.label} classValue={classNames(classes.value, classes.link)}
141                                     label='Workflow' value={process.containerRequest.properties.workflowName} />
142                             </span>}
143                     </Grid>
144                     <Grid item xs={6}>
145                         <span onClick={() => navigateToOutput(process.containerRequest.outputUuid!)}>
146                             <DetailsAttribute classLabel={classes.link} label='Outputs' />
147                         </span>
148                         <span onClick={() => openProcessInputDialog(process.containerRequest.uuid)}>
149                             <DetailsAttribute classLabel={classes.link} label='Inputs' />
150                         </span>
151                     </Grid>
152                 </Grid>
153             </CardContent>
154         </Card>;
155     }
156 );
157
158 const getDescription = (process: Process) =>
159     process.containerRequest.description || '(no-description)';