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 { collectionCreateActions } from '../../store/collections/creator/collection-creator-action';
24 import { ProjectPanel } from "../project-panel/project-panel";
25 import { DetailsPanel } from '../../views-components/details-panel/details-panel';
26 import { ArvadosTheme } from '../../common/custom-theme';
27 import { CreateProjectDialog } from "../../views-components/create-project-dialog/create-project-dialog";
28 import { authService } from '../../services/services';
30 import { detailsPanelActions, loadDetails } from "../../store/details-panel/details-panel-action";
31 import { contextMenuActions } from "../../store/context-menu/context-menu-actions";
32 import { SidePanelIdentifiers } from '../../store/side-panel/side-panel-reducer';
33 import { ProjectResource } from '../../models/project';
34 import { ResourceKind } from '../../models/resource';
35 import { ContextMenu, ContextMenuKind } from "../../views-components/context-menu/context-menu";
36 import { FavoritePanel, FAVORITE_PANEL_ID } from "../favorite-panel/favorite-panel";
37 import { CurrentTokenDialog } from '../../views-components/current-token-dialog/current-token-dialog';
38 import { dataExplorerActions } from '../../store/data-explorer/data-explorer-action';
39 import { Snackbar } from '../../views-components/snackbar/snackbar';
40 import { CreateCollectionDialog } from '../../views-components/create-collection-dialog/create-collection-dialog';
41 import { CollectionPanel } from '../collection-panel/collection-panel';
42 import { loadCollection } from '../../store/collection-panel/collection-panel-action';
43 import { getCollectionUrl } from '../../models/collection';
45 const drawerWidth = 240;
46 const appBarHeight = 100;
48 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
50 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
61 zIndex: theme.zIndex.drawer + 1,
69 flexDirection: 'column',
72 backgroundColor: theme.palette.background.default,
76 paddingTop: appBarHeight
79 padding: `${theme.spacing.unit}px ${theme.spacing.unit * 3}px`,
83 toolbar: theme.mixins.toolbar
86 interface WorkbenchDataProps {
87 projects: Array<TreeItem<ProjectResource>>;
88 currentProjectId: string;
90 currentToken?: string;
91 sidePanelItems: SidePanelItem[];
94 interface WorkbenchActionProps {
97 type WorkbenchProps = WorkbenchDataProps & WorkbenchActionProps & DispatchProp & WithStyles<CssRules>;
99 interface NavBreadcrumb extends Breadcrumb {
103 interface NavMenuItem extends MainAppBarMenuItem {
107 interface WorkbenchState {
108 isCurrentTokenDialogOpen: boolean;
112 accountMenu: NavMenuItem[],
113 helpMenu: NavMenuItem[],
114 anonymousMenu: NavMenuItem[]
118 export const Workbench = withStyles(styles)(
119 connect<WorkbenchDataProps>(
120 (state: RootState) => ({
121 projects: state.projects.items,
122 currentProjectId: state.projects.currentItemId,
123 user: state.auth.user,
124 currentToken: state.auth.apiToken,
125 sidePanelItems: state.sidePanel
128 class extends React.Component<WorkbenchProps, WorkbenchState> {
130 isCreationDialogOpen: false,
131 isCurrentTokenDialogOpen: false,
138 label: 'Current token',
139 action: () => this.toggleCurrentTokenModal()
143 action: () => this.props.dispatch(authActions.LOGOUT())
147 action: () => this.props.dispatch(push("/my-account"))
153 action: () => this.props.dispatch(push("/help"))
159 action: () => this.props.dispatch(authActions.LOGIN())
166 const path = getTreePath(this.props.projects, this.props.currentProjectId);
167 const breadcrumbs = path.map(item => ({
168 label: item.data.name,
169 itemId: item.data.uuid,
173 const { classes, user } = this.props;
175 <div className={classes.root}>
176 <div className={classes.appBar}>
178 breadcrumbs={breadcrumbs}
179 searchText={this.state.searchText}
180 user={this.props.user}
181 menuItems={this.state.menuItems}
182 {...this.mainAppBarActions} />
188 paper: classes.drawerPaper,
190 <div className={classes.toolbar} />
192 toggleOpen={this.toggleSidePanelOpen}
193 toggleActive={this.toggleSidePanelActive}
194 sidePanelItems={this.props.sidePanelItems}
195 onContextMenu={(event) => this.openContextMenu(event, {
196 uuid: authService.getUuid() || "",
198 kind: ContextMenuKind.ROOT_PROJECT
201 projects={this.props.projects}
202 toggleOpen={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.OPEN))}
203 onContextMenu={(event, item) => this.openContextMenu(event, {
204 uuid: item.data.uuid,
205 name: item.data.name,
206 kind: ContextMenuKind.PROJECT
208 toggleActive={itemId => {
209 this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE));
210 this.props.dispatch<any>(loadDetails(itemId, ResourceKind.PROJECT));
211 this.props.dispatch<any>(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
215 <main className={classes.contentWrapper}>
216 <div className={classes.content}>
218 <Route path="/projects/:id" render={this.renderProjectPanel} />
219 <Route path="/favorites" render={this.renderFavoritePanel} />
220 <Route path="/collections/:id" render={this.renderCollectionPanel} />
223 {user && <DetailsPanel />}
227 <CreateProjectDialog />
228 <CreateCollectionDialog />
230 currentToken={this.props.currentToken}
231 open={this.state.isCurrentTokenDialogOpen}
232 handleClose={this.toggleCurrentTokenModal} />
237 renderCollectionPanel = (props: RouteComponentProps<{ id: string }>) => <CollectionPanel
238 onItemRouteChange={(collectionId) => this.props.dispatch<any>(loadCollection(collectionId, ResourceKind.COLLECTION))}
239 onContextMenu={(event, item) => {
240 this.openContextMenu(event, {
243 kind: ContextMenuKind.COLLECTION
248 renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
249 onItemRouteChange={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE))}
250 onContextMenu={(event, item) => {
252 const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
253 this.openContextMenu(event, {
259 onProjectCreationDialogOpen={this.handleProjectCreationDialogOpen}
260 onCollectionCreationDialogOpen={this.handleCollectionCreationDialogOpen}
261 onItemClick={item => {
262 this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
264 onItemDoubleClick={item => {
266 case ResourceKind.COLLECTION:
267 this.props.dispatch<any>(loadCollection(item.uuid, item.kind as ResourceKind));
268 this.props.dispatch(push(getCollectionUrl(item.uuid)));
270 this.props.dispatch<any>(setProjectItem(item.uuid, ItemMode.ACTIVE));
271 this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
277 renderFavoritePanel = (props: RouteComponentProps<{ id: string }>) => <FavoritePanel
278 onItemRouteChange={() => this.props.dispatch<any>(dataExplorerActions.REQUEST_ITEMS({ id: FAVORITE_PANEL_ID }))}
279 onContextMenu={(event, item) => {
280 const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
281 this.openContextMenu(event, {
287 onDialogOpen={this.handleProjectCreationDialogOpen}
288 onItemClick={item => {
289 this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
291 onItemDoubleClick={item => {
293 case ResourceKind.COLLECTION:
294 this.props.dispatch<any>(loadCollection(item.uuid, item.kind as ResourceKind));
295 this.props.dispatch(push(getCollectionUrl(item.uuid)));
297 this.props.dispatch<any>(loadDetails(item.uuid, ResourceKind.PROJECT));
298 this.props.dispatch<any>(setProjectItem(item.uuid, ItemMode.ACTIVE));
299 this.props.dispatch<any>(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
305 mainAppBarActions: MainAppBarActionProps = {
306 onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
307 this.props.dispatch<any>(setProjectItem(itemId, ItemMode.BOTH));
308 this.props.dispatch<any>(loadDetails(itemId, ResourceKind.PROJECT));
310 onSearch: searchText => {
311 this.setState({ searchText });
312 this.props.dispatch(push(`/search?q=${searchText}`));
314 onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
315 onDetailsPanelToggle: () => {
316 this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
318 onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: NavBreadcrumb) => {
319 this.openContextMenu(event, {
320 uuid: breadcrumb.itemId,
321 name: breadcrumb.label,
322 kind: ContextMenuKind.PROJECT
327 toggleSidePanelOpen = (itemId: string) => {
328 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
331 toggleSidePanelActive = (itemId: string) => {
332 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
333 this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
334 const panelItem = this.props.sidePanelItems.find(it => it.id === itemId);
335 if (panelItem && panelItem.activeAction) {
336 panelItem.activeAction(this.props.dispatch);
340 handleProjectCreationDialogOpen = (itemUuid: string) => {
341 this.props.dispatch(projectActions.OPEN_PROJECT_CREATOR({ ownerUuid: itemUuid }));
344 handleCollectionCreationDialogOpen = (itemUuid: string) => {
345 this.props.dispatch(collectionCreateActions.OPEN_COLLECTION_CREATOR({ ownerUuid: itemUuid }));
348 openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: { name: string; uuid: string; kind: ContextMenuKind; }) => {
349 event.preventDefault();
351 contextMenuActions.OPEN_CONTEXT_MENU({
352 position: { x: event.clientX, y: event.clientY },
358 toggleCurrentTokenModal = () => {
359 this.setState({ isCurrentTokenDialogOpen: !this.state.isCurrentTokenDialogOpen });