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