16118: Changes read-only padlock icon with an explicit legend.
[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, Chip
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 data-cy='collection-info-panel' className={classes.card}>
95                             <CardHeader
96                                 avatar={
97                                     <IconButton onClick={this.openCollectionDetails}>
98                                         <CollectionIcon className={classes.iconHeader} />
99                                     </IconButton>
100                                 }
101                                 action={
102                                     <Tooltip title="More options" disableFocusListener>
103                                         <IconButton
104                                             aria-label="More options"
105                                             onClick={this.handleContextMenu}>
106                                             <MoreOptionsIcon />
107                                         </IconButton>
108                                     </Tooltip>
109                                 }
110                                 title={
111                                     <span>
112                                         <IllegalNamingWarning name={item.name}/>
113                                         {item.name}
114                                         {isWritable || <Chip variant="outlined" icon={<ReadOnlyIcon />} label="Read-only"/>}
115                                     </span>
116                                 }
117                                 titleTypographyProps={this.titleProps}
118                                 subheader={item.description}
119                                 subheaderTypographyProps={this.titleProps} />
120                             <CardContent>
121                                 <Grid container direction="column">
122                                     <Grid item xs={10}>
123                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
124                                             label='Collection UUID'
125                                             linkToUuid={item.uuid} />
126                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
127                                             label='Portable data hash'
128                                             linkToUuid={item.portableDataHash} />
129                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
130                                             label='Number of files' value={item.fileCount} />
131                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
132                                             label='Content size' value={formatFileSize(item.fileSizeTotal)} />
133                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
134                                             label='Owner' linkToUuid={item.ownerUuid} />
135                                         {(item.properties.container_request || item.properties.containerRequest) &&
136                                             <span onClick={() => dispatch<any>(navigateToProcess(item.properties.container_request || item.properties.containerRequest))}>
137                                                 <DetailsAttribute classLabel={classes.link} label='Link to process' />
138                                             </span>
139                                         }
140                                     </Grid>
141                                 </Grid>
142                             </CardContent>
143                         </Card>
144
145                         <Card className={classes.card}>
146                             <CardHeader title="Properties" />
147                             <CardContent>
148                                 <Grid container direction="column">
149                                     {isWritable && <Grid item xs={12}>
150                                         <CollectionTagForm />
151                                     </Grid>}
152                                     <Grid item xs={12}>
153                                     { Object.keys(item.properties).length > 0
154                                         ? Object.keys(item.properties).map(k =>
155                                             Array.isArray(item.properties[k])
156                                             ? item.properties[k].map((v: string) =>
157                                                 getPropertyChip(
158                                                     k, v,
159                                                     isWritable
160                                                         ? this.handleDelete(k, item.properties[k])
161                                                         : undefined,
162                                                     classes.tag))
163                                             : getPropertyChip(
164                                                 k, item.properties[k],
165                                                 isWritable
166                                                     ? this.handleDelete(k, item.properties[k])
167                                                     : undefined,
168                                                 classes.tag)
169                                         )
170                                         : <div className={classes.centeredLabel}>No properties set on this collection.</div>
171                                     }
172                                     </Grid>
173                                 </Grid>
174                             </CardContent>
175                         </Card>
176                         <div className={classes.card} data-cy="collection-files-panel">
177                             <CollectionPanelFiles isWritable={isWritable} />
178                         </div>
179                     </>
180                     : null;
181             }
182
183             handleContextMenu = (event: React.MouseEvent<any>) => {
184                 const { uuid, ownerUuid, name, description, kind, isTrashed } = this.props.item;
185                 const { isWritable } = this.props;
186                 const resource = {
187                     uuid,
188                     ownerUuid,
189                     name,
190                     description,
191                     kind,
192                     menuKind: isWritable
193                         ? isTrashed
194                             ? ContextMenuKind.TRASHED_COLLECTION
195                             : ContextMenuKind.COLLECTION
196                         : ContextMenuKind.READONLY_COLLECTION
197                 };
198                 this.props.dispatch<any>(openContextMenu(event, resource));
199             }
200
201             onCopy = (message: string) =>
202                 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
203                     message,
204                     hideDuration: 2000,
205                     kind: SnackbarKind.SUCCESS
206                 }))
207
208             handleDelete = (key: string, value: string) => () => {
209                 this.props.dispatch<any>(deleteCollectionTag(key, value));
210             }
211
212             openCollectionDetails = () => {
213                 const { item } = this.props;
214                 if (item) {
215                     this.props.dispatch(openDetailsPanel(item.uuid));
216                 }
217             }
218
219             titleProps = {
220                 onClick: this.openCollectionDetails
221             };
222
223         }
224     )
225 );