16439: Disables +NEW button even on trashed subprojects.
[arvados-workbench2.git] / src / views-components / side-panel-button / side-panel-button.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 { connect, DispatchProp } from 'react-redux';
7 import { RootState } from '~/store/store';
8 import { ArvadosTheme } from '~/common/custom-theme';
9 import { PopoverOrigin } from '@material-ui/core/Popover';
10 import { StyleRulesCallback, WithStyles, withStyles, Toolbar, Grid, Button, MenuItem, Menu } from '@material-ui/core';
11 import { AddIcon, CollectionIcon, ProcessIcon, ProjectIcon } from '~/components/icon/icon';
12 import { openProjectCreateDialog } from '~/store/projects/project-create-actions';
13 import { openCollectionCreateDialog } from '~/store/collections/collection-create-actions';
14 import { navigateToRunProcess } from '~/store/navigation/navigation-action';
15 import { runProcessPanelActions } from '~/store/run-process-panel/run-process-panel-actions';
16 import { getUserUuid } from '~/common/getuser';
17 import { matchProjectRoute } from '~/routes/routes';
18 import { GroupResource } from '~/models/group';
19 import { ResourcesState, getResource } from '~/store/resources/resources';
20 import { extractUuidKind, ResourceKind } from '~/models/resource';
21
22 type CssRules = 'button' | 'menuItem' | 'icon';
23
24 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
25     button: {
26         boxShadow: 'none',
27         padding: '2px 10px 2px 5px',
28         fontSize: '0.75rem'
29     },
30     menuItem: {
31         fontSize: '0.875rem',
32         color: theme.palette.grey["700"]
33     },
34     icon: {
35         marginRight: theme.spacing.unit
36     }
37 });
38
39 interface SidePanelDataProps {
40     location: any;
41     currentItemId: string;
42     resources: ResourcesState;
43     currentUserUUID: string | undefined;
44 }
45
46 interface SidePanelState {
47     anchorEl: any;
48 }
49
50 type SidePanelProps = SidePanelDataProps & DispatchProp & WithStyles<CssRules>;
51
52 const transformOrigin: PopoverOrigin = {
53     vertical: -50,
54     horizontal: 0
55 };
56
57 const isProjectTrashed = (proj: GroupResource, resources: ResourcesState): boolean => {
58     if (proj.isTrashed) { return true; }
59     if (extractUuidKind(proj.ownerUuid) === ResourceKind.USER) { return false; }
60     const parentProj = getResource<GroupResource>(proj.ownerUuid)(resources);
61     return isProjectTrashed(parentProj!, resources);
62 };
63
64 export const SidePanelButton = withStyles(styles)(
65     connect((state: RootState) => ({
66         currentItemId: state.router.location
67             ? state.router.location.pathname.split('/').slice(-1)[0]
68             : null,
69         location: state.router.location,
70         resources: state.resources,
71         currentUserUUID: getUserUuid(state),
72     }))(
73         class extends React.Component<SidePanelProps> {
74
75             state: SidePanelState = {
76                 anchorEl: undefined
77             };
78
79             render() {
80                 const { classes, location, resources, currentUserUUID, currentItemId } = this.props;
81                 const { anchorEl } = this.state;
82                 let enabled = false;
83                 if (currentItemId === currentUserUUID) {
84                     enabled = true;
85                 } else if (matchProjectRoute(location ? location.pathname : '')) {
86                     const currentProject = getResource<GroupResource>(currentItemId)(resources);
87                     if (currentProject &&
88                         currentProject.writableBy.indexOf(currentUserUUID || '') >= 0 &&
89                         !isProjectTrashed(currentProject, resources)) {
90                         enabled = true;
91                     }
92                 }
93                 return <Toolbar>
94                     <Grid container>
95                         <Grid container item xs alignItems="center" justify="flex-start">
96                             <Button variant="contained" disabled={!enabled}
97                                 color="primary" size="small" className={classes.button}
98                                 aria-owns={anchorEl ? 'aside-menu-list' : undefined}
99                                 aria-haspopup="true"
100                                 onClick={this.handleOpen}>
101                                 <AddIcon />
102                                 New
103                             </Button>
104                             <Menu
105                                 id='aside-menu-list'
106                                 anchorEl={anchorEl}
107                                 open={Boolean(anchorEl)}
108                                 onClose={this.handleClose}
109                                 onClick={this.handleClose}
110                                 transformOrigin={transformOrigin}>
111                                 <MenuItem className={classes.menuItem} onClick={this.handleNewCollectionClick}>
112                                     <CollectionIcon className={classes.icon} /> New collection
113                                 </MenuItem>
114                                 <MenuItem className={classes.menuItem} onClick={this.handleRunProcessClick}>
115                                     <ProcessIcon className={classes.icon} /> Run a process
116                                 </MenuItem>
117                                 <MenuItem className={classes.menuItem} onClick={this.handleNewProjectClick}>
118                                     <ProjectIcon className={classes.icon} /> New project
119                                 </MenuItem>
120                             </Menu>
121                         </Grid>
122                     </Grid>
123                 </Toolbar>;
124             }
125
126             handleNewProjectClick = () => {
127                 this.props.dispatch<any>(openProjectCreateDialog(this.props.currentItemId));
128             }
129
130             handleRunProcessClick = () => {
131                 const location = this.props.location;
132                 this.props.dispatch(runProcessPanelActions.RESET_RUN_PROCESS_PANEL());
133                 this.props.dispatch(runProcessPanelActions.SET_PROCESS_PATHNAME(location.pathname));
134                 this.props.dispatch(runProcessPanelActions.SET_PROCESS_OWNER_UUID(this.props.currentItemId));
135
136                 this.props.dispatch<any>(navigateToRunProcess);
137             }
138
139             handleNewCollectionClick = () => {
140                 this.props.dispatch<any>(openCollectionCreateDialog(this.props.currentItemId));
141             }
142
143             handleClose = () => {
144                 this.setState({ anchorEl: undefined });
145             }
146
147             handleOpen = (event: React.MouseEvent<HTMLButtonElement>) => {
148                 this.setState({ anchorEl: event.currentTarget });
149             }
150         }
151     )
152 );