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