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