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