1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import 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 { GroupClass, GroupResource } from 'models/group';
19 import { ResourcesState, getResource } from 'store/resources/resources';
20 import { extractUuidKind, ResourceKind } from 'models/resource';
21 import { pluginConfig } from 'plugins';
22 import { ElementListReducer } from 'common/plugintypes';
23 import { Location } from 'history';
24 import { ProjectResource } from 'models/project';
26 type CssRules = 'button' | 'menuItem' | 'icon';
28 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
31 padding: '2px 10px 2px 5px',
36 color: theme.palette.grey["700"]
39 marginRight: theme.spacing.unit
43 interface SidePanelDataProps {
45 currentItemId: string;
46 resources: ResourcesState;
47 currentUserUUID: string | undefined;
50 interface SidePanelState {
54 type SidePanelProps = SidePanelDataProps & DispatchProp & WithStyles<CssRules>;
56 const transformOrigin: PopoverOrigin = {
61 export const isProjectTrashed = (proj: GroupResource | undefined, resources: ResourcesState): boolean => {
62 if (proj === undefined) { return false; }
63 if (proj.isTrashed) { return true; }
64 if (extractUuidKind(proj.ownerUuid) === ResourceKind.USER) { return false; }
65 const parentProj = getResource<GroupResource>(proj.ownerUuid)(resources);
66 return isProjectTrashed(parentProj, resources);
69 export const SidePanelButton = withStyles(styles)(
70 connect((state: RootState) => ({
71 currentItemId: state.router.location
72 ? state.router.location.pathname.split('/').slice(-1)[0]
74 location: state.router.location,
75 resources: state.resources,
76 currentUserUUID: getUserUuid(state),
78 class extends React.Component<SidePanelProps> {
80 state: SidePanelState = {
85 const { classes, location, resources, currentUserUUID, currentItemId } = this.props;
86 const { anchorEl } = this.state;
88 if (currentItemId === currentUserUUID) {
90 } else if (matchProjectRoute(location ? location.pathname : '')) {
91 const currentProject = getResource<ProjectResource>(currentItemId)(resources);
92 if (currentProject && currentProject.canWrite &&
93 !currentProject.frozenByUuid &&
94 !isProjectTrashed(currentProject, resources) &&
95 currentProject.groupClass !== GroupClass.FILTER) {
100 for (const enableFn of pluginConfig.enableNewButtonMatchers) {
101 if (enableFn(location, currentItemId, currentUserUUID, resources)) {
107 <MenuItem data-cy='side-panel-new-collection' className={classes.menuItem} onClick={this.handleNewCollectionClick}>
108 <CollectionIcon className={classes.icon} /> New collection
110 <MenuItem data-cy='side-panel-run-process' className={classes.menuItem} onClick={this.handleRunProcessClick}>
111 <ProcessIcon className={classes.icon} /> Run a workflow
113 <MenuItem data-cy='side-panel-new-project' className={classes.menuItem} onClick={this.handleNewProjectClick}>
114 <ProjectIcon className={classes.icon} /> New project
118 const reduceItemsFn: (a: React.ReactElement[], b: ElementListReducer) => React.ReactElement[] =
119 (a, b) => b(a, classes.menuItem);
121 menuItems = React.createElement(React.Fragment, null,
122 pluginConfig.newButtonMenuList.reduce(reduceItemsFn, React.Children.toArray(menuItems.props.children)));
124 return <Toolbar style={{paddingRight: 0}}>
126 <Grid container item xs alignItems="center" justify="flex-start">
127 <Button data-cy="side-panel-button" variant="contained" disabled={!enabled}
128 color="primary" size="small" className={classes.button}
129 aria-owns={anchorEl ? 'aside-menu-list' : undefined}
131 onClick={this.handleOpen}>
138 open={Boolean(anchorEl)}
139 onClose={this.handleClose}
140 onClick={this.handleClose}
141 transformOrigin={transformOrigin}>
149 handleNewProjectClick = () => {
150 this.props.dispatch<any>(openProjectCreateDialog(this.props.currentItemId));
153 handleRunProcessClick = () => {
154 const location = this.props.location;
155 this.props.dispatch(runProcessPanelActions.RESET_RUN_PROCESS_PANEL());
156 this.props.dispatch(runProcessPanelActions.SET_PROCESS_PATHNAME(location.pathname));
157 this.props.dispatch(runProcessPanelActions.SET_PROCESS_OWNER_UUID(this.props.currentItemId));
159 this.props.dispatch<any>(navigateToRunProcess);
162 handleNewCollectionClick = () => {
163 this.props.dispatch<any>(openCollectionCreateDialog(this.props.currentItemId));
166 handleClose = () => {
167 this.setState({ anchorEl: undefined });
170 handleOpen = (event: React.MouseEvent<HTMLButtonElement>) => {
171 this.setState({ anchorEl: event.currentTarget });