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