16118: Changes read-only icon to be a slash+pencil fontawesome composite.
[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' | 'readOnlyChip';
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     readOnlyChip: {
65         marginLeft: theme.spacing.unit
66     }
67 });
68
69 interface CollectionPanelDataProps {
70     item: CollectionResource;
71     isWritable: boolean;
72 }
73
74 type CollectionPanelProps = CollectionPanelDataProps & DispatchProp
75     & WithStyles<CssRules> & RouteComponentProps<{ id: string }>;
76
77 export const CollectionPanel = withStyles(styles)(
78     connect((state: RootState, props: RouteComponentProps<{ id: string }>) => {
79         const currentUserUUID = getUserUuid(state);
80         const item = getResource<CollectionResource>(props.match.params.id)(state.resources);
81         let isWritable = false;
82         if (item && item.ownerUuid === currentUserUUID) {
83             isWritable = true;
84         } else if (item) {
85             const itemOwner = getResource<GroupResource|UserResource>(item.ownerUuid)(state.resources);
86             if (itemOwner) {
87                 isWritable = itemOwner.writableBy.indexOf(currentUserUUID || '') >= 0;
88             }
89         }
90         return { item, isWritable };
91     })(
92         class extends React.Component<CollectionPanelProps> {
93             render() {
94                 const { classes, item, dispatch, isWritable } = this.props;
95                 return item
96                     ? <>
97                         <Card data-cy='collection-info-panel' className={classes.card}>
98                             <CardHeader
99                                 avatar={
100                                     <IconButton onClick={this.openCollectionDetails}>
101                                         <CollectionIcon className={classes.iconHeader} />
102                                     </IconButton>
103                                 }
104                                 action={
105                                     <Tooltip title="More options" disableFocusListener>
106                                         <IconButton
107                                             data-cy='collection-panel-options-btn'
108                                             aria-label="More options"
109                                             onClick={this.handleContextMenu}>
110                                             <MoreOptionsIcon />
111                                         </IconButton>
112                                     </Tooltip>
113                                 }
114                                 title={
115                                     <span>
116                                         <IllegalNamingWarning name={item.name}/>
117                                         {item.name}
118                                         {isWritable || <Chip variant="outlined" icon={<ReadOnlyIcon className={classes.readOnlyChip}/>} label="Read-only" className={classes.readOnlyChip} />}
119                                     </span>
120                                 }
121                                 titleTypographyProps={this.titleProps}
122                                 subheader={item.description}
123                                 subheaderTypographyProps={this.titleProps} />
124                             <CardContent>
125                                 <Grid container direction="column">
126                                     <Grid item xs={10}>
127                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
128                                             label='Collection UUID'
129                                             linkToUuid={item.uuid} />
130                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
131                                             label='Portable data hash'
132                                             linkToUuid={item.portableDataHash} />
133                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
134                                             label='Number of files' value={item.fileCount} />
135                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
136                                             label='Content size' value={formatFileSize(item.fileSizeTotal)} />
137                                         <DetailsAttribute classLabel={classes.label} classValue={classes.value}
138                                             label='Owner' linkToUuid={item.ownerUuid} />
139                                         {(item.properties.container_request || item.properties.containerRequest) &&
140                                             <span onClick={() => dispatch<any>(navigateToProcess(item.properties.container_request || item.properties.containerRequest))}>
141                                                 <DetailsAttribute classLabel={classes.link} label='Link to process' />
142                                             </span>
143                                         }
144                                     </Grid>
145                                 </Grid>
146                             </CardContent>
147                         </Card>
148
149                         <Card data-cy='collection-properties-panel' className={classes.card}>
150                             <CardHeader title="Properties" />
151                             <CardContent>
152                                 <Grid container direction="column">
153                                     {isWritable && <Grid item xs={12}>
154                                         <CollectionTagForm />
155                                     </Grid>}
156                                     <Grid item xs={12}>
157                                     { Object.keys(item.properties).length > 0
158                                         ? Object.keys(item.properties).map(k =>
159                                             Array.isArray(item.properties[k])
160                                             ? item.properties[k].map((v: string) =>
161                                                 getPropertyChip(
162                                                     k, v,
163                                                     isWritable
164                                                         ? this.handleDelete(k, item.properties[k])
165                                                         : undefined,
166                                                     classes.tag))
167                                             : getPropertyChip(
168                                                 k, item.properties[k],
169                                                 isWritable
170                                                     ? this.handleDelete(k, item.properties[k])
171                                                     : undefined,
172                                                 classes.tag)
173                                         )
174                                         : <div className={classes.centeredLabel}>No properties set on this collection.</div>
175                                     }
176                                     </Grid>
177                                 </Grid>
178                             </CardContent>
179                         </Card>
180                         <div className={classes.card}>
181                             <CollectionPanelFiles isWritable={isWritable} />
182                         </div>
183                     </>
184                     : null;
185             }
186
187             handleContextMenu = (event: React.MouseEvent<any>) => {
188                 const { uuid, ownerUuid, name, description, kind, isTrashed } = this.props.item;
189                 const { isWritable } = this.props;
190                 const resource = {
191                     uuid,
192                     ownerUuid,
193                     name,
194                     description,
195                     kind,
196                     menuKind: isWritable
197                         ? isTrashed
198                             ? ContextMenuKind.TRASHED_COLLECTION
199                             : ContextMenuKind.COLLECTION
200                         : ContextMenuKind.READONLY_COLLECTION
201                 };
202                 this.props.dispatch<any>(openContextMenu(event, resource));
203             }
204
205             onCopy = (message: string) =>
206                 this.props.dispatch(snackbarActions.OPEN_SNACKBAR({
207                     message,
208                     hideDuration: 2000,
209                     kind: SnackbarKind.SUCCESS
210                 }))
211
212             handleDelete = (key: string, value: string) => () => {
213                 this.props.dispatch<any>(deleteCollectionTag(key, value));
214             }
215
216             openCollectionDetails = () => {
217                 const { item } = this.props;
218                 if (item) {
219                     this.props.dispatch(openDetailsPanel(item.uuid));
220                 }
221             }
222
223             titleProps = {
224                 onClick: this.openCollectionDetails
225             };
226
227         }
228     )
229 );