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, setFavoriteItem, 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 { sidePanelData, 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 { FavoritePanel } from "../favorite-panel/favorite-panel";
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 sidePanelItems: SidePanelItem[];
85 interface WorkbenchActionProps {
88 type WorkbenchProps = WorkbenchDataProps & WorkbenchActionProps & DispatchProp & WithStyles<CssRules>;
90 interface NavBreadcrumb extends Breadcrumb {
94 interface NavMenuItem extends MainAppBarMenuItem {
98 interface WorkbenchState {
102 accountMenu: NavMenuItem[],
103 helpMenu: NavMenuItem[],
104 anonymousMenu: NavMenuItem[]
108 export const Workbench = withStyles(styles)(
109 connect<WorkbenchDataProps>(
110 (state: RootState) => ({
111 projects: state.projects.items,
112 currentProjectId: state.projects.currentItemId,
113 user: state.auth.user,
114 sidePanelItems: state.sidePanel
117 class extends React.Component<WorkbenchProps, WorkbenchState> {
119 isCreationDialogOpen: false,
127 action: () => this.props.dispatch(authActions.LOGOUT())
131 action: () => this.props.dispatch(push("/my-account"))
137 action: () => this.props.dispatch(push("/help"))
143 action: () => this.props.dispatch(authActions.LOGIN())
150 const path = getTreePath(this.props.projects, this.props.currentProjectId);
151 const breadcrumbs = path.map(item => ({
152 label: item.data.name,
153 itemId: item.data.uuid,
157 const { classes, user } = this.props;
159 <div className={classes.root}>
160 <div className={classes.appBar}>
162 breadcrumbs={breadcrumbs}
163 searchText={this.state.searchText}
164 user={this.props.user}
165 menuItems={this.state.menuItems}
166 {...this.mainAppBarActions} />
172 paper: classes.drawerPaper,
174 <div className={classes.toolbar} />
176 toggleOpen={this.toggleSidePanelOpen}
177 toggleActive={this.toggleSidePanelActive}
178 sidePanelItems={this.props.sidePanelItems}
179 onContextMenu={(event) => this.openContextMenu(event, {
180 uuid: authService.getUuid() || "",
182 kind: ContextMenuKind.RootProject
185 projects={this.props.projects}
186 toggleOpen={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.OPEN))}
187 onContextMenu={(event, item) => this.openContextMenu(event, {
188 uuid: item.data.uuid,
189 name: item.data.name,
190 kind: 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} />
203 <Route path="/favorites" render={this.renderFavoritePanel} />
206 {user && <DetailsPanel />}
209 <CreateProjectDialog />
214 renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
215 onItemRouteChange={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE))}
216 onContextMenu={(event, item) => {
217 const kind = item.kind === ResourceKind.Project ? ContextMenuKind.Project : ContextMenuKind.Resource;
218 this.openContextMenu(event, {
224 onDialogOpen={this.handleCreationDialogOpen}
225 onItemClick={item => {
226 this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
228 onItemDoubleClick={item => {
229 this.props.dispatch<any>(setProjectItem(item.uuid, ItemMode.ACTIVE));
230 this.props.dispatch<any>(loadDetails(item.uuid, ResourceKind.Project));
234 renderFavoritePanel = (props: RouteComponentProps<{ id: string }>) => <FavoritePanel
235 onItemRouteChange={itemId => this.props.dispatch<any>(setFavoriteItem(itemId, ItemMode.ACTIVE))}
236 onContextMenu={(event, item) => {
237 this.openContextMenu(event, {
240 kind: ContextMenuKind.Favorite,
243 onDialogOpen={this.handleCreationDialogOpen}
244 onItemClick={item => {
245 this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
247 onItemDoubleClick={item => {
248 this.props.dispatch<any>(setFavoriteItem(item.uuid, ItemMode.ACTIVE));
249 this.props.dispatch<any>(loadDetails(item.uuid, ResourceKind.Project));
253 mainAppBarActions: MainAppBarActionProps = {
254 onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
255 this.props.dispatch<any>(setProjectItem(itemId, ItemMode.BOTH));
256 this.props.dispatch<any>(loadDetails(itemId, ResourceKind.Project));
258 onSearch: searchText => {
259 this.setState({ searchText });
260 this.props.dispatch(push(`/search?q=${searchText}`));
262 onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
263 onDetailsPanelToggle: () => {
264 this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
266 onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: NavBreadcrumb) => {
267 this.openContextMenu(event, {
268 uuid: breadcrumb.itemId,
269 name: breadcrumb.label,
270 kind: ContextMenuKind.Project
275 toggleSidePanelOpen = (itemId: string) => {
276 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
279 toggleSidePanelActive = (itemId: string) => {
280 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
281 this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
282 const panelItem = this.props.sidePanelItems.find(it => it.id === itemId);
283 if (panelItem && panelItem.activeAction) {
284 panelItem.activeAction(this.props.dispatch);
288 handleCreationDialogOpen = (itemUuid: string) => {
289 this.props.dispatch(projectActions.OPEN_PROJECT_CREATOR({ ownerUuid: itemUuid }));
292 openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: { name: string; uuid: string; kind: ContextMenuKind; }) => {
293 event.preventDefault();
295 contextMenuActions.OPEN_CONTEXT_MENU({
296 position: { x: event.clientX, y: event.clientY },