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