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 '@mui/material/Popover';
10 import { CustomStyleRulesCallback } from 'common/custom-theme';
11 import { Toolbar, Grid, Button, MenuItem, Menu } from '@mui/material';
12 import { WithStyles } from '@mui/styles';
13 import withStyles from '@mui/styles/withStyles';
14 import { AddIcon, CollectionIcon, ProcessIcon, ProjectIcon } from 'components/icon/icon';
15 import { openProjectCreateDialog } from 'store/projects/project-create-actions';
16 import { openCollectionCreateDialog } from 'store/collections/collection-create-actions';
17 import { navigateToRunProcess } from 'store/navigation/navigation-action';
18 import { runProcessPanelActions } from 'store/run-process-panel/run-process-panel-actions';
19 import { getUserUuid } from 'common/getuser';
20 import { matchProjectRoute } from 'routes/routes';
21 import { GroupClass, GroupResource } from 'models/group';
22 import { ResourcesState, getResource } from 'store/resources/resources';
23 import { extractUuidKind, ResourceKind } from 'models/resource';
24 import { pluginConfig } from 'plugins';
25 import { ElementListReducer } from 'common/plugintypes';
26 import { Location } from 'history';
27 import { ProjectResource } from 'models/project';
29 type CssRules = 'button' | 'menuItem' | 'icon';
31 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
34 padding: '2px 10px 2px 5px',
39 color: theme.palette.grey["700"]
42 marginRight: theme.spacing(1)
46 interface SidePanelDataProps {
48 currentItemId: string;
49 resources: ResourcesState;
50 currentUserUUID: string | undefined;
53 interface SidePanelState {
57 type SidePanelProps = SidePanelDataProps & DispatchProp & WithStyles<CssRules>;
59 const transformOrigin: PopoverOrigin = {
64 export const isProjectTrashed = (proj: GroupResource | undefined, resources: ResourcesState): boolean => {
65 if (proj === undefined) { return false; }
66 if (proj.isTrashed) { return true; }
67 if (extractUuidKind(proj.ownerUuid) === ResourceKind.USER) { return false; }
68 const parentProj = getResource<GroupResource>(proj.ownerUuid)(resources);
69 return isProjectTrashed(parentProj, resources);
72 export const SidePanelButton = withStyles(styles)(
73 connect((state: RootState) => ({
74 currentItemId: state.router.location
75 ? state.router.location.pathname.split('/').slice(-1)[0]
77 location: state.router.location,
78 resources: state.resources,
79 currentUserUUID: getUserUuid(state),
81 class extends React.Component<SidePanelProps> {
83 state: SidePanelState = {
88 const { classes, location, resources, currentUserUUID, currentItemId } = this.props;
89 const { anchorEl } = this.state;
91 if (currentItemId === currentUserUUID) {
93 } else if (matchProjectRoute(location ? location.pathname : '')) {
94 const currentProject = getResource<ProjectResource>(currentItemId)(resources);
95 if (currentProject && currentProject.canWrite &&
96 !currentProject.frozenByUuid &&
97 !isProjectTrashed(currentProject, resources) &&
98 currentProject.groupClass !== GroupClass.FILTER) {
103 for (const enableFn of pluginConfig.enableNewButtonMatchers) {
104 if (enableFn(location, currentItemId, currentUserUUID, resources)) {
110 <MenuItem data-cy='side-panel-new-collection' className={classes.menuItem} onClick={this.handleNewCollectionClick}>
111 <CollectionIcon className={classes.icon} /> New collection
113 <MenuItem data-cy='side-panel-run-process' className={classes.menuItem} onClick={this.handleRunProcessClick}>
114 <ProcessIcon className={classes.icon} /> Run a workflow
116 <MenuItem data-cy='side-panel-new-project' className={classes.menuItem} onClick={this.handleNewProjectClick}>
117 <ProjectIcon className={classes.icon} /> New project
121 const reduceItemsFn: (a: React.ReactElement[], b: ElementListReducer) => React.ReactElement[] =
122 (a, b) => b(a, classes.menuItem);
124 menuItems = React.createElement(React.Fragment, null,
125 pluginConfig.newButtonMenuList.reduce(reduceItemsFn, React.Children.toArray(menuItems.props.children)));
128 <Toolbar style={{paddingRight: 0}}>
130 <Grid container item xs alignItems="center" justifyContent="flex-start">
131 <Button data-cy="side-panel-button" variant="contained" disabled={!enabled}
132 color="primary" size="small" className={classes.button}
133 aria-owns={anchorEl ? 'aside-menu-list' : undefined}
135 onClick={this.handleOpen}>
142 open={Boolean(anchorEl)}
143 onClose={this.handleClose}
144 onClick={this.handleClose}
145 transformOrigin={transformOrigin}>
154 handleNewProjectClick = () => {
155 this.props.dispatch<any>(openProjectCreateDialog(this.props.currentItemId));
158 handleRunProcessClick = () => {
159 const location = this.props.location;
160 this.props.dispatch(runProcessPanelActions.RESET_RUN_PROCESS_PANEL());
161 this.props.dispatch(runProcessPanelActions.SET_PROCESS_PATHNAME(location.pathname));
162 this.props.dispatch(runProcessPanelActions.SET_PROCESS_OWNER_UUID(this.props.currentItemId));
164 this.props.dispatch<any>(navigateToRunProcess);
167 handleNewCollectionClick = () => {
168 this.props.dispatch<any>(openCollectionCreateDialog(this.props.currentItemId));
171 handleClose = () => {
172 this.setState({ anchorEl: undefined });
175 handleOpen = (event: React.MouseEvent<HTMLButtonElement>) => {
176 this.setState({ anchorEl: event.currentTarget });