27a685410116fb238e44c50f7a2808aef142b1d2
[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, ReadOnlyIcon } 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 { getPropertyChip } from '~/views-components/resource-properties-form/property-chip';
27 import { IllegalNamingWarning } from '~/components/warning/warning';
28 import { GroupResource } from '~/models/group';
29 import { UserResource } from '~/models/user';
30 import { getUserUuid } from '~/common/getuser';
31 import { getProgressIndicator } from '~/store/progress-indicator/progress-indicator-reducer';
32 import { COLLECTION_PANEL_LOAD_FILES } from '~/store/collection-panel/collection-panel-files/collection-panel-files-actions';
33
34 type CssRules = 'card' | 'iconHeader' | 'tag' | 'label' | 'value' | 'link' | 'centeredLabel' | 'readOnlyIcon';
35
36 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
37     card: {
38         marginBottom: theme.spacing.unit * 2
39     },
40     iconHeader: {
41         fontSize: '1.875rem',
42         color: theme.customs.colors.yellow700
43     },
44     tag: {
45         marginRight: theme.spacing.unit,
46         marginBottom: theme.spacing.unit
47     },
48     label: {
49         fontSize: '0.875rem'
50     },
51     centeredLabel: {
52         fontSize: '0.875rem',
53         textAlign: 'center'
54     },
55     value: {
56         textTransform: 'none',
57         fontSize: '0.875rem'
58     },
59     link: {
60         fontSize: '0.875rem',
61         color: theme.palette.primary.main,
62         '&:hover': {
63             cursor: 'pointer'
64         }
65     },
66     readOnlyIcon: {
67         marginLeft: theme.spacing.unit,
68         fontSize: 'small',
69     }
70 });
71
72 interface CollectionPanelDataProps {
73     item: CollectionResource;
74     isWritable: boolean;
75     isLoadingFiles: boolean;
76 }
77
78 type CollectionPanelProps = CollectionPanelDataProps & DispatchProp
79     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
80
81 export const CollectionPanel = withStyles(styles)(
82     connect((state: RootState, props: RouteComponentProps<{ id: string }>) => {
83         const currentUserUUID = getUserUuid(state);
84         const item = getResource<CollectionResource>(props.match.params.id)(state.resources);
85         let isWritable = false;
86         if (item && item.ownerUuid === currentUserUUID) {
87             isWritable = true;
88         } else if (item) {
89             const itemOwner = getResource<GroupResource|UserResource>(item.ownerUuid)(state.resources);
90             if (itemOwner) {
91                 isWritable = itemOwner.writableBy.indexOf(currentUserUUID || '') >= 0;
92             }
93         }
94         const loadingFilesIndicator = getProgressIndicator(COLLECTION_PANEL_LOAD_FILES)(state.progressIndicator);
95         const isLoadingFiles = loadingFilesIndicator && loadingFilesIndicator!.working || false;
96         return { item, isWritable, isLoadingFiles };
97     })(
98         class extends React.Component<CollectionPanelProps> {
99             render() {
100                 const { classes, item, dispatch, isWritable, isLoadingFiles } = this.props;
101                 return item
102                     ? <>
103                         <Card data-cy='collection-info-panel' className={classes.card}>
104                             <CardHeader
105                                 avatar={
106                                     <IconButton onClick={this.openCollectionDetails}>
107                                         <CollectionIcon className={classes.iconHeader} />
108                                     </IconButton>
109                                 }
110                                 action={
111                                     <Tooltip title="More options" disableFocusListener>
112                                         <IconButton
113                                             data-cy='collection-panel-options-btn'
114                                             aria-label="More options"
115                                             onClick={this.handleContextMenu}>
116                                             <MoreOptionsIcon />
117                                         </IconButton>
118                                     </Tooltip>
119                                 }
120                                 title={
121                                     <span>
122                                         <IllegalNamingWarning name={item.name}/>
123                                         {item.name}
124                                         {isWritable ||
125                                         <Tooltip title="Read-only">
126                                             <ReadOnlyIcon data-cy="read-only-icon" className={classes.readOnlyIcon} />
127                                         </Tooltip>
128                                         }
129                                     </span>
130                                 }
131                                 titleTypographyProps={this.titleProps}
132                                 subheader={item.description}
133                                 subheaderTypographyProps={this.titleProps} />
134                             <CardContent>
135                                 <Grid container direction="column">
136                                     <Grid item xs={10}>
137                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
138                                             label='Collection UUID'
139                                             linkToUuid={item.uuid} />
140                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
141                                             label='Portable data hash'
142                                             linkToUuid={item.portableDataHash} />
143                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
144                                             label='Number of files' value={item.fileCount} />
145                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
146                                             label='Content size' value={formatFileSize(item.fileSizeTotal)} />
147                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
148                                             label='Owner' linkToUuid={item.ownerUuid} />
149                                         {(item.properties.container_request || item.properties.containerRequest) &&
150                                             <span onClick={() => dispatch<any>(navigateToProcess(item.properties.container_request || item.properties.containerRequest))}>
151                                                 <DetailsAttribute classLabel={classes.link} label='Link to process' />
152                                             </span>
153                                         }
154                                     </Grid>
155                                 </Grid>
156                             </CardContent>
157                         </Card>
158
159                         <Card data-cy='collection-properties-panel' className={classes.card}>
160                             <CardHeader title="Properties" />
161                             <CardContent>
162                                 <Grid container direction="column">
163                                     {isWritable && <Grid item xs={12}>
164                                         <CollectionTagForm />
165                                     </Grid>}
166                                     <Grid item xs={12}>
167                                     { Object.keys(item.properties).length > 0
168                                         ? Object.keys(item.properties).map(k =>
169                                             Array.isArray(item.properties[k])
170                                             ? item.properties[k].map((v: string) =>
171                                                 getPropertyChip(
172                                                     k, v,
173                                                     isWritable
174                                                         ? this.handleDelete(k, item.properties[k])
175                                                         : undefined,
176                                                     classes.tag))
177                                             : getPropertyChip(
178                                                 k, item.properties[k],
179                                                 isWritable
180                                                     ? this.handleDelete(k, item.properties[k])
181                                                     : undefined,
182                                                 classes.tag)
183                                         )
184                                         : <div className={classes.centeredLabel}>No properties set on this collection.</div>
185                                     }
186                                     </Grid>
187                                 </Grid>
188                             </CardContent>
189                         </Card>
190                         <div className={classes.card}>
191                             <CollectionPanelFiles isWritable={isWritable} isLoading={isLoadingFiles} />
192                         </div>
193                     </>
194                     : null;
195             }
196
197             handleContextMenu = (event: React.MouseEvent<any>) => {
198                 const { uuid, ownerUuid, name, description, kind, isTrashed } = this.props.item;
199                 const { isWritable } = this.props;
200                 const resource = {
201                     uuid,
202                     ownerUuid,
203                     name,
204                     description,
205                     kind,
206                     menuKind: isWritable
207                         ? isTrashed
208                             ? ContextMenuKind.TRASHED_COLLECTION
209                             : ContextMenuKind.COLLECTION
210                         : ContextMenuKind.READONLY_COLLECTION
211                 };
212                 this.props.dispatch<any>(openContextMenu(event, resource));
213             }
214
215             onCopy = (message: string) =>
216                 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
217                     message,
218                     hideDuration: 2000,
219                     kind: SnackbarKind.SUCCESS
220                 }))
221
222             handleDelete = (key: string, value: string) => () => {
223                 this.props.dispatch<any>(deleteCollectionTag(key, value));
224             }
225
226             openCollectionDetails = () => {
227                 const { item } = this.props;
228                 if (item) {
229                     this.props.dispatch(openDetailsPanel(item.uuid));
230                 }
231             }
232
233             titleProps = {
234                 onClick: this.openCollectionDetails
235             };
236
237         }
238     )
239 );