1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as 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 { GroupResource } from '~/models/group';
19 import { ResourcesState, getResource } from '~/store/resources/resources';
21 type CssRules = 'button' | 'menuItem' | 'icon';
23 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
26 padding: '2px 10px 2px 5px',
31 color: theme.palette.grey["700"]
34 marginRight: theme.spacing.unit
38 interface SidePanelDataProps {
40 currentItemId: string;
41 resources: ResourcesState;
42 currentUserUUID: string | undefined;
45 interface SidePanelState {
49 type SidePanelProps = SidePanelDataProps & DispatchProp & WithStyles<CssRules>;
51 const transformOrigin: PopoverOrigin = {
56 export const SidePanelButton = withStyles(styles)(
57 connect((state: RootState) => ({
58 currentItemId: state.router.location
59 ? state.router.location.pathname.split('/').slice(-1)[0]
61 location: state.router.location,
62 resources: state.resources,
63 currentUserUUID: getUserUuid(state),
65 class extends React.Component<SidePanelProps> {
67 state: SidePanelState = {
72 const { classes, location, resources, currentUserUUID, currentItemId } = this.props;
73 const { anchorEl } = this.state;
75 if (currentItemId === currentUserUUID) {
77 } else if (matchProjectRoute(location ? location.pathname : '')) {
78 const currentProject = getResource<GroupResource>(currentItemId)(resources);
80 currentProject.writableBy.indexOf(currentUserUUID || '') >= 0 &&
81 !currentProject.isTrashed) {
87 <Grid container item xs alignItems="center" justify="flex-start">
88 <Button variant="contained" disabled={!enabled}
89 color="primary" size="small" className={classes.button}
90 aria-owns={anchorEl ? 'aside-menu-list' : undefined}
92 onClick={this.handleOpen}>
99 open={Boolean(anchorEl)}
100 onClose={this.handleClose}
101 onClick={this.handleClose}
102 transformOrigin={transformOrigin}>
103 <MenuItem className={classes.menuItem} onClick={this.handleNewCollectionClick}>
104 <CollectionIcon className={classes.icon} /> New collection
106 <MenuItem className={classes.menuItem} onClick={this.handleRunProcessClick}>
107 <ProcessIcon className={classes.icon} /> Run a process
109 <MenuItem className={classes.menuItem} onClick={this.handleNewProjectClick}>
110 <ProjectIcon className={classes.icon} /> New project
118 handleNewProjectClick = () => {
119 this.props.dispatch<any>(openProjectCreateDialog(this.props.currentItemId));
122 handleRunProcessClick = () => {
123 const location = this.props.location;
124 this.props.dispatch(runProcessPanelActions.RESET_RUN_PROCESS_PANEL());
125 this.props.dispatch(runProcessPanelActions.SET_PROCESS_PATHNAME(location.pathname));
126 this.props.dispatch(runProcessPanelActions.SET_PROCESS_OWNER_UUID(this.props.currentItemId));
128 this.props.dispatch<any>(navigateToRunProcess);
131 handleNewCollectionClick = () => {
132 this.props.dispatch<any>(openCollectionCreateDialog(this.props.currentItemId));
135 handleClose = () => {
136 this.setState({ anchorEl: undefined });
139 handleOpen = (event: React.MouseEvent<HTMLButtonElement>) => {
140 this.setState({ anchorEl: event.currentTarget });