1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/styles';
7 import Drawer from '@material-ui/core/Drawer';
8 import { connect, DispatchProp } from "react-redux";
9 import { Route, Switch, RouteComponentProps } from "react-router";
10 import { authActions } from "../../store/auth/auth-action";
11 import { User } from "../../models/user";
12 import { RootState } from "../../store/store";
13 import { MainAppBar, MainAppBarActionProps, MainAppBarMenuItem } from '../../views-components/main-app-bar/main-app-bar';
14 import { Breadcrumb } from '../../components/breadcrumbs/breadcrumbs';
15 import { push } from 'react-router-redux';
16 import { ProjectTree } from '../../views-components/project-tree/project-tree';
17 import { TreeItem } from "../../components/tree/tree";
18 import { getTreePath } from '../../store/project/project-reducer';
19 import { sidePanelActions } from '../../store/side-panel/side-panel-action';
20 import { SidePanel, SidePanelItem } from '../../components/side-panel/side-panel';
21 import { ItemMode, setProjectItem } from "../../store/navigation/navigation-action";
22 import { projectActions } from "../../store/project/project-action";
23 import { ProjectPanel } from "../project-panel/project-panel";
24 import { DetailsPanel } from '../../views-components/details-panel/details-panel';
25 import { ArvadosTheme } from '../../common/custom-theme';
26 import { CreateProjectDialog } from "../../views-components/create-project-dialog/create-project-dialog";
27 import { authService } from '../../services/services';
29 import { detailsPanelActions, loadDetails } from "../../store/details-panel/details-panel-action";
30 import { contextMenuActions } from "../../store/context-menu/context-menu-actions";
31 import { SidePanelIdentifiers } from '../../store/side-panel/side-panel-reducer';
32 import { ProjectResource } from '../../models/project';
33 import { ResourceKind } from '../../models/resource';
34 import { ContextMenu, ContextMenuKind } from "../../views-components/context-menu/context-menu";
35 import { CurrentTokenDialog } from '../../views-components/current-token-dialog/current-token-dialog';
37 const drawerWidth = 240;
38 const appBarHeight = 100;
40 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
42 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
53 zIndex: theme.zIndex.drawer + 1,
61 flexDirection: 'column',
64 backgroundColor: theme.palette.background.default,
68 paddingTop: appBarHeight
71 padding: `${theme.spacing.unit}px ${theme.spacing.unit * 3}px`,
75 toolbar: theme.mixins.toolbar
78 interface WorkbenchDataProps {
79 projects: Array<TreeItem<ProjectResource>>;
80 currentProjectId: string;
82 currentToken?: string;
83 sidePanelItems: SidePanelItem[];
86 interface WorkbenchActionProps {
89 type WorkbenchProps = WorkbenchDataProps & WorkbenchActionProps & DispatchProp & WithStyles<CssRules>;
91 interface NavBreadcrumb extends Breadcrumb {
95 interface NavMenuItem extends MainAppBarMenuItem {
99 interface WorkbenchState {
100 isCurrentTokenDialogOpen: boolean;
104 accountMenu: NavMenuItem[],
105 helpMenu: NavMenuItem[],
106 anonymousMenu: NavMenuItem[]
110 export const Workbench = withStyles(styles)(
111 connect<WorkbenchDataProps>(
112 (state: RootState) => ({
113 projects: state.projects.items,
114 currentProjectId: state.projects.currentItemId,
115 user: state.auth.user,
116 currentToken: state.auth.apiToken,
117 sidePanelItems: state.sidePanel
120 class extends React.Component<WorkbenchProps, WorkbenchState> {
122 isCreationDialogOpen: false,
123 isCurrentTokenDialogOpen: false,
130 label: 'Current token',
131 action: () => this.toggleCurrentTokenModal()
135 action: () => this.props.dispatch(authActions.LOGOUT())
139 action: () => this.props.dispatch(push("/my-account"))
145 action: () => this.props.dispatch(push("/help"))
151 action: () => this.props.dispatch(authActions.LOGIN())
158 const path = getTreePath(this.props.projects, this.props.currentProjectId);
159 const breadcrumbs = path.map(item => ({
160 label: item.data.name,
161 itemId: item.data.uuid,
165 const { classes, user } = this.props;
167 <div className={classes.root}>
168 <div className={classes.appBar}>
170 breadcrumbs={breadcrumbs}
171 searchText={this.state.searchText}
172 user={this.props.user}
173 menuItems={this.state.menuItems}
174 {...this.mainAppBarActions} />
180 paper: classes.drawerPaper,
182 <div className={classes.toolbar} />
184 toggleOpen={this.toggleSidePanelOpen}
185 toggleActive={this.toggleSidePanelActive}
186 sidePanelItems={this.props.sidePanelItems}
187 onContextMenu={(event) => this.openContextMenu(event, authService.getUuid() || "", ContextMenuKind.RootProject)}>
189 projects={this.props.projects}
190 toggleOpen={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.OPEN))}
191 onContextMenu={(event, item) => this.openContextMenu(event, item.data.uuid, ContextMenuKind.Project)}
192 toggleActive={itemId => {
193 this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE));
194 this.props.dispatch<any>(loadDetails(itemId, ResourceKind.Project));
195 this.props.dispatch<any>(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.Projects));
199 <main className={classes.contentWrapper}>
200 <div className={classes.content}>
202 <Route path="/projects/:id" render={this.renderProjectPanel} />
205 { user && <DetailsPanel /> }
208 <CreateProjectDialog />
210 currentToken={this.props.currentToken}
211 open={this.state.isCurrentTokenDialogOpen}
212 handleClose={this.toggleCurrentTokenModal} />
217 renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
218 onItemRouteChange={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE))}
219 onContextMenu={(event, item) => this.openContextMenu(event, item.uuid, ContextMenuKind.Project)}
220 onDialogOpen={this.handleCreationDialogOpen}
221 onItemClick={item => {
222 this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
224 onItemDoubleClick={item => {
225 this.props.dispatch<any>(setProjectItem(item.uuid, ItemMode.ACTIVE));
226 this.props.dispatch<any>(loadDetails(item.uuid, ResourceKind.Project));
230 mainAppBarActions: MainAppBarActionProps = {
231 onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
232 this.props.dispatch<any>(setProjectItem(itemId, ItemMode.BOTH));
233 this.props.dispatch<any>(loadDetails(itemId, ResourceKind.Project));
235 onSearch: searchText => {
236 this.setState({ searchText });
237 this.props.dispatch(push(`/search?q=${searchText}`));
239 onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
240 onDetailsPanelToggle: () => {
241 this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
243 onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: NavBreadcrumb) => {
244 this.openContextMenu(event, breadcrumb.itemId, ContextMenuKind.Project);
248 toggleSidePanelOpen = (itemId: string) => {
249 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
252 toggleSidePanelActive = (itemId: string) => {
253 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
254 this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
255 this.props.dispatch(push("/"));
258 handleCreationDialogOpen = (itemUuid: string) => {
259 this.props.dispatch(projectActions.OPEN_PROJECT_CREATOR({ ownerUuid: itemUuid }));
262 openContextMenu = (event: React.MouseEvent<HTMLElement>, itemUuid: string, kind: ContextMenuKind) => {
263 event.preventDefault();
265 contextMenuActions.OPEN_CONTEXT_MENU({
266 position: { x: event.clientX, y: event.clientY },
267 resource: { uuid: itemUuid, kind }
272 toggleCurrentTokenModal = () => {
273 this.setState({ isCurrentTokenDialogOpen: !this.state.isCurrentTokenDialogOpen });