merge conflicts
[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 { getProperty } from '~/store/properties/properties';
9 import { PROJECT_PANEL_CURRENT_UUID } from '~/store/project-panel/project-panel-action';
10 import { ArvadosTheme } from '~/common/custom-theme';
11 import { PopoverOrigin } from '@material-ui/core/Popover';
12 import { StyleRulesCallback, WithStyles, withStyles, Toolbar, Grid, Button, MenuItem, Menu } from '@material-ui/core';
13 import { AddIcon, CollectionIcon, ProcessIcon, ProjectIcon } from '~/components/icon/icon';
14 import { openProjectCreateDialog } from '~/store/projects/project-create-actions';
15 import { openCollectionCreateDialog } from '~/store/collections/collection-create-actions';
16 import { matchProjectRoute } from '~/routes/routes';
17 import { navigateToRunProcess } from '~/store/navigation/navigation-action';
18 import { runProcessPanelActions } from '~/store/run-process-panel/run-process-panel-actions';
19
20 type CssRules = 'button' | 'menuItem' | 'icon';
21
22 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
23     button: {
24         boxShadow: 'none',
25         padding: '2px 10px 2px 5px',
26         fontSize: '0.75rem'
27     },
28     menuItem: {
29         fontSize: '0.875rem',
30         color: theme.palette.grey["700"]
31     },
32     icon: {
33         marginRight: theme.spacing.unit
34     }
35 });
36
37 interface SidePanelDataProps {
38     currentItemId: string;
39     buttonVisible: boolean;
40 }
41
42 interface SidePanelState {
43     anchorEl: any;
44 }
45
46 type SidePanelProps = SidePanelDataProps & DispatchProp & WithStyles<CssRules>;
47
48 const transformOrigin: PopoverOrigin = {
49     vertical: -50,
50     horizontal: 0
51 };
52
53 const isButtonVisible = ({ router }: RootState) => {
54     const pathname = router.location ? router.location.pathname : '';
55     const match = matchProjectRoute(pathname);
56     return !!match;
57 };
58
59 export const SidePanelButton = withStyles(styles)(
60     connect((state: RootState) => ({
61         currentItemId: getProperty(PROJECT_PANEL_CURRENT_UUID)(state.properties),
62         buttonVisible: isButtonVisible(state)
63     }))(
64         class extends React.Component<SidePanelProps> {
65
66             state: SidePanelState = {
67                 anchorEl: undefined
68             };
69
70             render() {
71                 const { classes, buttonVisible } = this.props;
72                 const { anchorEl } = this.state;
73                 return <Toolbar>
74                     {buttonVisible && <Grid container>
75                         <Grid container item xs alignItems="center" justify="flex-start">
76                             <Button variant="contained" color="primary" size="small" className={classes.button}
77                                 aria-owns={anchorEl ? 'aside-menu-list' : undefined}
78                                 aria-haspopup="true"
79                                 onClick={this.handleOpen}>
80                                 <AddIcon />
81                                 New
82                             </Button>
83                             <Menu
84                                 id='aside-menu-list'
85                                 anchorEl={anchorEl}
86                                 open={Boolean(anchorEl)}
87                                 onClose={this.handleClose}
88                                 onClick={this.handleClose}
89                                 transformOrigin={transformOrigin}>
90                                 <MenuItem className={classes.menuItem} onClick={this.handleNewCollectionClick}>
91                                     <CollectionIcon className={classes.icon} /> New collection
92                                 </MenuItem>
93                                 <MenuItem className={classes.menuItem} onClick={this.handleRunProcessClick}>
94                                     <ProcessIcon className={classes.icon} /> Run a process
95                                 </MenuItem>
96                                 <MenuItem className={classes.menuItem} onClick={this.handleNewProjectClick}>
97                                     <ProjectIcon className={classes.icon} /> New project
98                                 </MenuItem>
99                             </Menu>
100                         </Grid>
101                     </Grid>}
102                 </Toolbar>;
103             }
104
105             handleNewProjectClick = () => {
106                 this.props.dispatch<any>(openProjectCreateDialog(this.props.currentItemId));
107             }
108
109             handleRunProcessClick = () => {
110                 this.props.dispatch(runProcessPanelActions.SET_PROCESS_OWNER_UUID(this.props.currentItemId));
111                 this.props.dispatch<any>(navigateToRunProcess);
112             }
113
114             handleNewCollectionClick = () => {
115                 this.props.dispatch<any>(openCollectionCreateDialog(this.props.currentItemId));
116             }
117
118             handleClose = () => {
119                 this.setState({ anchorEl: undefined });
120             }
121
122             handleOpen = (event: React.MouseEvent<HTMLButtonElement>) => {
123                 this.setState({ anchorEl: event.currentTarget });
124             }
125         }
126     )
127 );