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