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 { FavoritePanel } from "../favorite-panel/favorite-panel";
36 import { CurrentTokenDialog } from '../../views-components/current-token-dialog/current-token-dialog';
37 import { Snackbar } from '../../views-components/snackbar/snackbar';
38 import { favoritePanelActions } from '../../store/favorite-panel/favorite-panel-action';
40 const drawerWidth = 240;
41 const appBarHeight = 100;
43 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
45 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
56 zIndex: theme.zIndex.drawer + 1,
64 flexDirection: 'column',
67 backgroundColor: theme.palette.background.default,
71 paddingTop: appBarHeight
74 padding: `${theme.spacing.unit}px ${theme.spacing.unit * 3}px`,
78 toolbar: theme.mixins.toolbar
81 interface WorkbenchDataProps {
82 projects: Array<TreeItem<ProjectResource>>;
83 currentProjectId: string;
85 currentToken?: string;
86 sidePanelItems: SidePanelItem[];
89 interface WorkbenchActionProps {
92 type WorkbenchProps = WorkbenchDataProps & WorkbenchActionProps & DispatchProp & WithStyles<CssRules>;
94 interface NavBreadcrumb extends Breadcrumb {
98 interface NavMenuItem extends MainAppBarMenuItem {
102 interface WorkbenchState {
103 isCurrentTokenDialogOpen: boolean;
107 accountMenu: NavMenuItem[],
108 helpMenu: NavMenuItem[],
109 anonymousMenu: NavMenuItem[]
113 export const Workbench = withStyles(styles)(
114 connect<WorkbenchDataProps>(
115 (state: RootState) => ({
116 projects: state.projects.items,
117 currentProjectId: state.projects.currentItemId,
118 user: state.auth.user,
119 currentToken: state.auth.apiToken,
120 sidePanelItems: state.sidePanel
123 class extends React.Component<WorkbenchProps, WorkbenchState> {
125 isCreationDialogOpen: false,
126 isCurrentTokenDialogOpen: false,
133 label: 'Current token',
134 action: () => this.toggleCurrentTokenModal()
138 action: () => this.props.dispatch(authActions.LOGOUT())
142 action: () => this.props.dispatch(push("/my-account"))
148 action: () => this.props.dispatch(push("/help"))
154 action: () => this.props.dispatch(authActions.LOGIN())
161 const path = getTreePath(this.props.projects, this.props.currentProjectId);
162 const breadcrumbs = path.map(item => ({
163 label: item.data.name,
164 itemId: item.data.uuid,
168 const { classes, user } = this.props;
170 <div className={classes.root}>
171 <div className={classes.appBar}>
173 breadcrumbs={breadcrumbs}
174 searchText={this.state.searchText}
175 user={this.props.user}
176 menuItems={this.state.menuItems}
177 {...this.mainAppBarActions} />
183 paper: classes.drawerPaper,
185 <div className={classes.toolbar} />
187 toggleOpen={this.toggleSidePanelOpen}
188 toggleActive={this.toggleSidePanelActive}
189 sidePanelItems={this.props.sidePanelItems}
190 onContextMenu={(event) => this.openContextMenu(event, {
191 uuid: authService.getUuid() || "",
193 kind: ContextMenuKind.ROOT_PROJECT
196 projects={this.props.projects}
197 toggleOpen={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.OPEN))}
198 onContextMenu={(event, item) => this.openContextMenu(event, {
199 uuid: item.data.uuid,
200 name: item.data.name,
201 kind: ContextMenuKind.PROJECT
203 toggleActive={itemId => {
204 this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE));
205 this.props.dispatch<any>(loadDetails(itemId, ResourceKind.PROJECT));
206 this.props.dispatch<any>(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
210 <main className={classes.contentWrapper}>
211 <div className={classes.content}>
213 <Route path="/projects/:id" render={this.renderProjectPanel} />
214 <Route path="/favorites" render={this.renderFavoritePanel} />
217 {user && <DetailsPanel />}
221 <CreateProjectDialog />
223 currentToken={this.props.currentToken}
224 open={this.state.isCurrentTokenDialogOpen}
225 handleClose={this.toggleCurrentTokenModal} />
230 renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
231 onItemRouteChange={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE))}
232 onContextMenu={(event, item) => {
233 const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
234 this.openContextMenu(event, {
240 onDialogOpen={this.handleCreationDialogOpen}
241 onItemClick={item => {
242 this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
244 onItemDoubleClick={item => {
245 this.props.dispatch<any>(setProjectItem(item.uuid, ItemMode.ACTIVE));
246 this.props.dispatch<any>(loadDetails(item.uuid, ResourceKind.PROJECT));
250 renderFavoritePanel = (props: RouteComponentProps<{ id: string }>) => <FavoritePanel
251 onItemRouteChange={() => this.props.dispatch<any>(favoritePanelActions.REQUEST_ITEMS())}
252 onContextMenu={(event, item) => {
253 const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
254 this.openContextMenu(event, {
260 onDialogOpen={this.handleCreationDialogOpen}
261 onItemClick={item => {
262 this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
264 onItemDoubleClick={item => {
265 this.props.dispatch<any>(loadDetails(item.uuid, ResourceKind.PROJECT));
266 this.props.dispatch<any>(setProjectItem(item.uuid, ItemMode.ACTIVE));
267 this.props.dispatch<any>(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
271 mainAppBarActions: MainAppBarActionProps = {
272 onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
273 this.props.dispatch<any>(setProjectItem(itemId, ItemMode.BOTH));
274 this.props.dispatch<any>(loadDetails(itemId, ResourceKind.PROJECT));
276 onSearch: searchText => {
277 this.setState({ searchText });
278 this.props.dispatch(push(`/search?q=${searchText}`));
280 onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
281 onDetailsPanelToggle: () => {
282 this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
284 onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: NavBreadcrumb) => {
285 this.openContextMenu(event, {
286 uuid: breadcrumb.itemId,
287 name: breadcrumb.label,
288 kind: ContextMenuKind.PROJECT
293 toggleSidePanelOpen = (itemId: string) => {
294 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
297 toggleSidePanelActive = (itemId: string) => {
298 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
299 this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
300 const panelItem = this.props.sidePanelItems.find(it => it.id === itemId);
301 if (panelItem && panelItem.activeAction) {
302 panelItem.activeAction(this.props.dispatch);
306 handleCreationDialogOpen = (itemUuid: string) => {
307 this.props.dispatch(projectActions.OPEN_PROJECT_CREATOR({ ownerUuid: itemUuid }));
310 openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: { name: string; uuid: string; kind: ContextMenuKind; }) => {
311 event.preventDefault();
313 contextMenuActions.OPEN_CONTEXT_MENU({
314 position: { x: event.clientX, y: event.clientY },
320 toggleCurrentTokenModal = () => {
321 this.setState({ isCurrentTokenDialogOpen: !this.state.isCurrentTokenDialogOpen });