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.RootProject
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))}
238 renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
239 onItemRouteChange={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE))}
240 onContextMenu={(event, item) => {
241 const kind = item.kind === ResourceKind.Project ? ContextMenuKind.Project : ContextMenuKind.Resource;
242 this.openContextMenu(event, {
248 onDialogOpen={this.handleCreationDialogOpen}
249 onItemClick={item => {
250 this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
252 onItemDoubleClick={item => {
254 case ResourceKind.Collection:
255 this.props.dispatch<any>(loadCollection(item.uuid, item.kind as ResourceKind));
256 this.props.dispatch(push(getCollectionUrl(item.uuid)));
258 this.props.dispatch<any>(setProjectItem(item.uuid, ItemMode.ACTIVE));
259 this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
264 renderFavoritePanel = (props: RouteComponentProps<{ id: string }>) => <FavoritePanel
265 onItemRouteChange={() => this.props.dispatch<any>(dataExplorerActions.REQUEST_ITEMS({ id: FAVORITE_PANEL_ID }))}
266 onContextMenu={(event, item) => {
267 const kind = item.kind === ResourceKind.Project ? ContextMenuKind.Project : ContextMenuKind.Resource;
268 this.openContextMenu(event, {
274 onDialogOpen={this.handleCreationDialogOpen}
275 onItemClick={item => {
276 this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
278 onItemDoubleClick={item => {
279 this.props.dispatch<any>(loadDetails(item.uuid, ResourceKind.Project));
280 this.props.dispatch<any>(setProjectItem(item.uuid, ItemMode.ACTIVE));
281 this.props.dispatch<any>(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.Projects));
285 mainAppBarActions: MainAppBarActionProps = {
286 onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
287 this.props.dispatch<any>(setProjectItem(itemId, ItemMode.BOTH));
288 this.props.dispatch<any>(loadDetails(itemId, ResourceKind.Project));
290 onSearch: searchText => {
291 this.setState({ searchText });
292 this.props.dispatch(push(`/search?q=${searchText}`));
294 onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
295 onDetailsPanelToggle: () => {
296 this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
298 onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: NavBreadcrumb) => {
299 this.openContextMenu(event, {
300 uuid: breadcrumb.itemId,
301 name: breadcrumb.label,
302 kind: ContextMenuKind.Project
307 toggleSidePanelOpen = (itemId: string) => {
308 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
311 toggleSidePanelActive = (itemId: string) => {
312 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
313 this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
314 const panelItem = this.props.sidePanelItems.find(it => it.id === itemId);
315 if (panelItem && panelItem.activeAction) {
316 panelItem.activeAction(this.props.dispatch);
320 handleCreationDialogOpen = (itemUuid: string) => {
321 this.props.dispatch(projectActions.OPEN_PROJECT_CREATOR({ ownerUuid: itemUuid }));
324 openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: { name: string; uuid: string; kind: ContextMenuKind; }) => {
325 event.preventDefault();
327 contextMenuActions.OPEN_CONTEXT_MENU({
328 position: { x: event.clientX, y: event.clientY },
334 toggleCurrentTokenModal = () => {
335 this.setState({ isCurrentTokenDialogOpen: !this.state.isCurrentTokenDialogOpen });