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