Merge branch '21128-toolbar-context-menu'
[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, CardHeader, CardContent
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 { MoreVerticalIcon, 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 import { resourceIsFrozen } from 'common/frozen-resources';
40 import { NotFoundView } from 'views/not-found-panel/not-found-panel';
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     | 'header'
57     | 'title'
58     | 'avatar'
59     | 'content';
60
61 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
62     root: {
63         width: '100%',
64     },
65     button: {
66         cursor: 'pointer'
67     },
68     infoCard: {
69     },
70     propertiesCard: {
71         padding: 0,
72     },
73     filesCard: {
74         padding: 0,
75     },
76     iconHeader: {
77         fontSize: '1.875rem',
78         color: theme.customs.colors.greyL
79     },
80     tag: {
81         marginRight: theme.spacing.unit / 2,
82         marginBottom: theme.spacing.unit / 2
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     header: {
113         paddingTop: theme.spacing.unit,
114         paddingBottom: theme.spacing.unit,
115     },
116     title: {
117         overflow: 'hidden',
118         paddingTop: theme.spacing.unit * 0.5,
119         color: theme.customs.colors.green700,
120     },
121     avatar: {
122         alignSelf: 'flex-start',
123         paddingTop: theme.spacing.unit * 0.5
124     },
125     content: {
126         padding: theme.spacing.unit * 1.0,
127         paddingTop: theme.spacing.unit * 0.5,
128         '&:last-child': {
129             paddingBottom: theme.spacing.unit * 1,
130         }
131     }
132 });
133
134 interface CollectionPanelDataProps {
135     item: CollectionResource;
136     isWritable: boolean;
137     isOldVersion: boolean;
138     isLoadingFiles: boolean;
139 }
140
141 type CollectionPanelProps = CollectionPanelDataProps & DispatchProp & WithStyles<CssRules>
142
143 export const CollectionPanel = withStyles(styles)(connect(
144     (state: RootState, props: RouteComponentProps<{ id: string }>) => {
145         const currentUserUUID = getUserUuid(state);
146         const item = getResource<CollectionResource>(props.match.params.id)(state.resources);
147         let isWritable = false;
148         const isOldVersion = item && item.currentVersionUuid !== item.uuid;
149         if (item && !isOldVersion) {
150             if (item.ownerUuid === currentUserUUID) {
151                 isWritable = true;
152             } else {
153                 const itemOwner = getResource<GroupResource | UserResource>(item.ownerUuid)(state.resources);
154                 if (itemOwner) {
155                     isWritable = itemOwner.canWrite;
156                 }
157             }
158         }
159
160         if (item && isWritable) {
161             isWritable = !resourceIsFrozen(item, state.resources);
162         }
163
164         return { item, isWritable, isOldVersion };
165     })(
166         class extends React.Component<CollectionPanelProps> {
167             render() {
168                 const { classes, item, dispatch, isWritable, isOldVersion } = this.props;
169                 const panelsData: MPVPanelState[] = [
170                     { name: "Details" },
171                     { name: "Files" },
172                 ];
173                 return item
174                     ? <MPVContainer className={classes.root} spacing={8} direction="column" justify-content="flex-start" wrap="nowrap" panelStates={panelsData}>
175                         <MPVPanelContent xs="auto" data-cy='collection-info-panel'>
176                             <Card className={classes.infoCard}>
177                                 <CardHeader
178                                     className={classes.header}
179                                     classes={{
180                                         content: classes.title,
181                                         avatar: classes.avatar,
182                                     }}
183                                     avatar={<IconButton onClick={this.openCollectionDetails}>
184                                         {isOldVersion
185                                             ? <CollectionOldVersionIcon className={classes.iconHeader} />
186                                             : <CollectionIcon className={classes.iconHeader} />}
187                                     </IconButton>}
188                                     title={
189                                         <span>
190                                             <IllegalNamingWarning name={item.name} />
191                                             {item.name}
192                                             {isWritable ||
193                                                 <Tooltip title="Read-only">
194                                                     <ReadOnlyIcon data-cy="read-only-icon" className={classes.readOnlyIcon} />
195                                                 </Tooltip>}
196                                         </span>
197                                     }
198                                     action={
199                                         <Tooltip title="Actions" disableFocusListener>
200                                             <IconButton
201                                                 data-cy='collection-panel-options-btn'
202                                                 aria-label="Actions"
203                                                 onClick={this.handleContextMenu}>
204                                                 <MoreVerticalIcon />
205                                             </IconButton>
206                                         </Tooltip>
207                                     }
208                                 />
209                                 <CardContent className={classes.content}>
210                                     <Typography variant="caption">
211                                         {item.description}
212                                     </Typography>
213                                     <CollectionDetailsAttributes item={item} classes={classes} twoCol={true} showVersionBrowser={() => dispatch<any>(openDetailsPanel(item.uuid, 1))} />
214                                     {(item.properties.container_request || item.properties.containerRequest) &&
215                                         <span onClick={() => dispatch<any>(navigateToProcess(item.properties.container_request || item.properties.containerRequest))}>
216                                             <DetailsAttribute classLabel={classes.link} label='Link to process' />
217                                         </span>
218                                     }
219                                     {isOldVersion &&
220                                         <Typography className={classes.warningLabel} variant="caption">
221                                             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.
222                                         </Typography>
223                                     }
224                                 </CardContent>
225                             </Card>
226                         </MPVPanelContent>
227                         <MPVPanelContent xs>
228                             <Card className={classes.filesCard}>
229                                 <CollectionPanelFiles isWritable={isWritable} />
230                             </Card>
231                         </MPVPanelContent>
232                     </MPVContainer >
233                     : <NotFoundView
234                         icon={CollectionIcon}
235                         messages={["Collection not found"]}
236                     />
237                     ;
238             }
239
240             handleContextMenu = (event: React.MouseEvent<any>) => {
241                 const { uuid, ownerUuid, name, description,
242                     kind, storageClassesDesired, properties } = this.props.item;
243                 const menuKind = this.props.dispatch<any>(resourceUuidToContextMenuKind(uuid));
244                 const resource = {
245                     uuid,
246                     ownerUuid,
247                     name,
248                     description,
249                     storageClassesDesired,
250                     kind,
251                     menuKind,
252                     properties,
253                 };
254                 // Avoid expanding/collapsing the panel
255                 event.stopPropagation();
256                 this.props.dispatch<any>(openContextMenu(event, resource));
257             }
258
259             onCopy = (message: string) =>
260                 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
261                     message,
262                     hideDuration: 2000,
263                     kind: SnackbarKind.SUCCESS
264                 }))
265
266             openCollectionDetails = (e: React.MouseEvent<HTMLElement>) => {
267                 const { item } = this.props;
268                 if (item) {
269                     e.stopPropagation();
270                     this.props.dispatch<any>(openDetailsPanel(item.uuid));
271                 }
272             }
273
274             titleProps = {
275                 onClick: this.openCollectionDetails
276             };
277
278         }
279     )
280 );
281
282 interface CollectionDetailsProps {
283     item: CollectionResource;
284     classes?: any;
285     twoCol?: boolean;
286     showVersionBrowser?: () => void;
287 }
288
289 export const CollectionDetailsAttributes = (props: CollectionDetailsProps) => {
290     const item = props.item;
291     const classes = props.classes || { label: '', value: '', button: '', tag: '' };
292     const isOldVersion = item && item.currentVersionUuid !== item.uuid;
293     const mdSize = props.twoCol ? 6 : 12;
294     const showVersionBrowser = props.showVersionBrowser;
295     const responsiblePersonRef = React.useRef(null);
296     return <Grid container>
297         <Grid item xs={12} md={mdSize}>
298             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
299                 label={isOldVersion ? "This version's UUID" : "Collection UUID"}
300                 linkToUuid={item.uuid} />
301         </Grid>
302         <Grid item xs={12} md={mdSize}>
303             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
304                 label={isOldVersion ? "This version's PDH" : "Portable data hash"}
305                 linkToUuid={item.portableDataHash} />
306         </Grid>
307         <Grid item xs={12} md={mdSize}>
308             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
309                 label='Owner' linkToUuid={item.ownerUuid}
310                 uuidEnhancer={(uuid: string) => <ResourceWithName uuid={uuid} />} />
311         </Grid>
312         <div data-cy="responsible-person-wrapper" ref={responsiblePersonRef}>
313             <Grid item xs={12} md={12}>
314                 <DetailsAttribute classLabel={classes.label} classValue={classes.value}
315                     label='Responsible person' linkToUuid={item.ownerUuid}
316                     uuidEnhancer={(uuid: string) => <ResponsiblePerson uuid={item.uuid} parentRef={responsiblePersonRef.current} />} />
317             </Grid>
318         </div>
319         <Grid item xs={12} md={mdSize}>
320             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
321                 label='Head version'
322                 value={isOldVersion ? undefined : 'this one'}
323                 linkToUuid={isOldVersion ? item.currentVersionUuid : undefined} />
324         </Grid>
325         <Grid item xs={12} md={mdSize}>
326             <DetailsAttribute
327                 classLabel={classes.label} classValue={classes.value}
328                 label='Version number'
329                 value={showVersionBrowser !== undefined
330                     ? <Tooltip title="Open version browser"><ButtonLink underline='none' className={classes.button} onClick={() => showVersionBrowser()}>
331                         {<span data-cy='collection-version-number'>{item.version}</span>}
332                     </ButtonLink></Tooltip>
333                     : item.version
334                 }
335             />
336         </Grid>
337         <Grid item xs={12} md={mdSize}>
338             <DetailsAttribute label='Created at' value={formatDate(item.createdAt)} />
339         </Grid>
340         <Grid item xs={12} md={mdSize}>
341             <DetailsAttribute label='Last modified' value={formatDate(item.modifiedAt)} />
342         </Grid>
343         <Grid item xs={12} md={mdSize}>
344             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
345                 label='Number of files' value={<span data-cy='collection-file-count'>{item.fileCount}</span>} />
346         </Grid>
347         <Grid item xs={12} md={mdSize}>
348             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
349                 label='Content size' value={formatFileSize(item.fileSizeTotal)} />
350         </Grid>
351         <Grid item xs={12} md={mdSize}>
352             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
353                 label='Storage classes' value={item.storageClassesDesired ? item.storageClassesDesired.join(', ') : ["default"]} />
354         </Grid>
355
356         {/*
357             NOTE: The property list should be kept at the bottom, because it spans
358             the entire available width, without regards of the twoCol prop.
359           */}
360         <Grid item xs={12} md={12}>
361             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
362                 label='Properties' />
363             {Object.keys(item.properties).length > 0
364                 ? Object.keys(item.properties).map(k =>
365                     Array.isArray(item.properties[k])
366                         ? item.properties[k].map((v: string) =>
367                             getPropertyChip(k, v, undefined, classes.tag))
368                         : getPropertyChip(k, item.properties[k], undefined, classes.tag))
369                 : <div className={classes.value}>No properties</div>}
370         </Grid>
371     </Grid>;
372 };