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, FAVORITE_PANEL_ID } from "../favorite-panel/favorite-panel";
36 import { CurrentTokenDialog } from '../../views-components/current-token-dialog/current-token-dialog';
37 import { dataExplorerActions } from '../../store/data-explorer/data-explorer-action';
38 import { Snackbar } from '../../views-components/snackbar/snackbar';
39 import { CollectionPanel } from '../collection-panel/collection-panel';
40 import { loadCollection } from '../../store/collection-panel/collection-panel-action';
41 import { getCollectionUrl } from '../../models/collection';
43 const drawerWidth = 240;
44 const appBarHeight = 100;
46 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
48 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
59 zIndex: theme.zIndex.drawer + 1,
67 flexDirection: 'column',
70 backgroundColor: theme.palette.background.default,
74 paddingTop: appBarHeight
77 padding: `${theme.spacing.unit}px ${theme.spacing.unit * 3}px`,
81 toolbar: theme.mixins.toolbar
84 interface WorkbenchDataProps {
85 projects: Array<TreeItem<ProjectResource>>;
86 currentProjectId: string;
88 currentToken?: string;
89 sidePanelItems: SidePanelItem[];
92 interface WorkbenchActionProps {
95 type WorkbenchProps = WorkbenchDataProps & WorkbenchActionProps & DispatchProp & WithStyles<CssRules>;
97 interface NavBreadcrumb extends Breadcrumb {
101 interface NavMenuItem extends MainAppBarMenuItem {
105 interface WorkbenchState {
106 isCurrentTokenDialogOpen: boolean;
110 accountMenu: NavMenuItem[],
111 helpMenu: NavMenuItem[],
112 anonymousMenu: NavMenuItem[]
116 export const Workbench = withStyles(styles)(
117 connect<WorkbenchDataProps>(
118 (state: RootState) => ({
119 projects: state.projects.items,
120 currentProjectId: state.projects.currentItemId,
121 user: state.auth.user,
122 currentToken: state.auth.apiToken,
123 sidePanelItems: state.sidePanel
126 class extends React.Component<WorkbenchProps, WorkbenchState> {
128 isCreationDialogOpen: false,
129 isCurrentTokenDialogOpen: false,
136 label: 'Current token',
137 action: () => this.toggleCurrentTokenModal()
141 action: () => this.props.dispatch(authActions.LOGOUT())
145 action: () => this.props.dispatch(push("/my-account"))
151 action: () => this.props.dispatch(push("/help"))
157 action: () => this.props.dispatch(authActions.LOGIN())
164 const path = getTreePath(this.props.projects, this.props.currentProjectId);
165 const breadcrumbs = path.map(item => ({
166 label: item.data.name,
167 itemId: item.data.uuid,
171 const { classes, user } = this.props;
173 <div className={classes.root}>
174 <div className={classes.appBar}>
176 breadcrumbs={breadcrumbs}
177 searchText={this.state.searchText}
178 user={this.props.user}
179 menuItems={this.state.menuItems}
180 {...this.mainAppBarActions} />
186 paper: classes.drawerPaper,
188 <div className={classes.toolbar} />
190 toggleOpen={this.toggleSidePanelOpen}
191 toggleActive={this.toggleSidePanelActive}
192 sidePanelItems={this.props.sidePanelItems}
193 onContextMenu={(event) => this.openContextMenu(event, {
194 uuid: authService.getUuid() || "",
196 kind: ContextMenuKind.ROOT_PROJECT
199 projects={this.props.projects}
200 toggleOpen={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.OPEN))}
201 onContextMenu={(event, item) => this.openContextMenu(event, {
202 uuid: item.data.uuid,
203 name: item.data.name,
204 kind: ContextMenuKind.PROJECT
206 toggleActive={itemId => {
207 this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE));
208 this.props.dispatch<any>(loadDetails(itemId, ResourceKind.PROJECT));
209 this.props.dispatch<any>(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
213 <main className={classes.contentWrapper}>
214 <div className={classes.content}>
216 <Route path="/projects/:id" render={this.renderProjectPanel} />
217 <Route path="/favorites" render={this.renderFavoritePanel} />
218 <Route path="/collections/:id" render={this.renderCollectionPanel} />
221 {user && <DetailsPanel />}
225 <CreateProjectDialog />
227 currentToken={this.props.currentToken}
228 open={this.state.isCurrentTokenDialogOpen}
229 handleClose={this.toggleCurrentTokenModal} />
234 renderCollectionPanel = (props: RouteComponentProps<{ id: string }>) => <CollectionPanel
235 onItemRouteChange={(collectionId) => this.props.dispatch<any>(loadCollection(collectionId, ResourceKind.COLLECTION))}
236 onContextMenu={(event, item) => {
237 this.openContextMenu(event, {
240 kind: ContextMenuKind.COLLECTION
245 renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
246 onItemRouteChange={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE))}
247 onContextMenu={(event, item) => {
249 const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
250 this.openContextMenu(event, {
256 onDialogOpen={this.handleCreationDialogOpen}
257 onItemClick={item => {
258 this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
260 onItemDoubleClick={item => {
262 case ResourceKind.COLLECTION:
263 this.props.dispatch<any>(loadCollection(item.uuid, item.kind as ResourceKind));
264 this.props.dispatch(push(getCollectionUrl(item.uuid)));
266 this.props.dispatch<any>(setProjectItem(item.uuid, ItemMode.ACTIVE));
267 this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
273 renderFavoritePanel = (props: RouteComponentProps<{ id: string }>) => <FavoritePanel
274 onItemRouteChange={() => this.props.dispatch<any>(dataExplorerActions.REQUEST_ITEMS({ id: FAVORITE_PANEL_ID }))}
275 onContextMenu={(event, item) => {
276 const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
277 this.openContextMenu(event, {
283 onDialogOpen={this.handleCreationDialogOpen}
284 onItemClick={item => {
285 this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
287 onItemDoubleClick={item => {
289 case ResourceKind.COLLECTION:
290 this.props.dispatch<any>(loadCollection(item.uuid, item.kind as ResourceKind));
291 this.props.dispatch(push(getCollectionUrl(item.uuid)));
293 this.props.dispatch<any>(loadDetails(item.uuid, ResourceKind.PROJECT));
294 this.props.dispatch<any>(setProjectItem(item.uuid, ItemMode.ACTIVE));
295 this.props.dispatch<any>(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
301 mainAppBarActions: MainAppBarActionProps = {
302 onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
303 this.props.dispatch<any>(setProjectItem(itemId, ItemMode.BOTH));
304 this.props.dispatch<any>(loadDetails(itemId, ResourceKind.PROJECT));
306 onSearch: searchText => {
307 this.setState({ searchText });
308 this.props.dispatch(push(`/search?q=${searchText}`));
310 onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
311 onDetailsPanelToggle: () => {
312 this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
314 onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: NavBreadcrumb) => {
315 this.openContextMenu(event, {
316 uuid: breadcrumb.itemId,
317 name: breadcrumb.label,
318 kind: ContextMenuKind.PROJECT
323 toggleSidePanelOpen = (itemId: string) => {
324 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
327 toggleSidePanelActive = (itemId: string) => {
328 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
329 this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
330 const panelItem = this.props.sidePanelItems.find(it => it.id === itemId);
331 if (panelItem && panelItem.activeAction) {
332 panelItem.activeAction(this.props.dispatch);
336 handleCreationDialogOpen = (itemUuid: string) => {
337 this.props.dispatch(projectActions.OPEN_PROJECT_CREATOR({ ownerUuid: itemUuid }));
340 openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: { name: string; uuid: string; kind: ContextMenuKind; }) => {
341 event.preventDefault();
343 contextMenuActions.OPEN_CONTEXT_MENU({
344 position: { x: event.clientX, y: event.clientY },
350 toggleCurrentTokenModal = () => {
351 this.setState({ isCurrentTokenDialogOpen: !this.state.isCurrentTokenDialogOpen });