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