16719: Enhances collection 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,
8     IconButton, Grid, Tooltip, Typography, ExpansionPanel,
9     ExpansionPanelSummary, ExpansionPanelDetails
10 } from '@material-ui/core';
11 import { connect, DispatchProp } from "react-redux";
12 import { RouteComponentProps } from 'react-router';
13 import { ArvadosTheme } from '~/common/custom-theme';
14 import { RootState } from '~/store/store';
15 import { MoreOptionsIcon, CollectionIcon, ReadOnlyIcon, ExpandIcon, CollectionOldVersionIcon } from '~/components/icon/icon';
16 import { DetailsAttribute } from '~/components/details-attribute/details-attribute';
17 import { CollectionResource } from '~/models/collection';
18 import { CollectionPanelFiles } from '~/views-components/collection-panel-files/collection-panel-files';
19 import { CollectionTagForm } from './collection-tag-form';
20 import { deleteCollectionTag, navigateToProcess, collectionPanelActions } from '~/store/collection-panel/collection-panel-action';
21 import { getResource } from '~/store/resources/resources';
22 import { openContextMenu } from '~/store/context-menu/context-menu-actions';
23 import { ContextMenuKind } from '~/views-components/context-menu/context-menu';
24 import { formatDate, formatFileSize } from "~/common/formatters";
25 import { openDetailsPanel } from '~/store/details-panel/details-panel-action';
26 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
27 import { getPropertyChip } from '~/views-components/resource-properties-form/property-chip';
28 import { IllegalNamingWarning } from '~/components/warning/warning';
29 import { GroupResource } from '~/models/group';
30 import { UserResource } from '~/models/user';
31 import { getUserUuid } from '~/common/getuser';
32 import { getProgressIndicator } from '~/store/progress-indicator/progress-indicator-reducer';
33 import { COLLECTION_PANEL_LOAD_FILES, loadCollectionFiles, COLLECTION_PANEL_LOAD_FILES_THRESHOLD } from '~/store/collection-panel/collection-panel-files/collection-panel-files-actions';
34
35 type CssRules = 'root'
36     | 'filesCard'
37     | 'iconHeader'
38     | 'tag'
39     | 'label'
40     | 'value'
41     | 'link'
42     | 'centeredLabel'
43     | 'warningLabel'
44     | 'collectionName'
45     | 'readOnlyIcon';
46
47 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
48     root: {
49         display: 'flex',
50         flexFlow: 'column',
51         height: 'calc(100vh - 130px)', // (100% viewport height) - (top bar + breadcrumbs)
52     },
53     filesCard: {
54         marginBottom: theme.spacing.unit * 2,
55         flex: 1,
56     },
57     iconHeader: {
58         fontSize: '1.875rem',
59         color: theme.customs.colors.yellow700
60     },
61     tag: {
62         marginRight: theme.spacing.unit,
63         marginBottom: theme.spacing.unit
64     },
65     label: {
66         fontSize: '0.875rem'
67     },
68     centeredLabel: {
69         fontSize: '0.875rem',
70         textAlign: 'center'
71     },
72     warningLabel: {
73         fontStyle: 'italic'
74     },
75     collectionName: {
76         flexDirection: 'column',
77     },
78     value: {
79         textTransform: 'none',
80         fontSize: '0.875rem'
81     },
82     link: {
83         fontSize: '0.875rem',
84         color: theme.palette.primary.main,
85         '&:hover': {
86             cursor: 'pointer'
87         }
88     },
89     readOnlyIcon: {
90         marginLeft: theme.spacing.unit,
91         fontSize: 'small',
92     }
93 });
94
95 interface CollectionPanelDataProps {
96     item: CollectionResource;
97     isWritable: boolean;
98     isOldVersion: boolean;
99     isLoadingFiles: boolean;
100     tooManyFiles: boolean;
101 }
102
103 type CollectionPanelProps = CollectionPanelDataProps & DispatchProp
104     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
105
106 export const CollectionPanel = withStyles(styles)(
107     connect((state: RootState, props: RouteComponentProps<{ id: string }>) => {
108         const currentUserUUID = getUserUuid(state);
109         const item = getResource<CollectionResource>(props.match.params.id)(state.resources);
110         let isWritable = false;
111         const isOldVersion = item && item.currentVersionUuid !== item.uuid;
112         if (item && !isOldVersion) {
113             if (item.ownerUuid === currentUserUUID) {
114                 isWritable = true;
115             } else {
116                 const itemOwner = getResource<GroupResource|UserResource>(item.ownerUuid)(state.resources);
117                 if (itemOwner) {
118                     isWritable = itemOwner.writableBy.indexOf(currentUserUUID || '') >= 0;
119                 }
120             }
121         }
122         const loadingFilesIndicator = getProgressIndicator(COLLECTION_PANEL_LOAD_FILES)(state.progressIndicator);
123         const isLoadingFiles = loadingFilesIndicator && loadingFilesIndicator!.working || false;
124         const tooManyFiles = !state.collectionPanel.loadBigCollections && item && item.fileCount > COLLECTION_PANEL_LOAD_FILES_THRESHOLD || false;
125         return { item, isWritable, isOldVersion, isLoadingFiles, tooManyFiles };
126     })(
127         class extends React.Component<CollectionPanelProps> {
128             render() {
129                 const { classes, item, dispatch, isWritable, isOldVersion, isLoadingFiles, tooManyFiles } = this.props;
130                 return item
131                     ? <div className={classes.root}>
132                         <ExpansionPanel data-cy='collection-info-panel' defaultExpanded>
133                             <ExpansionPanelSummary expandIcon={<ExpandIcon />}>
134                                 <Grid container justify="space-between">
135                                     <Grid item xs={11}><span>
136                                         <IconButton onClick={this.openCollectionDetails}>
137                                             { isOldVersion
138                                             ? <CollectionOldVersionIcon className={classes.iconHeader} />
139                                             : <CollectionIcon className={classes.iconHeader} /> }
140                                         </IconButton>
141                                         <IllegalNamingWarning name={item.name}/>
142                                         <span>
143                                             {item.name}
144                                             {isWritable ||
145                                             <Tooltip title="Read-only">
146                                                 <ReadOnlyIcon data-cy="read-only-icon" className={classes.readOnlyIcon} />
147                                             </Tooltip>
148                                             }
149                                         </span>
150                                     </span></Grid>
151                                     <Grid item xs={1} style={{textAlign: "right"}}>
152                                         <Tooltip title="Actions" disableFocusListener>
153                                             <IconButton
154                                                 data-cy='collection-panel-options-btn'
155                                                 aria-label="Actions"
156                                                 onClick={this.handleContextMenu}>
157                                                 <MoreOptionsIcon />
158                                             </IconButton>
159                                         </Tooltip>
160                                     </Grid>
161                                 </Grid>
162                             </ExpansionPanelSummary>
163                             <ExpansionPanelDetails>
164                                 <Grid container justify="space-between">
165                                     <Grid item xs={12}>
166                                         <Typography variant="caption">
167                                             {item.description}
168                                         </Typography>
169                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
170                                             label={isOldVersion ? "This version's UUID" : "Collection UUID"}
171                                             linkToUuid={item.uuid} />
172                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
173                                             label={isOldVersion ? "This version's PDH" : "Portable data hash"}
174                                             linkToUuid={item.portableDataHash} />
175                                         {isOldVersion &&
176                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
177                                             label='Most recent version'
178                                             linkToUuid={item.currentVersionUuid} />
179                                         }
180                                         <DetailsAttribute label='Last modified' value={formatDate(item.modifiedAt)} />
181                                         <DetailsAttribute label='Created at' value={formatDate(item.createdAt)} />
182                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
183                                             label='Version number' value={item.version} />
184                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
185                                             label='Number of files' value={item.fileCount} />
186                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
187                                             label='Content size' value={formatFileSize(item.fileSizeTotal)} />
188                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
189                                             label='Owner' linkToUuid={item.ownerUuid} />
190                                         {(item.properties.container_request || item.properties.containerRequest) &&
191                                             <span onClick={() => dispatch<any>(navigateToProcess(item.properties.container_request || item.properties.containerRequest))}>
192                                                 <DetailsAttribute classLabel={classes.link} label='Link to process' />
193                                             </span>
194                                         }
195                                         {isOldVersion &&
196                                         <Typography className={classes.warningLabel} variant="caption">
197                                             This is an old version. Copy it as a new one if you need to make changes. Go to the current version if you need to share it.
198                                         </Typography>
199                                         }
200                                     </Grid>
201                                 </Grid>
202                             </ExpansionPanelDetails>
203                         </ExpansionPanel>
204
205                         <ExpansionPanel data-cy='collection-properties-panel' defaultExpanded>
206                             <ExpansionPanelSummary expandIcon={<ExpandIcon />}>
207                                 {"Properties"}
208                             </ExpansionPanelSummary>
209                             <ExpansionPanelDetails>
210                                 <Grid container>
211                                     {isWritable && <Grid item xs={12}>
212                                         <CollectionTagForm />
213                                     </Grid>}
214                                     <Grid item xs={12}>
215                                     { Object.keys(item.properties).length > 0
216                                         ? Object.keys(item.properties).map(k =>
217                                             Array.isArray(item.properties[k])
218                                             ? item.properties[k].map((v: string) =>
219                                                 getPropertyChip(
220                                                     k, v,
221                                                     isWritable
222                                                         ? this.handleDelete(k, item.properties[k])
223                                                         : undefined,
224                                                     classes.tag))
225                                             : getPropertyChip(
226                                                 k, item.properties[k],
227                                                 isWritable
228                                                     ? this.handleDelete(k, item.properties[k])
229                                                     : undefined,
230                                                 classes.tag)
231                                         )
232                                         : <div className={classes.centeredLabel}>No properties set on this collection.</div>
233                                     }
234                                     </Grid>
235                                 </Grid>
236                             </ExpansionPanelDetails>
237                         </ExpansionPanel>
238                         <div className={classes.filesCard}>
239                             <CollectionPanelFiles
240                                 isWritable={isWritable}
241                                 isLoading={isLoadingFiles}
242                                 tooManyFiles={tooManyFiles}
243                                 loadFilesFunc={() => {
244                                     dispatch(collectionPanelActions.LOAD_BIG_COLLECTIONS(true));
245                                     dispatch<any>(loadCollectionFiles(this.props.item.uuid));
246                                 }
247                             } />
248                         </div>
249                     </div>
250                     : null;
251             }
252
253             handleContextMenu = (event: React.MouseEvent<any>) => {
254                 const { uuid, ownerUuid, name, description, kind, isTrashed } = this.props.item;
255                 const { isWritable } = this.props;
256                 const resource = {
257                     uuid,
258                     ownerUuid,
259                     name,
260                     description,
261                     kind,
262                     menuKind: isWritable
263                         ? isTrashed
264                             ? ContextMenuKind.TRASHED_COLLECTION
265                             : ContextMenuKind.COLLECTION
266                         : ContextMenuKind.READONLY_COLLECTION
267                 };
268                 // Avoid expanding/collapsing the panel
269                 event.stopPropagation();
270                 this.props.dispatch<any>(openContextMenu(event, resource));
271             }
272
273             onCopy = (message: string) =>
274                 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
275                     message,
276                     hideDuration: 2000,
277                     kind: SnackbarKind.SUCCESS
278                 }))
279
280             handleDelete = (key: string, value: string) => () => {
281                 this.props.dispatch<any>(deleteCollectionTag(key, value));
282             }
283
284             openCollectionDetails = () => {
285                 const { item } = this.props;
286                 if (item) {
287                     this.props.dispatch(openDetailsPanel(item.uuid));
288                 }
289             }
290
291             titleProps = {
292                 onClick: this.openCollectionDetails
293             };
294
295         }
296     )
297 );