a21d70ed175d9923bf6933a2406b3e47f6f33308
[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 * as React from 'react';
6 import {
7     StyleRulesCallback, WithStyles, withStyles,
8     IconButton, Grid, Tooltip, Typography, ExpansionPanel,
9     ExpansionPanelSummary, ExpansionPanelDetails
10 } from '@material-ui/core';
11 import { connect, DispatchProp } from "react-redux";
12 import { RouteComponentProps } from 'react-router';
13 import { ArvadosTheme } from '~/common/custom-theme';
14 import { RootState } from '~/store/store';
15 import { MoreOptionsIcon, CollectionIcon, ReadOnlyIcon, ExpandIcon, CollectionOldVersionIcon } from '~/components/icon/icon';
16 import { DetailsAttribute } from '~/components/details-attribute/details-attribute';
17 import { CollectionResource } from '~/models/collection';
18 import { CollectionPanelFiles } from '~/views-components/collection-panel-files/collection-panel-files';
19 import { CollectionTagForm } from './collection-tag-form';
20 import { deleteCollectionTag, navigateToProcess, collectionPanelActions } from '~/store/collection-panel/collection-panel-action';
21 import { getResource } from '~/store/resources/resources';
22 import { openContextMenu } from '~/store/context-menu/context-menu-actions';
23 import { ContextMenuKind } from '~/views-components/context-menu/context-menu';
24 import { formatFileSize } from "~/common/formatters";
25 import { openDetailsPanel } from '~/store/details-panel/details-panel-action';
26 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
27 import { getPropertyChip } from '~/views-components/resource-properties-form/property-chip';
28 import { IllegalNamingWarning } from '~/components/warning/warning';
29 import { GroupResource } from '~/models/group';
30 import { UserResource } from '~/models/user';
31 import { getUserUuid } from '~/common/getuser';
32 import { getProgressIndicator } from '~/store/progress-indicator/progress-indicator-reducer';
33 import { COLLECTION_PANEL_LOAD_FILES, loadCollectionFiles, COLLECTION_PANEL_LOAD_FILES_THRESHOLD } from '~/store/collection-panel/collection-panel-files/collection-panel-files-actions';
34
35 type CssRules = 'root'
36     | 'filesCard'
37     | 'iconHeader'
38     | 'tag'
39     | 'label'
40     | 'value'
41     | 'link'
42     | 'centeredLabel'
43     | 'warningLabel'
44     | 'collectionName'
45     | 'readOnlyIcon';
46
47 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
48     root: {
49         display: 'flex',
50         flexFlow: 'column',
51         height: 'calc(100vh - 130px)', // (100% viewport height) - (top bar + breadcrumbs)
52     },
53     filesCard: {
54         marginBottom: theme.spacing.unit * 2,
55         flex: 1,
56     },
57     iconHeader: {
58         fontSize: '1.875rem',
59         color: theme.customs.colors.yellow700
60     },
61     tag: {
62         marginRight: theme.spacing.unit,
63         marginBottom: theme.spacing.unit
64     },
65     label: {
66         fontSize: '0.875rem'
67     },
68     centeredLabel: {
69         fontSize: '0.875rem',
70         textAlign: 'center'
71     },
72     warningLabel: {
73         fontStyle: 'italic',
74         fontWeight: 'bold'
75     },
76     collectionName: {
77         flexDirection: 'column',
78     },
79     value: {
80         textTransform: 'none',
81         fontSize: '0.875rem'
82     },
83     link: {
84         fontSize: '0.875rem',
85         color: theme.palette.primary.main,
86         '&:hover': {
87             cursor: 'pointer'
88         }
89     },
90     readOnlyIcon: {
91         marginLeft: theme.spacing.unit,
92         fontSize: 'small',
93     }
94 });
95
96 interface CollectionPanelDataProps {
97     item: CollectionResource;
98     isWritable: boolean;
99     isOldVersion: boolean;
100     isLoadingFiles: boolean;
101     tooManyFiles: boolean;
102 }
103
104 type CollectionPanelProps = CollectionPanelDataProps & DispatchProp
105     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
106
107 export const CollectionPanel = withStyles(styles)(
108     connect((state: RootState, props: RouteComponentProps<{ id: string }>) => {
109         const currentUserUUID = getUserUuid(state);
110         const item = getResource<CollectionResource>(props.match.params.id)(state.resources);
111         let isWritable = false;
112         const isOldVersion = item && item.currentVersionUuid !== item.uuid;
113         if (item && !isOldVersion) {
114             if (item.ownerUuid === currentUserUUID) {
115                 isWritable = true;
116             } else {
117                 const itemOwner = getResource<GroupResource|UserResource>(item.ownerUuid)(state.resources);
118                 if (itemOwner) {
119                     isWritable = itemOwner.writableBy.indexOf(currentUserUUID || '') >= 0;
120                 }
121             }
122         }
123         const loadingFilesIndicator = getProgressIndicator(COLLECTION_PANEL_LOAD_FILES)(state.progressIndicator);
124         const isLoadingFiles = loadingFilesIndicator && loadingFilesIndicator!.working || false;
125         const tooManyFiles = !state.collectionPanel.loadBigCollections && item && item.fileCount > COLLECTION_PANEL_LOAD_FILES_THRESHOLD || false;
126         return { item, isWritable, isOldVersion, isLoadingFiles, tooManyFiles };
127     })(
128         class extends React.Component<CollectionPanelProps> {
129             render() {
130                 const { classes, item, dispatch, isWritable, isOldVersion, isLoadingFiles, tooManyFiles } = this.props;
131                 return item
132                     ? <div className={classes.root}>
133                         <ExpansionPanel data-cy='collection-info-panel' defaultExpanded>
134                             <ExpansionPanelSummary expandIcon={<ExpandIcon />}>
135                                 <span>
136                                     <IconButton onClick={this.openCollectionDetails}>
137                                         { isOldVersion
138                                         ? <CollectionOldVersionIcon className={classes.iconHeader} />
139                                         : <CollectionIcon className={classes.iconHeader} /> }
140                                     </IconButton>
141                                     <IllegalNamingWarning name={item.name}/>
142                                     <span>
143                                         {item.name}
144                                         {isWritable ||
145                                         <Tooltip title="Read-only">
146                                             <ReadOnlyIcon data-cy="read-only-icon" className={classes.readOnlyIcon} />
147                                         </Tooltip>
148                                         }
149                                         {isOldVersion &&
150                                         <Typography className={classes.warningLabel} variant="caption">
151                                             This is an old version. Copy it as a new one if you need to make changes. Go to the current version if you need to share it.
152                                         </Typography>
153                                         }
154                                     </span>
155                                 </span>
156                             </ExpansionPanelSummary>
157                             <ExpansionPanelDetails>
158                                 <Grid container justify="space-between">
159                                     <Grid item xs={11}>
160                                         <Typography variant="caption">
161                                             {item.description}
162                                         </Typography>
163                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
164                                             label='Collection UUID'
165                                             linkToUuid={item.uuid} />
166                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
167                                             label='Portable data hash'
168                                             linkToUuid={item.portableDataHash} />
169                                         {isOldVersion &&
170                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
171                                             label='Current Version UUID'
172                                             linkToUuid={item.currentVersionUuid} />
173                                         }
174                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
175                                             label='Number of files' value={item.fileCount} />
176                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
177                                             label='Content size' value={formatFileSize(item.fileSizeTotal)} />
178                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
179                                             label='Owner' linkToUuid={item.ownerUuid} />
180                                         {(item.properties.container_request || item.properties.containerRequest) &&
181                                             <span onClick={() => dispatch<any>(navigateToProcess(item.properties.container_request || item.properties.containerRequest))}>
182                                                 <DetailsAttribute classLabel={classes.link} label='Link to process' />
183                                             </span>
184                                         }
185                                     </Grid>
186                                     <Grid item xs={1} style={{textAlign: "right"}}>
187                                         <Tooltip title="More options" disableFocusListener>
188                                             <IconButton
189                                                 data-cy='collection-panel-options-btn'
190                                                 aria-label="More options"
191                                                 onClick={this.handleContextMenu}>
192                                                 <MoreOptionsIcon />
193                                             </IconButton>
194                                         </Tooltip>
195                                     </Grid>
196                                 </Grid>
197                             </ExpansionPanelDetails>
198                         </ExpansionPanel>
199
200                         <ExpansionPanel data-cy='collection-properties-panel' defaultExpanded>
201                             <ExpansionPanelSummary expandIcon={<ExpandIcon />}>
202                                 {"Properties"}
203                             </ExpansionPanelSummary>
204                             <ExpansionPanelDetails>
205                                 <Grid container>
206                                     {isWritable && <Grid item xs={12}>
207                                         <CollectionTagForm />
208                                     </Grid>}
209                                     <Grid item xs={12}>
210                                     { Object.keys(item.properties).length > 0
211                                         ? Object.keys(item.properties).map(k =>
212                                             Array.isArray(item.properties[k])
213                                             ? item.properties[k].map((v: string) =>
214                                                 getPropertyChip(
215                                                     k, v,
216                                                     isWritable
217                                                         ? this.handleDelete(k, item.properties[k])
218                                                         : undefined,
219                                                     classes.tag))
220                                             : getPropertyChip(
221                                                 k, item.properties[k],
222                                                 isWritable
223                                                     ? this.handleDelete(k, item.properties[k])
224                                                     : undefined,
225                                                 classes.tag)
226                                         )
227                                         : <div className={classes.centeredLabel}>No properties set on this collection.</div>
228                                     }
229                                     </Grid>
230                                 </Grid>
231                             </ExpansionPanelDetails>
232                         </ExpansionPanel>
233                         <div className={classes.filesCard}>
234                             <CollectionPanelFiles
235                                 isWritable={isWritable}
236                                 isLoading={isLoadingFiles}
237                                 tooManyFiles={tooManyFiles}
238                                 loadFilesFunc={() => {
239                                     dispatch(collectionPanelActions.LOAD_BIG_COLLECTIONS(true));
240                                     dispatch<any>(loadCollectionFiles(this.props.item.uuid));
241                                 }
242                             } />
243                         </div>
244                     </div>
245                     : null;
246             }
247
248             handleContextMenu = (event: React.MouseEvent<any>) => {
249                 const { uuid, ownerUuid, name, description, kind, isTrashed } = this.props.item;
250                 const { isWritable } = this.props;
251                 const resource = {
252                     uuid,
253                     ownerUuid,
254                     name,
255                     description,
256                     kind,
257                     menuKind: isWritable
258                         ? isTrashed
259                             ? ContextMenuKind.TRASHED_COLLECTION
260                             : ContextMenuKind.COLLECTION
261                         : ContextMenuKind.READONLY_COLLECTION
262                 };
263                 this.props.dispatch<any>(openContextMenu(event, resource));
264             }
265
266             onCopy = (message: string) =>
267                 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
268                     message,
269                     hideDuration: 2000,
270                     kind: SnackbarKind.SUCCESS
271                 }))
272
273             handleDelete = (key: string, value: string) => () => {
274                 this.props.dispatch<any>(deleteCollectionTag(key, value));
275             }
276
277             openCollectionDetails = () => {
278                 const { item } = this.props;
279                 if (item) {
280                     this.props.dispatch(openDetailsPanel(item.uuid));
281                 }
282             }
283
284             titleProps = {
285                 onClick: this.openCollectionDetails
286             };
287
288         }
289     )
290 );