16118: Restricts UI elements when a collection is read-only.
[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, Card,
8     CardHeader, IconButton, CardContent, Grid, Tooltip
9 } from '@material-ui/core';
10 import { connect, DispatchProp } from "react-redux";
11 import { RouteComponentProps } from 'react-router';
12 import { ArvadosTheme } from '~/common/custom-theme';
13 import { RootState } from '~/store/store';
14 import { MoreOptionsIcon, CollectionIcon, ReadOnlyIcon } from '~/components/icon/icon';
15 import { DetailsAttribute } from '~/components/details-attribute/details-attribute';
16 import { CollectionResource } from '~/models/collection';
17 import { CollectionPanelFiles } from '~/views-components/collection-panel-files/collection-panel-files';
18 import { CollectionTagForm } from './collection-tag-form';
19 import { deleteCollectionTag, navigateToProcess } from '~/store/collection-panel/collection-panel-action';
20 import { getResource } from '~/store/resources/resources';
21 import { openContextMenu } from '~/store/context-menu/context-menu-actions';
22 import { ContextMenuKind } from '~/views-components/context-menu/context-menu';
23 import { formatFileSize } from "~/common/formatters";
24 import { openDetailsPanel } from '~/store/details-panel/details-panel-action';
25 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
26 import { getPropertyChip } from '~/views-components/resource-properties-form/property-chip';
27 import { IllegalNamingWarning } from '~/components/warning/warning';
28 import { GroupResource } from '~/models/group';
29 import { UserResource } from '~/models/user';
30 import { getUserUuid } from '~/common/getuser';
31
32 type CssRules = 'card' | 'iconHeader' | 'tag' | 'label' | 'value' | 'link' | 'centeredLabel';
33
34 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
35     card: {
36         marginBottom: theme.spacing.unit * 2
37     },
38     iconHeader: {
39         fontSize: '1.875rem',
40         color: theme.customs.colors.yellow700
41     },
42     tag: {
43         marginRight: theme.spacing.unit,
44         marginBottom: theme.spacing.unit
45     },
46     label: {
47         fontSize: '0.875rem'
48     },
49     centeredLabel: {
50         fontSize: '0.875rem',
51         textAlign: 'center'
52     },
53     value: {
54         textTransform: 'none',
55         fontSize: '0.875rem'
56     },
57     link: {
58         fontSize: '0.875rem',
59         color: theme.palette.primary.main,
60         '&:hover': {
61             cursor: 'pointer'
62         }
63     }
64 });
65
66 interface CollectionPanelDataProps {
67     item: CollectionResource;
68     isWritable: boolean;
69 }
70
71 type CollectionPanelProps = CollectionPanelDataProps & DispatchProp
72     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
73
74 export const CollectionPanel = withStyles(styles)(
75     connect((state: RootState, props: RouteComponentProps<{ id: string }>) => {
76         const currentUserUUID = getUserUuid(state);
77         const item = getResource<CollectionResource>(props.match.params.id)(state.resources);
78         let isWritable = false;
79         if (item && item.ownerUuid === currentUserUUID) {
80             isWritable = true;
81         } else if (item) {
82             const itemOwner = getResource<GroupResource|UserResource>(item.ownerUuid)(state.resources);
83             if (itemOwner) {
84                 isWritable = itemOwner.writableBy.indexOf(currentUserUUID || '') >= 0;
85             }
86         }
87         return { item, isWritable };
88     })(
89         class extends React.Component<CollectionPanelProps> {
90             render() {
91                 const { classes, item, dispatch, isWritable } = this.props;
92                 return item
93                     ? <>
94                         <Card className={classes.card}>
95                             <CardHeader
96                                 avatar={
97                                     <IconButton onClick={this.openCollectionDetails}>
98                                         <CollectionIcon className={classes.iconHeader} />
99                                     </IconButton>
100                                 }
101                                 action={<div>
102                                     {isWritable === false &&
103                                     <Tooltip title="This collection is read-only">
104                                         <ReadOnlyIcon />
105                                     </Tooltip>}
106                                     <Tooltip title="More options" disableFocusListener>
107                                         <IconButton
108                                             aria-label="More options"
109                                             onClick={this.handleContextMenu}>
110                                             <MoreOptionsIcon />
111                                         </IconButton>
112                                     </Tooltip>
113                                 </div>}
114                                 title={<span><IllegalNamingWarning name={item.name}/>{item.name}</span>}
115                                 titleTypographyProps={this.titleProps}
116                                 subheader={item.description}
117                                 subheaderTypographyProps={this.titleProps} />
118                             <CardContent>
119                                 <Grid container direction="column">
120                                     <Grid item xs={10}>
121                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
122                                             label='Collection UUID'
123                                             linkToUuid={item.uuid} />
124                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
125                                             label='Portable data hash'
126                                             linkToUuid={item.portableDataHash} />
127                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
128                                             label='Number of files' value={item.fileCount} />
129                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
130                                             label='Content size' value={formatFileSize(item.fileSizeTotal)} />
131                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
132                                             label='Owner' linkToUuid={item.ownerUuid} />
133                                         {(item.properties.container_request || item.properties.containerRequest) &&
134                                             <span onClick={() => dispatch<any>(navigateToProcess(item.properties.container_request || item.properties.containerRequest))}>
135                                                 <DetailsAttribute classLabel={classes.link} label='Link to process' />
136                                             </span>
137                                         }
138                                     </Grid>
139                                 </Grid>
140                             </CardContent>
141                         </Card>
142
143                         <Card className={classes.card}>
144                             <CardHeader title="Properties" />
145                             <CardContent>
146                                 <Grid container direction="column">
147                                     {isWritable && <Grid item xs={12}>
148                                         <CollectionTagForm />
149                                     </Grid>}
150                                     <Grid item xs={12}>
151                                     { Object.keys(item.properties).length > 0
152                                         ? Object.keys(item.properties).map(k =>
153                                             Array.isArray(item.properties[k])
154                                             ? item.properties[k].map((v: string) =>
155                                                 getPropertyChip(
156                                                     k, v,
157                                                     isWritable
158                                                         ? this.handleDelete(k, item.properties[k])
159                                                         : undefined,
160                                                     classes.tag))
161                                             : getPropertyChip(
162                                                 k, item.properties[k],
163                                                 isWritable
164                                                     ? this.handleDelete(k, item.properties[k])
165                                                     : undefined,
166                                                 classes.tag)
167                                         )
168                                         : <div className={classes.centeredLabel}>No properties set on this collection.</div>
169                                     }
170                                     </Grid>
171                                 </Grid>
172                             </CardContent>
173                         </Card>
174                         <div className={classes.card}>
175                             <CollectionPanelFiles isWritable={isWritable} />
176                         </div>
177                     </>
178                     : null;
179             }
180
181             handleContextMenu = (event: React.MouseEvent<any>) => {
182                 const { uuid, ownerUuid, name, description, kind, isTrashed } = this.props.item;
183                 const { isWritable } = this.props;
184                 const resource = {
185                     uuid,
186                     ownerUuid,
187                     name,
188                     description,
189                     kind,
190                     menuKind: isWritable
191                         ? isTrashed
192                             ? ContextMenuKind.TRASHED_COLLECTION
193                             : ContextMenuKind.COLLECTION
194                         : ContextMenuKind.READONLY_COLLECTION
195                 };
196                 this.props.dispatch<any>(openContextMenu(event, resource));
197             }
198
199             onCopy = (message: string) =>
200                 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
201                     message,
202                     hideDuration: 2000,
203                     kind: SnackbarKind.SUCCESS
204                 }))
205
206             handleDelete = (key: string, value: string) => () => {
207                 this.props.dispatch<any>(deleteCollectionTag(key, value));
208             }
209
210             openCollectionDetails = () => {
211                 const { item } = this.props;
212                 if (item) {
213                     this.props.dispatch(openDetailsPanel(item.uuid));
214                 }
215             }
216
217             titleProps = {
218                 onClick: this.openCollectionDetails
219             };
220
221         }
222     )
223 );