Merge branch 'master' into 13988-make-a-copy-popup
[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 { 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, CopyIcon } 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 * as CopyToClipboard from 'react-copy-to-clipboard';
19 import { TagResource } from '~/models/tag';
20 import { CollectionTagForm } from './collection-tag-form';
21 import { deleteCollectionTag } from '~/store/collection-panel/collection-panel-action';
22 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
23
24 type CssRules = 'card' | 'iconHeader' | 'tag' | 'copyIcon' | 'label' | 'value';
25
26 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
27     card: {
28         marginBottom: theme.spacing.unit * 2
29     },
30     iconHeader: {
31         fontSize: '1.875rem',
32         color: theme.customs.colors.yellow700
33     },
34     tag: {
35         marginRight: theme.spacing.unit,
36         marginBottom: theme.spacing.unit
37     },
38     copyIcon: {
39         marginLeft: theme.spacing.unit,
40         fontSize: '1.125rem',
41         color: theme.palette.grey["500"],
42         cursor: 'pointer'
43     },
44     label: {
45         fontSize: '0.875rem'
46     },
47     value: {
48         textTransform: 'none',
49         fontSize: '0.875rem'
50     }
51 });
52
53 interface CollectionPanelDataProps {
54     item: CollectionResource;
55     tags: TagResource[];
56 }
57
58 interface CollectionPanelActionProps {
59     onItemRouteChange: (collectionId: string) => void;
60     onContextMenu: (event: React.MouseEvent<HTMLElement>, item: CollectionResource) => void;
61 }
62
63 type CollectionPanelProps = CollectionPanelDataProps & CollectionPanelActionProps & DispatchProp
64                             & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
65
66
67 export const CollectionPanel = withStyles(styles)(
68     connect((state: RootState) => ({
69         item: state.collectionPanel.item,
70         tags: state.collectionPanel.tags
71     }))(
72         class extends React.Component<CollectionPanelProps> {
73
74             render() {
75                 const { classes, item, tags, onContextMenu } = this.props;
76                 return <div>
77                         <Card className={classes.card}>
78                             <CardHeader
79                                 avatar={ <CollectionIcon className={classes.iconHeader} /> }
80                                 action={
81                                     <IconButton
82                                         aria-label="More options"
83                                         onClick={event => onContextMenu(event, item)}>
84                                         <MoreOptionsIcon />
85                                     </IconButton>
86                                 }
87                                 title={item && item.name }
88                                 subheader={item && item.description} />
89                             <CardContent>
90                                 <Grid container direction="column">
91                                     <Grid item xs={6}>
92                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
93                                                 label='Collection UUID'
94                                                 value={item && item.uuid}>
95                                             <Tooltip title="Copy uuid">
96                                                 <CopyToClipboard text={item && item.uuid} onCopy={() => this.onCopy() }>
97                                                     <CopyIcon className={classes.copyIcon} />
98                                                 </CopyToClipboard>
99                                             </Tooltip>
100                                         </DetailsAttribute>
101                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value} 
102                                             label='Number of files' value='14' />
103                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value} 
104                                             label='Content size' value='54 MB' />
105                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value} 
106                                             label='Owner' value={item && item.ownerUuid} />
107                                     </Grid>
108                                 </Grid>
109                             </CardContent>
110                         </Card>
111
112                         <Card className={classes.card}>
113                             <CardHeader title="Properties" />
114                             <CardContent>
115                                 <Grid container direction="column">
116                                     <Grid item xs={12}><CollectionTagForm /></Grid>
117                                     <Grid item xs={12}>
118                                         {
119                                             tags.map(tag => {
120                                                 return <Chip key={tag.etag} className={classes.tag}
121                                                     onDelete={this.handleDelete(tag.uuid)}
122                                                     label={renderTagLabel(tag)}  />;
123                                             })
124                                         }
125                                     </Grid>
126                                 </Grid>
127                             </CardContent>
128                         </Card>
129                         <div className={classes.card}>
130                             <CollectionPanelFiles/>
131                         </div>
132                     </div>;
133             }
134
135             handleDelete = (uuid: string) => () => {
136                 this.props.dispatch<any>(deleteCollectionTag(uuid));
137             }
138
139             onCopy = () => {
140                 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
141                     message: "Uuid has been copied",
142                     hideDuration: 2000
143                 }));
144             }
145
146             componentWillReceiveProps({ match, item, onItemRouteChange }: CollectionPanelProps) {
147                 if (!item || match.params.id !== item.uuid) {
148                     onItemRouteChange(match.params.id);
149                 }
150             }
151
152         }
153     )
154 );
155
156 const renderTagLabel = (tag: TagResource) => {
157     const { properties } = tag;
158     return `${properties.key}: ${properties.value}`;
159 };