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