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