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