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