Merge branch '18123-group-edit-page-rebase1' into main. Closes #18123
[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 { 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 { CollectionTagForm } from './collection-tag-form';
25 import { deleteCollectionTag, navigateToProcess, collectionPanelActions } from 'store/collection-panel/collection-panel-action';
26 import { getResource } from 'store/resources/resources';
27 import { openContextMenu, resourceUuidToContextMenuKind } from 'store/context-menu/context-menu-actions';
28 import { formatDate, formatFileSize } from "common/formatters";
29 import { openDetailsPanel } from 'store/details-panel/details-panel-action';
30 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
31 import { getPropertyChip } from 'views-components/resource-properties-form/property-chip';
32 import { IllegalNamingWarning } from 'components/warning/warning';
33 import { GroupResource } from 'models/group';
34 import { UserResource } from 'models/user';
35 import { getUserUuid } from 'common/getuser';
36 import { getProgressIndicator } from 'store/progress-indicator/progress-indicator-reducer';
37 import { COLLECTION_PANEL_LOAD_FILES, loadCollectionFiles, COLLECTION_PANEL_LOAD_FILES_THRESHOLD } from 'store/collection-panel/collection-panel-files/collection-panel-files-actions';
38 import { Link } from 'react-router-dom';
39 import { Link as ButtonLink } from '@material-ui/core';
40 import { ResourceOwnerWithName, ResponsiblePerson } from 'views-components/data-explorer/renderers';
41 import { MPVContainer, MPVPanelContent, MPVPanelState } from 'components/multi-panel-view/multi-panel-view';
42
43 type CssRules = 'root'
44     | 'button'
45     | 'infoCard'
46     | 'propertiesCard'
47     | 'filesCard'
48     | 'iconHeader'
49     | 'tag'
50     | 'label'
51     | 'value'
52     | 'link'
53     | 'centeredLabel'
54     | 'warningLabel'
55     | 'collectionName'
56     | 'readOnlyIcon';
57
58 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
59     root: {
60         width: '100%',
61     },
62     button: {
63         cursor: 'pointer'
64     },
65     infoCard: {
66         paddingLeft: theme.spacing.unit * 2,
67         paddingRight: theme.spacing.unit * 2,
68         paddingBottom: theme.spacing.unit * 2,
69     },
70     propertiesCard: {
71         padding: 0,
72     },
73     filesCard: {
74         padding: 0,
75     },
76     iconHeader: {
77         fontSize: '1.875rem',
78         color: theme.customs.colors.yellow700
79     },
80     tag: {
81         marginRight: theme.spacing.unit,
82         marginBottom: theme.spacing.unit
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 });
113
114 interface CollectionPanelDataProps {
115     item: CollectionResource;
116     isWritable: boolean;
117     isOldVersion: boolean;
118     isLoadingFiles: boolean;
119     tooManyFiles: boolean;
120 }
121
122 type CollectionPanelProps = CollectionPanelDataProps & DispatchProp
123     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
124
125 export const CollectionPanel = withStyles(styles)(
126     connect((state: RootState, props: RouteComponentProps<{ id: string }>) => {
127         const currentUserUUID = getUserUuid(state);
128         const item = getResource<CollectionResource>(props.match.params.id)(state.resources);
129         let isWritable = false;
130         const isOldVersion = item && item.currentVersionUuid !== item.uuid;
131         if (item && !isOldVersion) {
132             if (item.ownerUuid === currentUserUUID) {
133                 isWritable = true;
134             } else {
135                 const itemOwner = getResource<GroupResource | UserResource>(item.ownerUuid)(state.resources);
136                 if (itemOwner && itemOwner.writableBy) {
137                     isWritable = itemOwner.writableBy.indexOf(currentUserUUID || '') >= 0;
138                 }
139             }
140         }
141         const loadingFilesIndicator = getProgressIndicator(COLLECTION_PANEL_LOAD_FILES)(state.progressIndicator);
142         const isLoadingFiles = (loadingFilesIndicator && loadingFilesIndicator!.working) || false;
143         const tooManyFiles = (!state.collectionPanel.loadBigCollections && item && item.fileCount > COLLECTION_PANEL_LOAD_FILES_THRESHOLD) || false;
144         return { item, isWritable, isOldVersion, isLoadingFiles, tooManyFiles };
145     })(
146         class extends React.Component<CollectionPanelProps> {
147             render() {
148                 const { classes, item, dispatch, isWritable, isOldVersion, isLoadingFiles, tooManyFiles } = this.props;
149                 const panelsData: MPVPanelState[] = [
150                     {name: "Details"},
151                     {name: "Properties"},
152                     {name: "Files"},
153                 ];
154                 return item
155                     ? <MPVContainer className={classes.root} spacing={8} direction="column" justify-content="flex-start" wrap="nowrap" panelStates={panelsData}>
156                         <MPVPanelContent xs="auto" data-cy='collection-info-panel'>
157                             <Card className={classes.infoCard}>
158                                 <Grid container justify="space-between">
159                                     <Grid item xs={11}><span>
160                                         <IconButton onClick={this.openCollectionDetails}>
161                                             {isOldVersion
162                                                 ? <CollectionOldVersionIcon className={classes.iconHeader} />
163                                                 : <CollectionIcon className={classes.iconHeader} />}
164                                         </IconButton>
165                                         <IllegalNamingWarning name={item.name} />
166                                         <span>
167                                             {item.name}
168                                             {isWritable ||
169                                                 <Tooltip title="Read-only">
170                                                     <ReadOnlyIcon data-cy="read-only-icon" className={classes.readOnlyIcon} />
171                                                 </Tooltip>
172                                             }
173                                         </span>
174                                     </span></Grid>
175                                     <Grid item xs={1} style={{ textAlign: "right" }}>
176                                         <Tooltip title="Actions" disableFocusListener>
177                                             <IconButton
178                                                 data-cy='collection-panel-options-btn'
179                                                 aria-label="Actions"
180                                                 onClick={this.handleContextMenu}>
181                                                 <MoreOptionsIcon />
182                                             </IconButton>
183                                         </Tooltip>
184                                     </Grid>
185                                 </Grid>
186                                 <Grid container justify="space-between">
187                                     <Grid item xs={12}>
188                                         <Typography variant="caption">
189                                             {item.description}
190                                         </Typography>
191                                         <CollectionDetailsAttributes item={item} classes={classes} twoCol={true} showVersionBrowser={() => dispatch<any>(openDetailsPanel(item.uuid, 1))} />
192                                         {(item.properties.container_request || item.properties.containerRequest) &&
193                                             <span onClick={() => dispatch<any>(navigateToProcess(item.properties.container_request || item.properties.containerRequest))}>
194                                                 <DetailsAttribute classLabel={classes.link} label='Link to process' />
195                                             </span>
196                                         }
197                                         {isOldVersion &&
198                                             <Typography className={classes.warningLabel} variant="caption">
199                                                 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.
200                                           </Typography>
201                                         }
202                                     </Grid>
203                                 </Grid>
204                             </Card>
205                         </MPVPanelContent>
206                         <MPVPanelContent xs="auto" data-cy='collection-properties-panel'>
207                             <Card className={classes.propertiesCard}>
208                                 <CardHeader title="Properties" />
209                                 <CardContent><Grid container>
210                                     {isWritable && <Grid item xs={12}>
211                                         <CollectionTagForm />
212                                     </Grid>}
213                                     <Grid item xs={12}>
214                                         {Object.keys(item.properties).length > 0
215                                             ? Object.keys(item.properties).map(k =>
216                                                 Array.isArray(item.properties[k])
217                                                     ? item.properties[k].map((v: string) =>
218                                                         getPropertyChip(
219                                                             k, v,
220                                                             isWritable
221                                                                 ? this.handleDelete(k, v)
222                                                                 : undefined,
223                                                             classes.tag))
224                                                     : getPropertyChip(
225                                                         k, item.properties[k],
226                                                         isWritable
227                                                             ? this.handleDelete(k, item.properties[k])
228                                                             : undefined,
229                                                         classes.tag)
230                                             )
231                                             : <div className={classes.centeredLabel}>No properties set on this collection.</div>
232                                         }
233                                     </Grid>
234                                 </Grid></CardContent>
235                             </Card>
236                         </MPVPanelContent>
237                         <MPVPanelContent xs>
238                             <Card className={classes.filesCard}>
239                                 <CollectionPanelFiles
240                                     isWritable={isWritable}
241                                     isLoading={isLoadingFiles}
242                                     tooManyFiles={tooManyFiles}
243                                     loadFilesFunc={() => {
244                                         dispatch(collectionPanelActions.LOAD_BIG_COLLECTIONS(true));
245                                         dispatch<any>(loadCollectionFiles(this.props.item.uuid));
246                                     }
247                                     } />
248                             </Card>
249                         </MPVPanelContent>
250                     </MPVContainer>
251                     : null;
252             }
253
254             handleContextMenu = (event: React.MouseEvent<any>) => {
255                 const { uuid, ownerUuid, name, description, kind, storageClassesDesired } = this.props.item;
256                 const menuKind = this.props.dispatch<any>(resourceUuidToContextMenuKind(uuid));
257                 const resource = {
258                     uuid,
259                     ownerUuid,
260                     name,
261                     description,
262                     storageClassesDesired,
263                     kind,
264                     menuKind,
265                 };
266                 // Avoid expanding/collapsing the panel
267                 event.stopPropagation();
268                 this.props.dispatch<any>(openContextMenu(event, resource));
269             }
270
271             onCopy = (message: string) =>
272                 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
273                     message,
274                     hideDuration: 2000,
275                     kind: SnackbarKind.SUCCESS
276                 }))
277
278             handleDelete = (key: string, value: string) => () => {
279                 this.props.dispatch<any>(deleteCollectionTag(key, value));
280             }
281
282             openCollectionDetails = (e: React.MouseEvent<HTMLElement>) => {
283                 const { item } = this.props;
284                 if (item) {
285                     e.stopPropagation();
286                     this.props.dispatch<any>(openDetailsPanel(item.uuid));
287                 }
288             }
289
290             titleProps = {
291                 onClick: this.openCollectionDetails
292             };
293
294         }
295     )
296 );
297
298 export const CollectionDetailsAttributes = (props: { item: CollectionResource, twoCol: boolean, classes?: Record<CssRules, string>, showVersionBrowser?: () => void }) => {
299     const item = props.item;
300     const classes = props.classes || { label: '', value: '', button: '' };
301     const isOldVersion = item && item.currentVersionUuid !== item.uuid;
302     const mdSize = props.twoCol ? 6 : 12;
303     const showVersionBrowser = props.showVersionBrowser;
304     const responsiblePersonRef = React.useRef(null);
305     return <Grid container>
306         <Grid item xs={12} md={mdSize}>
307             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
308                 label={isOldVersion ? "This version's UUID" : "Collection UUID"}
309                 linkToUuid={item.uuid} />
310         </Grid>
311         <Grid item xs={12} md={mdSize}>
312             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
313                 label={isOldVersion ? "This version's PDH" : "Portable data hash"}
314                 linkToUuid={item.portableDataHash} />
315         </Grid>
316         <Grid item xs={12} md={mdSize}>
317             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
318                 label='Owner' linkToUuid={item.ownerUuid}
319                 uuidEnhancer={(uuid: string) => <ResourceOwnerWithName uuid={uuid} />} />
320         </Grid>
321         <div data-cy="responsible-person-wrapper" ref={responsiblePersonRef}>
322             <Grid item xs={12} md={12}>
323                 <DetailsAttribute classLabel={classes.label} classValue={classes.value}
324                     label='Responsible person' linkToUuid={item.ownerUuid}
325                     uuidEnhancer={(uuid: string) => <ResponsiblePerson uuid={item.uuid} parentRef={responsiblePersonRef.current} />} />
326             </Grid>
327         </div>
328         <Grid item xs={12} md={mdSize}>
329             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
330                 label='Head version'
331                 value={isOldVersion ? undefined : 'this one'}
332                 linkToUuid={isOldVersion ? item.currentVersionUuid : undefined} />
333         </Grid>
334         <Grid item xs={12} md={mdSize}>
335             <DetailsAttribute
336                 classLabel={classes.label} classValue={classes.value}
337                 label='Version number'
338                 value={showVersionBrowser !== undefined
339                     ? <Tooltip title="Open version browser"><ButtonLink underline='none' className={classes.button} onClick={() => showVersionBrowser()}>
340                         {<span data-cy='collection-version-number'>{item.version}</span>}
341                     </ButtonLink></Tooltip>
342                     : item.version
343                 }
344             />
345         </Grid>
346         <Grid item xs={12} md={mdSize}>
347             <DetailsAttribute label='Created at' value={formatDate(item.createdAt)} />
348         </Grid>
349         <Grid item xs={12} md={mdSize}>
350             <DetailsAttribute label='Last modified' value={formatDate(item.modifiedAt)} />
351         </Grid>
352         <Grid item xs={12} md={mdSize}>
353             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
354                 label='Number of files' value={item.fileCount} />
355         </Grid>
356         <Grid item xs={12} md={mdSize}>
357             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
358                 label='Content size' value={formatFileSize(item.fileSizeTotal)} />
359         </Grid>
360         <Grid item xs={12} md={mdSize}>
361             <DetailsAttribute classLabel={classes.label} classValue={classes.value}
362                 label='Storage classes' value={item.storageClassesDesired.join(', ')} />
363         </Grid>
364     </Grid>;
365 };