Merge branch '15067-tag-editing-by-ids'
[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, 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 } 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 { CollectionTagForm } from './collection-tag-form';
19 import { deleteCollectionTag, navigateToProcess } from '~/store/collection-panel/collection-panel-action';
20 import { getResource } from '~/store/resources/resources';
21 import { openContextMenu } from '~/store/context-menu/context-menu-actions';
22 import { ContextMenuKind } from '~/views-components/context-menu/context-menu';
23 import { formatFileSize } from "~/common/formatters";
24 import { getResourceData } from "~/store/resources-data/resources-data";
25 import { ResourceData } from "~/store/resources-data/resources-data-reducer";
26 import { openDetailsPanel } from '~/store/details-panel/details-panel-action';
27 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
28 import { PropertyChipComponent } from '~/views-components/resource-properties-form/property-chip';
29
30 type CssRules = 'card' | 'iconHeader' | 'tag' | '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     label: {
45         fontSize: '0.875rem'
46     },
47     value: {
48         textTransform: 'none',
49         fontSize: '0.875rem'
50     },
51     link: {
52         fontSize: '0.875rem',
53         color: theme.palette.primary.main,
54         '&:hover': {
55             cursor: 'pointer'
56         }
57     }
58 });
59
60 interface CollectionPanelDataProps {
61     item: CollectionResource;
62     data: ResourceData;
63 }
64
65 type CollectionPanelProps = CollectionPanelDataProps & DispatchProp
66     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
67
68 export const CollectionPanel = withStyles(styles)(
69         connect((state: RootState, props: RouteComponentProps<{ id: string }>) => {
70             const item = getResource(props.match.params.id)(state.resources);
71             const data = getResourceData(props.match.params.id)(state.resourcesData);
72             return { item, data };
73         })(
74         class extends React.Component<CollectionPanelProps> {
75
76             render() {
77                 const { classes, item, data, dispatch } = this.props;
78                 return item
79                     ? <>
80                         <Card className={classes.card}>
81                             <CardHeader
82                                 avatar={
83                                     <IconButton onClick={this.openCollectionDetails}>
84                                         <CollectionIcon className={classes.iconHeader} />
85                                     </IconButton>
86                                 }
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                                 titleTypographyProps={this.titleProps}
98                                 subheader={item && item.description}
99                                 subheaderTypographyProps={this.titleProps} />
100                             <CardContent>
101                                 <Grid container direction="column">
102                                     <Grid item xs={10}>
103                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
104                                             label='Collection UUID'
105                                             linkToUuid={item && item.uuid} />
106                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
107                                             label='Portable data hash'
108                                             linkToUuid={item && item.portableDataHash} />
109                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
110                                             label='Number of files' value={data && data.fileCount} />
111                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
112                                             label='Content size' value={data && formatFileSize(data.fileSize)} />
113                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
114                                             label='Owner' linkToUuid={item && item.ownerUuid} />
115                                         {(item.properties.container_request || item.properties.containerRequest) &&
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                                         }
120                                     </Grid>
121                                 </Grid>
122                             </CardContent>
123                         </Card>
124
125                         <Card className={classes.card}>
126                             <CardHeader title="Properties" />
127                             <CardContent>
128                                 <Grid container direction="column">
129                                     <Grid item xs={12}>
130                                         <CollectionTagForm />
131                                     </Grid>
132                                     <Grid item xs={12}>
133                                         {Object.keys(item.properties).map(k =>
134                                             <PropertyChipComponent
135                                                 key={k} className={classes.tag}
136                                                 onDelete={this.handleDelete(k)}
137                                                 propKey={k} propValue={item.properties[k]} />
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             onCopy = (message: string) =>
166                 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
167                     message,
168                     hideDuration: 2000,
169                     kind: SnackbarKind.SUCCESS
170                 }))
171
172             handleDelete = (key: string) => () => {
173                 this.props.dispatch<any>(deleteCollectionTag(key));
174             }
175
176             openCollectionDetails = () => {
177                 const { item } = this.props;
178                 if (item) {
179                     this.props.dispatch(openDetailsPanel(item.uuid));
180                 }
181             }
182
183             titleProps = {
184                 onClick: this.openCollectionDetails
185             };
186
187         }
188     )
189 );