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';
20 import { extractUuidKind, ResourceKind } from '~/models/resource';
22 type CssRules = 'button' | 'menuItem' | 'icon';
24 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
27 padding: '2px 10px 2px 5px',
32 color: theme.palette.grey["700"]
35 marginRight: theme.spacing.unit
39 interface SidePanelDataProps {
41 currentItemId: string;
42 resources: ResourcesState;
43 currentUserUUID: string | undefined;
46 interface SidePanelState {
50 type SidePanelProps = SidePanelDataProps & DispatchProp & WithStyles<CssRules>;
52 const transformOrigin: PopoverOrigin = {
57 export const isProjectTrashed = (proj: GroupResource | undefined, resources: ResourcesState): boolean => {
58 if (proj === undefined) { return false; }
59 if (proj.isTrashed) { return true; }
60 if (extractUuidKind(proj.ownerUuid) === ResourceKind.USER) { return false; }
61 const parentProj = getResource<GroupResource>(proj.ownerUuid)(resources);
62 return isProjectTrashed(parentProj, resources);
65 export const SidePanelButton = withStyles(styles)(
66 connect((state: RootState) => ({
67 currentItemId: state.router.location
68 ? state.router.location.pathname.split('/').slice(-1)[0]
70 location: state.router.location,
71 resources: state.resources,
72 currentUserUUID: getUserUuid(state),
74 class extends React.Component<SidePanelProps> {
76 state: SidePanelState = {
81 const { classes, location, resources, currentUserUUID, currentItemId } = this.props;
82 const { anchorEl } = this.state;
84 if (currentItemId === currentUserUUID) {
86 } else if (matchProjectRoute(location ? location.pathname : '')) {
87 const currentProject = getResource<GroupResource>(currentItemId)(resources);
89 currentProject.writableBy.indexOf(currentUserUUID || '') >= 0 &&
90 !isProjectTrashed(currentProject, resources)) {
96 <Grid container item xs alignItems="center" justify="flex-start">
97 <Button data-cy="side-panel-button" variant="contained" disabled={!enabled}
98 color="primary" size="small" className={classes.button}
99 aria-owns={anchorEl ? 'aside-menu-list' : undefined}
101 onClick={this.handleOpen}>
108 open={Boolean(anchorEl)}
109 onClose={this.handleClose}
110 onClick={this.handleClose}
111 transformOrigin={transformOrigin}>
112 <MenuItem data-cy='side-panel-new-collection' className={classes.menuItem} onClick={this.handleNewCollectionClick}>
113 <CollectionIcon className={classes.icon} /> New collection
115 <MenuItem data-cy='side-panel-run-process' className={classes.menuItem} onClick={this.handleRunProcessClick}>
116 <ProcessIcon className={classes.icon} /> Run a process
118 <MenuItem data-cy='side-panel-new-project' className={classes.menuItem} onClick={this.handleNewProjectClick}>
119 <ProjectIcon className={classes.icon} /> New project
127 handleNewProjectClick = () => {
128 this.props.dispatch<any>(openProjectCreateDialog(this.props.currentItemId));
131 handleRunProcessClick = () => {
132 const location = this.props.location;
133 this.props.dispatch(runProcessPanelActions.RESET_RUN_PROCESS_PANEL());
134 this.props.dispatch(runProcessPanelActions.SET_PROCESS_PATHNAME(location.pathname));
135 this.props.dispatch(runProcessPanelActions.SET_PROCESS_OWNER_UUID(this.props.currentItemId));
137 this.props.dispatch<any>(navigateToRunProcess);
140 handleNewCollectionClick = () => {
141 this.props.dispatch<any>(openCollectionCreateDialog(this.props.currentItemId));
144 handleClose = () => {
145 this.setState({ anchorEl: undefined });
148 handleOpen = (event: React.MouseEvent<HTMLButtonElement>) => {
149 this.setState({ anchorEl: event.currentTarget });