Merge branch '14602_admin_compute_node_paginations'
[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 { navigateToRunProcess } from '~/store/navigation/navigation-action';
17 import { runProcessPanelActions } from '~/store/run-process-panel/run-process-panel-actions';
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 }
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: 0
49 };
50
51 export const SidePanelButton = withStyles(styles)(
52     connect((state: RootState) => ({
53         currentItemId: getProperty(PROJECT_PANEL_CURRENT_UUID)(state.properties)
54     }))(
55         class extends React.Component<SidePanelProps> {
56
57             state: SidePanelState = {
58                 anchorEl: undefined
59             };
60
61             render() {
62                 const { classes } = this.props;
63                 const { anchorEl } = this.state;
64                 return <Toolbar>
65                     <Grid container>
66                         <Grid container item xs alignItems="center" justify="flex-start">
67                             <Button variant="contained" color="primary" size="small" className={classes.button}
68                                 aria-owns={anchorEl ? 'aside-menu-list' : undefined}
69                                 aria-haspopup="true"
70                                 onClick={this.handleOpen}>
71                                 <AddIcon />
72                                 New
73                             </Button>
74                             <Menu
75                                 id='aside-menu-list'
76                                 anchorEl={anchorEl}
77                                 open={Boolean(anchorEl)}
78                                 onClose={this.handleClose}
79                                 onClick={this.handleClose}
80                                 transformOrigin={transformOrigin}>
81                                 <MenuItem className={classes.menuItem} onClick={this.handleNewCollectionClick}>
82                                     <CollectionIcon className={classes.icon} /> New collection
83                                 </MenuItem>
84                                 <MenuItem className={classes.menuItem} onClick={this.handleRunProcessClick}>
85                                     <ProcessIcon className={classes.icon} /> Run a process
86                                 </MenuItem>
87                                 <MenuItem className={classes.menuItem} onClick={this.handleNewProjectClick}>
88                                     <ProjectIcon className={classes.icon} /> New project
89                                 </MenuItem>
90                             </Menu>
91                         </Grid>
92                     </Grid>
93                 </Toolbar>;
94             }
95
96             handleNewProjectClick = () => {
97                 this.props.dispatch<any>(openProjectCreateDialog(this.props.currentItemId));
98             }
99
100             handleRunProcessClick = () => {
101                 this.props.dispatch(runProcessPanelActions.SET_PROCESS_OWNER_UUID(this.props.currentItemId));
102                 this.props.dispatch<any>(navigateToRunProcess);
103             }
104
105             handleNewCollectionClick = () => {
106                 this.props.dispatch<any>(openCollectionCreateDialog(this.props.currentItemId));
107             }
108
109             handleClose = () => {
110                 this.setState({ anchorEl: undefined });
111             }
112
113             handleOpen = (event: React.MouseEvent<HTMLButtonElement>) => {
114                 this.setState({ anchorEl: event.currentTarget });
115             }
116         }
117     )
118 );