15781: Renders multi-value properties as multiple chips.
[arvados-workbench2.git] / src / views / collection-panel / collection-panel.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, Tooltip
9 } from '@material-ui/core';
10 import { connect, DispatchProp } from "react-redux";
11 import { RouteComponentProps } from 'react-router';
12 import { ArvadosTheme } from '~/common/custom-theme';
13 import { RootState } from '~/store/store';
14 import { MoreOptionsIcon, CollectionIcon } from '~/components/icon/icon';
15 import { DetailsAttribute } from '~/components/details-attribute/details-attribute';
16 import { CollectionResource } from '~/models/collection';
17 import { CollectionPanelFiles } from '~/views-components/collection-panel-files/collection-panel-files';
18 import { CollectionTagForm } from './collection-tag-form';
19 import { deleteCollectionTag, navigateToProcess } from '~/store/collection-panel/collection-panel-action';
20 import { getResource } from '~/store/resources/resources';
21 import { openContextMenu } from '~/store/context-menu/context-menu-actions';
22 import { ContextMenuKind } from '~/views-components/context-menu/context-menu';
23 import { formatFileSize } from "~/common/formatters";
24 import { openDetailsPanel } from '~/store/details-panel/details-panel-action';
25 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
26 import { PropertyChipComponent } from '~/views-components/resource-properties-form/property-chip';
27 import { IllegalNamingWarning } from '~/components/warning/warning';
28
29 type CssRules = 'card' | 'iconHeader' | 'tag' | 'label' | 'value' | 'link';
30
31 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
32     card: {
33         marginBottom: theme.spacing.unit * 2
34     },
35     iconHeader: {
36         fontSize: '1.875rem',
37         color: theme.customs.colors.yellow700
38     },
39     tag: {
40         marginRight: theme.spacing.unit,
41         marginBottom: theme.spacing.unit
42     },
43     label: {
44         fontSize: '0.875rem'
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 });
58
59 interface CollectionPanelDataProps {
60     item: CollectionResource;
61 }
62
63 type CollectionPanelProps = CollectionPanelDataProps & DispatchProp
64     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
65
66 export const CollectionPanel = withStyles(styles)(
67     connect((state: RootState, props: RouteComponentProps<{ id: string }>) => {
68         const item = getResource(props.match.params.id)(state.resources);
69         return { item };
70     })(
71         class extends React.Component<CollectionPanelProps> {
72
73             render() {
74                 const { classes, item, dispatch } = this.props;
75                 return item
76                     ? <>
77                         <Card className={classes.card}>
78                             <CardHeader
79                                 avatar={
80                                     <IconButton onClick={this.openCollectionDetails}>
81                                         <CollectionIcon className={classes.iconHeader} />
82                                     </IconButton>
83                                 }
84                                 action={
85                                     <Tooltip title="More options" disableFocusListener>
86                                         <IconButton
87                                             aria-label="More options"
88                                             onClick={this.handleContextMenu}>
89                                             <MoreOptionsIcon />
90                                         </IconButton>
91                                     </Tooltip>
92                                 }
93                                 title={item && <span><IllegalNamingWarning name={item.name}/>{item.name}</span>}
94                                 titleTypographyProps={this.titleProps}
95                                 subheader={item && item.description}
96                                 subheaderTypographyProps={this.titleProps} />
97                             <CardContent>
98                                 <Grid container direction="column">
99                                     <Grid item xs={10}>
100                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
101                                             label='Collection UUID'
102                                             linkToUuid={item && item.uuid} />
103                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
104                                             label='Portable data hash'
105                                             linkToUuid={item && item.portableDataHash} />
106                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
107                                             label='Number of files' value={item && item.fileCount} />
108                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
109                                             label='Content size' value={item && formatFileSize(item.fileSizeTotal)} />
110                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
111                                             label='Owner' linkToUuid={item && item.ownerUuid} />
112                                         {(item.properties.container_request || item.properties.containerRequest) &&
113                                             <span onClick={() => dispatch<any>(navigateToProcess(item.properties.container_request || item.properties.containerRequest))}>
114                                                 <DetailsAttribute classLabel={classes.link} label='Link to process' />
115                                             </span>
116                                         }
117                                     </Grid>
118                                 </Grid>
119                             </CardContent>
120                         </Card>
121
122                         <Card className={classes.card}>
123                             <CardHeader title="Properties" />
124                             <CardContent>
125                                 <Grid container direction="column">
126                                     <Grid item xs={12}>
127                                         <CollectionTagForm />
128                                     </Grid>
129                                     <Grid item xs={12}>
130                                         {Object.keys(item.properties).map(k =>
131                                             Array.isArray(item.properties[k])
132                                             ? item.properties[k].map((v: string) =>
133                                                 getPropertyChip(k, v, this.handleDelete, classes.tag))
134                                             : getPropertyChip(k, item.properties[k], this.handleDelete, classes.tag)
135                                         )}
136                                     </Grid>
137                                 </Grid>
138                             </CardContent>
139                         </Card>
140                         <div className={classes.card}>
141                             <CollectionPanelFiles />
142                         </div>
143                     </>
144                     : null;
145             }
146
147             handleContextMenu = (event: React.MouseEvent<any>) => {
148                 const { uuid, ownerUuid, name, description, kind, isTrashed } = this.props.item;
149                 const resource = {
150                     uuid,
151                     ownerUuid,
152                     name,
153                     description,
154                     kind,
155                     menuKind: isTrashed
156                         ? ContextMenuKind.TRASHED_COLLECTION
157                         : ContextMenuKind.COLLECTION
158                 };
159                 this.props.dispatch<any>(openContextMenu(event, resource));
160             }
161
162             onCopy = (message: string) =>
163                 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
164                     message,
165                     hideDuration: 2000,
166                     kind: SnackbarKind.SUCCESS
167                 }))
168
169             handleDelete = (key: string, value: string) => () => {
170                 this.props.dispatch<any>(deleteCollectionTag(key, value));
171             }
172
173             openCollectionDetails = () => {
174                 const { item } = this.props;
175                 if (item) {
176                     this.props.dispatch(openDetailsPanel(item.uuid));
177                 }
178             }
179
180             titleProps = {
181                 onClick: this.openCollectionDetails
182             };
183
184         }
185     )
186 );
187
188 const getPropertyChip = (k:string, v:string, handleDelete:any, className:string) =>
189     <PropertyChipComponent
190         key={k} className={className}
191         onDelete={handleDelete(k, v)}
192         propKey={k} propValue={v} />;
193