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