Merge branch '16115-sharing-links'. Closes #16115
[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 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 { GroupClass, GroupResource } from 'models/group';
19 import { ResourcesState, getResource } from 'store/resources/resources';
20 import { extractUuidKind, ResourceKind } from 'models/resource';
21 import { pluginConfig } from 'plugins';
22 import { ElementListReducer } from 'common/plugintypes';
23 import { Location } from 'history';
24
25 type CssRules = 'button' | 'menuItem' | 'icon';
26
27 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
28     button: {
29         boxShadow: 'none',
30         padding: '2px 10px 2px 5px',
31         fontSize: '0.75rem'
32     },
33     menuItem: {
34         fontSize: '0.875rem',
35         color: theme.palette.grey["700"]
36     },
37     icon: {
38         marginRight: theme.spacing.unit
39     }
40 });
41
42 interface SidePanelDataProps {
43     location: Location;
44     currentItemId: string;
45     resources: ResourcesState;
46     currentUserUUID: string | undefined;
47 }
48
49 interface SidePanelState {
50     anchorEl: any;
51 }
52
53 type SidePanelProps = SidePanelDataProps & DispatchProp & WithStyles<CssRules>;
54
55 const transformOrigin: PopoverOrigin = {
56     vertical: -50,
57     horizontal: 0
58 };
59
60 export const isProjectTrashed = (proj: GroupResource | undefined, resources: ResourcesState): boolean => {
61     if (proj === undefined) { return false; }
62     if (proj.isTrashed) { return true; }
63     if (extractUuidKind(proj.ownerUuid) === ResourceKind.USER) { return false; }
64     const parentProj = getResource<GroupResource>(proj.ownerUuid)(resources);
65     return isProjectTrashed(parentProj, resources);
66 };
67
68 export const SidePanelButton = withStyles(styles)(
69     connect((state: RootState) => ({
70         currentItemId: state.router.location
71             ? state.router.location.pathname.split('/').slice(-1)[0]
72             : null,
73         location: state.router.location,
74         resources: state.resources,
75         currentUserUUID: getUserUuid(state),
76     }))(
77         class extends React.Component<SidePanelProps> {
78
79             state: SidePanelState = {
80                 anchorEl: undefined
81             };
82
83             render() {
84                 const { classes, location, resources, currentUserUUID, currentItemId } = this.props;
85                 const { anchorEl } = this.state;
86                 let enabled = false;
87                 if (currentItemId === currentUserUUID) {
88                     enabled = true;
89                 } else if (matchProjectRoute(location ? location.pathname : '')) {
90                     const currentProject = getResource<GroupResource>(currentItemId)(resources);
91                     if (currentProject &&
92                         currentProject.writableBy.indexOf(currentUserUUID || '') >= 0 &&
93                         !isProjectTrashed(currentProject, resources) &&
94                         currentProject.groupClass !== GroupClass.FILTER) {
95                         enabled = true;
96                     }
97                 }
98
99                 for (const enableFn of pluginConfig.enableNewButtonMatchers) {
100                     if (enableFn(location, currentItemId, currentUserUUID, resources)) {
101                         enabled = true;
102                     }
103                 }
104
105                 let menuItems = <>
106                     <MenuItem data-cy='side-panel-new-collection' className={classes.menuItem} onClick={this.handleNewCollectionClick}>
107                         <CollectionIcon className={classes.icon} /> New collection
108                     </MenuItem>
109                     <MenuItem data-cy='side-panel-run-process' className={classes.menuItem} onClick={this.handleRunProcessClick}>
110                         <ProcessIcon className={classes.icon} /> Run a process
111                     </MenuItem>
112                     <MenuItem data-cy='side-panel-new-project' className={classes.menuItem} onClick={this.handleNewProjectClick}>
113                         <ProjectIcon className={classes.icon} /> New project
114                     </MenuItem>
115                 </>;
116
117                 const reduceItemsFn: (a: React.ReactElement[], b: ElementListReducer) => React.ReactElement[] =
118                     (a, b) => b(a, classes.menuItem);
119
120                 menuItems = React.createElement(React.Fragment, null,
121                     pluginConfig.newButtonMenuList.reduce(reduceItemsFn, React.Children.toArray(menuItems.props.children)));
122
123                 return <Toolbar>
124                     <Grid container>
125                         <Grid container item xs alignItems="center" justify="flex-start">
126                             <Button data-cy="side-panel-button" variant="contained" disabled={!enabled}
127                                 color="primary" size="small" className={classes.button}
128                                 aria-owns={anchorEl ? 'aside-menu-list' : undefined}
129                                 aria-haspopup="true"
130                                 onClick={this.handleOpen}>
131                                 <AddIcon />
132                                 New
133                             </Button>
134                             <Menu
135                                 id='aside-menu-list'
136                                 anchorEl={anchorEl}
137                                 open={Boolean(anchorEl)}
138                                 onClose={this.handleClose}
139                                 onClick={this.handleClose}
140                                 transformOrigin={transformOrigin}>
141                                 {menuItems}
142                             </Menu>
143                         </Grid>
144                     </Grid>
145                 </Toolbar>;
146             }
147
148             handleNewProjectClick = () => {
149                 this.props.dispatch<any>(openProjectCreateDialog(this.props.currentItemId));
150             }
151
152             handleRunProcessClick = () => {
153                 const location = this.props.location;
154                 this.props.dispatch(runProcessPanelActions.RESET_RUN_PROCESS_PANEL());
155                 this.props.dispatch(runProcessPanelActions.SET_PROCESS_PATHNAME(location.pathname));
156                 this.props.dispatch(runProcessPanelActions.SET_PROCESS_OWNER_UUID(this.props.currentItemId));
157
158                 this.props.dispatch<any>(navigateToRunProcess);
159             }
160
161             handleNewCollectionClick = () => {
162                 this.props.dispatch<any>(openCollectionCreateDialog(this.props.currentItemId));
163             }
164
165             handleClose = () => {
166                 this.setState({ anchorEl: undefined });
167             }
168
169             handleOpen = (event: React.MouseEvent<HTMLButtonElement>) => {
170                 this.setState({ anchorEl: event.currentTarget });
171             }
172         }
173     )
174 );