18787: Removes remaining traces of old big collection loading check.
[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 } 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 } 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 { ResourceWithName, 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 }
119
120 type CollectionPanelProps = CollectionPanelDataProps & DispatchProp
121     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
122
123 export const CollectionPanel = withStyles(styles)(
124     connect((state: RootState, props: RouteComponentProps<{ id: string }>) => {
125         const currentUserUUID = getUserUuid(state);
126         const item = getResource<CollectionResource>(props.match.params.id)(state.resources);
127         let isWritable = false;
128         const isOldVersion = item && item.currentVersionUuid !== item.uuid;
129         if (item && !isOldVersion) {
130             if (item.ownerUuid === currentUserUUID) {
131                 isWritable = true;
132             } else {
133                 const itemOwner = getResource<GroupResource | UserResource>(item.ownerUuid)(state.resources);
134                 if (itemOwner && itemOwner.writableBy) {
135                     isWritable = itemOwner.writableBy.indexOf(currentUserUUID || '') >= 0;
136                 }
137             }
138         }
139         const loadingFilesIndicator = getProgressIndicator(COLLECTION_PANEL_LOAD_FILES)(state.progressIndicator);
140         const isLoadingFiles = (loadingFilesIndicator && loadingFilesIndicator!.working) || false;
141         return { item, isWritable, isOldVersion, isLoadingFiles };
142     })(
143         class extends React.Component<CollectionPanelProps> {
144             render() {
145                 const { classes, item, dispatch, isWritable, isOldVersion, isLoadingFiles } = this.props;
146                 const panelsData: MPVPanelState[] = [
147                     { name: "Details" },
148                     { name: "Files" },
149                 ];
150                 return item
151                     ? <MPVContainer className={classes.root} spacing={8} direction="column" justify-content="flex-start" wrap="nowrap" panelStates={panelsData}>
152                         <MPVPanelContent xs="auto" data-cy='collection-info-panel'>
153                             <Card className={classes.infoCard}>
154                                 <Grid container justify="space-between">
155                                     <Grid item xs={11}><span>
156                                         <IconButton onClick={this.openCollectionDetails}>
157                                             {isOldVersion
158                                                 ? <CollectionOldVersionIcon className={classes.iconHeader} />
159                                                 : <CollectionIcon className={classes.iconHeader} />}
160                                         </IconButton>
161                                         <IllegalNamingWarning name={item.name} />
162                                         <span>
163                                             {item.name}
164                                             {isWritable ||
165                                                 <Tooltip title="Read-only">
166                                                     <ReadOnlyIcon data-cy="read-only-icon" className={classes.readOnlyIcon} />
167                                                 </Tooltip>
168                                             }
169                                         </span>
170                                     </span></Grid>
171                                     <Grid item xs={1} style={{ textAlign: "right" }}>
172                                         <Tooltip title="Actions" disableFocusListener>
173                                             <IconButton
174                                                 data-cy='collection-panel-options-btn'
175                                                 aria-label="Actions"
176                                                 onClick={this.handleContextMenu}>
177                                                 <MoreOptionsIcon />
178                                             </IconButton>
179                                         </Tooltip>
180                                     </Grid>
181                                 </Grid>
182                                 <Grid container justify="space-between">
183                                     <Grid item xs={12}>
184                                         <Typography variant="caption">
185                                             {item.description}
186                                         </Typography>
187                                         <CollectionDetailsAttributes item={item} classes={classes} twoCol={true} showVersionBrowser={() => dispatch<any>(openDetailsPanel(item.uuid, 1))} />
188                                         {(item.properties.container_request || item.properties.containerRequest) &&
189                                             <span onClick={() => dispatch<any>(navigateToProcess(item.properties.container_request || item.properties.containerRequest))}>
190                                                 <DetailsAttribute classLabel={classes.link} label='Link to process' />
191                                             </span>
192                                         }
193                                         {isOldVersion &&
194                                             <Typography className={classes.warningLabel} variant="caption">
195                                                 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.
196                                             </Typography>
197                                         }
198                                     </Grid>
199                                 </Grid>
200                             </Card>
201                         </MPVPanelContent>
202                         <MPVPanelContent xs>
203                             <Card className={classes.filesCard}>
204                                 <CollectionPanelFiles
205                                     isWritable={isWritable}
206                                     isLoading={isLoadingFiles}
207                                     loadFilesFunc={() => {
208                                         dispatch<any>(loadCollectionFiles(this.props.item.uuid));
209                                     }
210                                     } />
211                             </Card>
212                         </MPVPanelContent>
213                     </MPVContainer>
214                     : null;
215             }
216
217             handleContextMenu = (event: React.MouseEvent<any>) => {
218                 const { uuid, ownerUuid, name, description,
219                     kind, storageClassesDesired, properties } = this.props.item;
220                 const menuKind = this.props.dispatch<any>(resourceUuidToContextMenuKind(uuid));
221                 const resource = {
222                     uuid,
223                     ownerUuid,
224                     name,
225                     description,
226                     storageClassesDesired,
227                     kind,
228                     menuKind,
229                     properties,
230                 };
231                 // Avoid expanding/collapsing the panel
232                 event.stopPropagation();
233                 this.props.dispatch<any>(openContextMenu(event, resource));
234             }
235
236             onCopy = (message: string) =>
237                 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
238                     message,
239                     hideDuration: 2000,
240                     kind: SnackbarKind.SUCCESS
241                 }))
242
243             openCollectionDetails = (e: React.MouseEvent<HTMLElement>) => {
244                 const { item } = this.props;
245                 if (item) {
246                     e.stopPropagation();
247                     this.props.dispatch<any>(openDetailsPanel(item.uuid));
248                 }
249             }
250
251             titleProps = {
252                 onClick: this.openCollectionDetails
253             };
254
255         }
256     )
257 );
258
259 interface CollectionDetailsProps {
260     item: CollectionResource;
261     classes?: any;
262     twoCol?: boolean;
263     showVersionBrowser?: () => void;
264 }
265
266 export const CollectionDetailsAttributes = (props: CollectionDetailsProps) => {
267     const item = props.item;
268     const classes = props.classes || { label: '', value: '', button: '', tag: '' };
269     const isOldVersion = item && item.currentVersionUuid !== item.uuid;
270     const mdSize = props.twoCol ? 6 : 12;
271     const showVersionBrowser = props.showVersionBrowser;
272     const responsiblePersonRef = React.useRef(null);
273     return <Grid container>
274         <Grid item xs={12} md={mdSize}>
275             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
276                 label={isOldVersion ? "This version's UUID" : "Collection UUID"}
277                 linkToUuid={item.uuid} />
278         </Grid>
279         <Grid item xs={12} md={mdSize}>
280             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
281                 label={isOldVersion ? "This version's PDH" : "Portable data hash"}
282                 linkToUuid={item.portableDataHash} />
283         </Grid>
284         <Grid item xs={12} md={mdSize}>
285             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
286                 label='Owner' linkToUuid={item.ownerUuid}
287                 uuidEnhancer={(uuid: string) => <ResourceWithName uuid={uuid} />} />
288         </Grid>
289         <div data-cy="responsible-person-wrapper" ref={responsiblePersonRef}>
290             <Grid item xs={12} md={12}>
291                 <DetailsAttribute classLabel={classes.label} classValue={classes.value}
292                     label='Responsible person' linkToUuid={item.ownerUuid}
293                     uuidEnhancer={(uuid: string) => <ResponsiblePerson uuid={item.uuid} parentRef={responsiblePersonRef.current} />} />
294             </Grid>
295         </div>
296         <Grid item xs={12} md={mdSize}>
297             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
298                 label='Head version'
299                 value={isOldVersion ? undefined : 'this one'}
300                 linkToUuid={isOldVersion ? item.currentVersionUuid : undefined} />
301         </Grid>
302         <Grid item xs={12} md={mdSize}>
303             <DetailsAttribute
304                 classLabel={classes.label} classValue={classes.value}
305                 label='Version number'
306                 value={showVersionBrowser !== undefined
307                     ? <Tooltip title="Open version browser"><ButtonLink underline='none' className={classes.button} onClick={() => showVersionBrowser()}>
308                         {<span data-cy='collection-version-number'>{item.version}</span>}
309                     </ButtonLink></Tooltip>
310                     : item.version
311                 }
312             />
313         </Grid>
314         <Grid item xs={12} md={mdSize}>
315             <DetailsAttribute label='Created at' value={formatDate(item.createdAt)} />
316         </Grid>
317         <Grid item xs={12} md={mdSize}>
318             <DetailsAttribute label='Last modified' value={formatDate(item.modifiedAt)} />
319         </Grid>
320         <Grid item xs={12} md={mdSize}>
321             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
322                 label='Number of files' value={<span data-cy='collection-file-count'>{item.fileCount}</span>} />
323         </Grid>
324         <Grid item xs={12} md={mdSize}>
325             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
326                 label='Content size' value={formatFileSize(item.fileSizeTotal)} />
327         </Grid>
328         <Grid item xs={12} md={mdSize}>
329             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
330                 label='Storage classes' value={item.storageClassesDesired.join(', ')} />
331         </Grid>
332
333         {/*
334             NOTE: The property list should be kept at the bottom, because it spans
335             the entire available width, without regards of the twoCol prop.
336         */}
337         <Grid item xs={12} md={12}>
338             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
339                 label='Properties' />
340             {Object.keys(item.properties).length > 0
341                 ? Object.keys(item.properties).map(k =>
342                     Array.isArray(item.properties[k])
343                         ? item.properties[k].map((v: string) =>
344                             getPropertyChip(k, v, undefined, classes.tag))
345                         : getPropertyChip(k, item.properties[k], undefined, classes.tag))
346                 : <div className={classes.value}>No properties</div>}
347         </Grid>
348     </Grid>;
349 };