Merge branch '14278-unexpected-behavior-with-property-tags'
[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 { CollectionTagForm } from './collection-tag-form';
20 import { deleteCollectionTag } from '~/store/collection-panel/collection-panel-action';
21 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
22 import { getResource } from '~/store/resources/resources';
23 import { openContextMenu } from '~/store/context-menu/context-menu-actions';
24 import { ContextMenuKind } from '~/views-components/context-menu/context-menu';
25
26 type CssRules = 'card' | 'iconHeader' | 'tag' | 'copyIcon' | 'label' | 'value';
27
28 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
29     card: {
30         marginBottom: theme.spacing.unit * 2
31     },
32     iconHeader: {
33         fontSize: '1.875rem',
34         color: theme.customs.colors.yellow700
35     },
36     tag: {
37         marginRight: theme.spacing.unit,
38         marginBottom: theme.spacing.unit
39     },
40     copyIcon: {
41         marginLeft: theme.spacing.unit,
42         fontSize: '1.125rem',
43         color: theme.palette.grey["500"],
44         cursor: 'pointer'
45     },
46     label: {
47         fontSize: '0.875rem'
48     },
49     value: {
50         textTransform: 'none',
51         fontSize: '0.875rem'
52     }
53 });
54
55 interface CollectionPanelDataProps {
56     item: CollectionResource;
57 }
58
59 type CollectionPanelProps = CollectionPanelDataProps & DispatchProp
60     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
61
62
63 export const CollectionPanel = withStyles(styles)(
64     connect((state: RootState, props: RouteComponentProps<{ id: string }>) => {
65         const collection = getResource(props.match.params.id)(state.resources);
66         return {
67             item: collection
68         };
69     })(
70         class extends React.Component<CollectionPanelProps> {
71             render() {
72                 const { classes, item } = this.props;
73                 return item
74                     ? <>
75                         <Card className={classes.card}>
76                             <CardHeader
77                                 avatar={<CollectionIcon className={classes.iconHeader} />}
78                                 action={
79                                     <Tooltip title="More options">
80                                         <IconButton
81                                             aria-label="More options"
82                                             onClick={this.handleContextMenu}>
83                                             <MoreOptionsIcon />
84                                         </IconButton>
85                                     </Tooltip>
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}>
117                                         <CollectionTagForm />
118                                     </Grid>
119                                     <Grid item xs={12}>
120                                         {
121                                             Object.keys(item.properties).map(k => {
122                                                 return <Chip key={k} className={classes.tag}
123                                                     onDelete={this.handleDelete(k)}
124                                                     label={`${k}: ${item.properties[k]}`} />;
125                                             })
126                                         }
127                                     </Grid>
128                                 </Grid>
129                             </CardContent>
130                         </Card>
131                         <div className={classes.card}>
132                             <CollectionPanelFiles />
133                         </div>
134                     </>
135                     : null;
136             }
137
138             handleContextMenu = (event: React.MouseEvent<any>) => {
139                 const { uuid, ownerUuid, name, description, kind, isTrashed } = this.props.item;
140                 const resource = {
141                     uuid,
142                     ownerUuid,
143                     name,
144                     description,
145                     kind,
146                     menuKind: isTrashed
147                         ? ContextMenuKind.TRASHED_COLLECTION
148                         : ContextMenuKind.COLLECTION
149                 };
150                 this.props.dispatch<any>(openContextMenu(event, resource));
151             }
152
153             handleDelete = (key: string) => () => {
154                 this.props.dispatch<any>(deleteCollectionTag(key));
155             }
156
157             onCopy = () => {
158                 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
159                     message: "Uuid has been copied",
160                     hideDuration: 2000
161                 }));
162             }
163         }
164     )
165 );