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