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