Merge branch 'master' of git.curoverse.com:arvados-workbench2 into 14163-trash-loadin...
[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 <div>
74                     <Card className={classes.card}>
75                         <CardHeader
76                             avatar={<CollectionIcon className={classes.iconHeader} />}
77                             action={
78                                 <Tooltip title="More options">
79                                     <IconButton
80                                         aria-label="More options"
81                                         onClick={this.handleContextMenu}>
82                                         <MoreOptionsIcon />
83                                     </IconButton>
84                                 </Tooltip>
85                             }
86                             title={item && item.name}
87                             subheader={item && item.description} />
88                         <CardContent>
89                             <Grid container direction="column">
90                                 <Grid item xs={6}>
91                                     <DetailsAttribute classLabel={classes.label} classValue={classes.value}
92                                         label='Collection UUID'
93                                         value={item && item.uuid}>
94                                         <Tooltip title="Copy uuid">
95                                             <CopyToClipboard text={item && item.uuid} onCopy={() => this.onCopy()}>
96                                                 <CopyIcon className={classes.copyIcon} />
97                                             </CopyToClipboard>
98                                         </Tooltip>
99                                     </DetailsAttribute>
100                                     <DetailsAttribute classLabel={classes.label} classValue={classes.value}
101                                         label='Number of files' value='14' />
102                                     <DetailsAttribute classLabel={classes.label} classValue={classes.value}
103                                         label='Content size' value='54 MB' />
104                                     <DetailsAttribute classLabel={classes.label} classValue={classes.value}
105                                         label='Owner' value={item && item.ownerUuid} />
106                                 </Grid>
107                             </Grid>
108                         </CardContent>
109                     </Card>
110
111                     <Card className={classes.card}>
112                         <CardHeader title="Properties" />
113                         <CardContent>
114                             <Grid container direction="column">
115                                 <Grid item xs={12}><CollectionTagForm /></Grid>
116                                 <Grid item xs={12}>
117                                     {
118                                         Object.keys(item.properties).map( key => {
119                                             return <Chip key={key} className={classes.tag}
120                                                 onDelete={this.handleDelete(key)}
121                                                 label={`${key}: ${item.properties[key]}`} />;
122                                         })
123                                     }
124                                 </Grid>
125                             </Grid>
126                         </CardContent>
127                     </Card>
128                     <div className={classes.card}>
129                         <CollectionPanelFiles />
130                     </div>
131                 </div>;
132             }
133
134             handleContextMenu = (event: React.MouseEvent<any>) => {
135                 const { uuid, ownerUuid, name, description, kind } = this.props.item;
136                 const resource = {
137                     uuid,
138                     ownerUuid,
139                     name,
140                     description,
141                     kind,
142                     menuKind: ContextMenuKind.COLLECTION
143                 };
144                 this.props.dispatch<any>(openContextMenu(event, resource));
145             }
146
147             handleDelete = (key: string) => () => {
148                 this.props.dispatch<any>(deleteCollectionTag(key));
149             }
150
151             onCopy = () => {
152                 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
153                     message: "Uuid has been copied",
154                     hideDuration: 2000
155                 }));
156             }
157         }
158     )
159 );