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';
25 type CssRules = 'button' | 'menuItem' | 'icon';
27 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
30 padding: '2px 10px 2px 5px',
35 color: theme.palette.grey["700"]
38 marginRight: theme.spacing.unit
42 interface SidePanelDataProps {
44 currentItemId: string;
45 resources: ResourcesState;
46 currentUserUUID: string | undefined;
49 interface SidePanelState {
53 type SidePanelProps = SidePanelDataProps & DispatchProp & WithStyles<CssRules>;
55 const transformOrigin: PopoverOrigin = {
60 export const isProjectTrashed = (proj: GroupResource | undefined, resources: ResourcesState): boolean => {
61 if (proj === undefined) { return false; }
62 if (proj.isTrashed) { return true; }
63 if (extractUuidKind(proj.ownerUuid) === ResourceKind.USER) { return false; }
64 const parentProj = getResource<GroupResource>(proj.ownerUuid)(resources);
65 return isProjectTrashed(parentProj, resources);
68 export const SidePanelButton = withStyles(styles)(
69 connect((state: RootState) => ({
70 currentItemId: state.router.location
71 ? state.router.location.pathname.split('/').slice(-1)[0]
73 location: state.router.location,
74 resources: state.resources,
75 currentUserUUID: getUserUuid(state),
77 class extends React.Component<SidePanelProps> {
79 state: SidePanelState = {
84 const { classes, location, resources, currentUserUUID, currentItemId } = this.props;
85 const { anchorEl } = this.state;
87 if (currentItemId === currentUserUUID) {
89 } else if (matchProjectRoute(location ? location.pathname : '')) {
90 const currentProject = getResource<GroupResource>(currentItemId)(resources);
92 currentProject.writableBy.indexOf(currentUserUUID || '') >= 0 &&
93 !isProjectTrashed(currentProject, resources) &&
94 currentProject.groupClass !== GroupClass.FILTER) {
99 for (const enableFn of pluginConfig.enableNewButtonMatchers) {
100 if (enableFn(location, currentItemId, currentUserUUID, resources)) {
106 <MenuItem data-cy='side-panel-new-collection' className={classes.menuItem} onClick={this.handleNewCollectionClick}>
107 <CollectionIcon className={classes.icon} /> New collection
109 <MenuItem data-cy='side-panel-run-process' className={classes.menuItem} onClick={this.handleRunProcessClick}>
110 <ProcessIcon className={classes.icon} /> Run a process
112 <MenuItem data-cy='side-panel-new-project' className={classes.menuItem} onClick={this.handleNewProjectClick}>
113 <ProjectIcon className={classes.icon} /> New project
117 const reduceItemsFn: (a: React.ReactElement[], b: ElementListReducer) => React.ReactElement[] =
118 (a, b) => b(a, classes.menuItem);
120 menuItems = React.createElement(React.Fragment, null,
121 pluginConfig.newButtonMenuList.reduce(reduceItemsFn, React.Children.toArray(menuItems.props.children)));
125 <Grid container item xs alignItems="center" justify="flex-start">
126 <Button data-cy="side-panel-button" variant="contained" disabled={!enabled}
127 color="primary" size="small" className={classes.button}
128 aria-owns={anchorEl ? 'aside-menu-list' : undefined}
130 onClick={this.handleOpen}>
137 open={Boolean(anchorEl)}
138 onClose={this.handleClose}
139 onClick={this.handleClose}
140 transformOrigin={transformOrigin}>
148 handleNewProjectClick = () => {
149 this.props.dispatch<any>(openProjectCreateDialog(this.props.currentItemId));
152 handleRunProcessClick = () => {
153 const location = this.props.location;
154 this.props.dispatch(runProcessPanelActions.RESET_RUN_PROCESS_PANEL());
155 this.props.dispatch(runProcessPanelActions.SET_PROCESS_PATHNAME(location.pathname));
156 this.props.dispatch(runProcessPanelActions.SET_PROCESS_OWNER_UUID(this.props.currentItemId));
158 this.props.dispatch<any>(navigateToRunProcess);
161 handleNewCollectionClick = () => {
162 this.props.dispatch<any>(openCollectionCreateDialog(this.props.currentItemId));
165 handleClose = () => {
166 this.setState({ anchorEl: undefined });
169 handleOpen = (event: React.MouseEvent<HTMLButtonElement>) => {
170 this.setState({ anchorEl: event.currentTarget });