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 { login, logout } 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";
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 { dataExplorerActions } from '../../store/data-explorer/data-explorer-action';
38 import { Snackbar } from '../../views-components/snackbar/snackbar';
39 import { favoritePanelActions } from '../../store/favorite-panel/favorite-panel-action';
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';
44 import { UpdateCollectionDialog } from '../../views-components/update-collection-dialog/update-collection-dialog.';
45 import { AuthService } from "../../services/auth-service/auth-service";
47 const DRAWER_WITDH = 240;
48 const APP_BAR_HEIGHT = 100;
50 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
52 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
63 zIndex: theme.zIndex.drawer + 1,
71 flexDirection: 'column',
74 backgroundColor: theme.palette.background.default,
78 paddingTop: APP_BAR_HEIGHT
81 padding: `${theme.spacing.unit}px ${theme.spacing.unit * 3}px`,
85 toolbar: theme.mixins.toolbar
88 interface WorkbenchDataProps {
89 projects: Array<TreeItem<ProjectResource>>;
90 currentProjectId: string;
92 currentToken?: string;
93 sidePanelItems: SidePanelItem[];
96 interface WorkbenchServiceProps {
97 authService: AuthService;
100 interface WorkbenchActionProps {
103 type WorkbenchProps = WorkbenchDataProps & WorkbenchServiceProps & WorkbenchActionProps & DispatchProp<any> & WithStyles<CssRules>;
105 interface NavBreadcrumb extends Breadcrumb {
109 interface NavMenuItem extends MainAppBarMenuItem {
113 interface WorkbenchState {
114 isCurrentTokenDialogOpen: boolean;
118 accountMenu: NavMenuItem[],
119 helpMenu: NavMenuItem[],
120 anonymousMenu: NavMenuItem[]
124 export const Workbench = withStyles(styles)(
125 connect<WorkbenchDataProps>(
126 (state: RootState) => ({
127 projects: state.projects.items,
128 currentProjectId: state.projects.currentItemId,
129 user: state.auth.user,
130 currentToken: state.auth.apiToken,
131 sidePanelItems: state.sidePanel
134 class extends React.Component<WorkbenchProps, WorkbenchState> {
136 isCreationDialogOpen: false,
137 isCurrentTokenDialogOpen: false,
144 label: 'Current token',
145 action: () => this.toggleCurrentTokenModal()
149 action: () => this.props.dispatch(logout())
153 action: () => this.props.dispatch(push("/my-account"))
159 action: () => this.props.dispatch(push("/help"))
165 action: () => this.props.dispatch(login())
172 const path = getTreePath(this.props.projects, this.props.currentProjectId);
173 const breadcrumbs = path.map(item => ({
174 label: item.data.name,
175 itemId: item.data.uuid,
179 const { classes, user } = this.props;
181 <div className={classes.root}>
182 <div className={classes.appBar}>
184 breadcrumbs={breadcrumbs}
185 searchText={this.state.searchText}
186 user={this.props.user}
187 menuItems={this.state.menuItems}
188 {...this.mainAppBarActions} />
194 paper: classes.drawerPaper,
196 <div className={classes.toolbar} />
198 toggleOpen={this.toggleSidePanelOpen}
199 toggleActive={this.toggleSidePanelActive}
200 sidePanelItems={this.props.sidePanelItems}
201 onContextMenu={(event) => this.openContextMenu(event, {
202 uuid: this.props.authService.getUuid() || "",
204 kind: ContextMenuKind.ROOT_PROJECT
207 projects={this.props.projects}
208 toggleOpen={itemId => this.props.dispatch(setProjectItem(itemId, ItemMode.OPEN))}
209 onContextMenu={(event, item) => this.openContextMenu(event, {
210 uuid: item.data.uuid,
211 name: item.data.name,
212 kind: ContextMenuKind.PROJECT
214 toggleActive={itemId => {
215 this.props.dispatch(setProjectItem(itemId, ItemMode.ACTIVE));
216 this.props.dispatch(loadDetails(itemId, ResourceKind.PROJECT));
217 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
221 <main className={classes.contentWrapper}>
222 <div className={classes.content}>
224 <Route path="/projects/:id" render={this.renderProjectPanel} />
225 <Route path="/favorites" render={this.renderFavoritePanel} />
226 <Route path="/collections/:id" render={this.renderCollectionPanel} />
229 {user && <DetailsPanel />}
233 <CreateProjectDialog />
234 <CreateCollectionDialog />
235 <UpdateCollectionDialog />
237 currentToken={this.props.currentToken}
238 open={this.state.isCurrentTokenDialogOpen}
239 handleClose={this.toggleCurrentTokenModal} />
244 renderCollectionPanel = (props: RouteComponentProps<{ id: string }>) => <CollectionPanel
245 onItemRouteChange={(collectionId) => this.props.dispatch<any>(loadCollection(collectionId, ResourceKind.COLLECTION))}
246 onContextMenu={(event, item) => {
247 this.openContextMenu(event, {
250 kind: ContextMenuKind.COLLECTION
255 renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
256 onItemRouteChange={itemId => this.props.dispatch(setProjectItem(itemId, ItemMode.ACTIVE))}
257 onContextMenu={(event, item) => {
259 const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
260 this.openContextMenu(event, {
266 onProjectCreationDialogOpen={this.handleProjectCreationDialogOpen}
267 onCollectionCreationDialogOpen={this.handleCollectionCreationDialogOpen}
268 onItemClick={item => {
269 this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
271 onItemDoubleClick={item => {
273 case ResourceKind.COLLECTION:
274 this.props.dispatch(loadCollection(item.uuid, item.kind as ResourceKind));
275 this.props.dispatch(push(getCollectionUrl(item.uuid)));
277 this.props.dispatch(setProjectItem(item.uuid, ItemMode.ACTIVE));
278 this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
284 renderFavoritePanel = (props: RouteComponentProps<{ id: string }>) => <FavoritePanel
285 onItemRouteChange={() => this.props.dispatch(favoritePanelActions.REQUEST_ITEMS())}
286 onContextMenu={(event, item) => {
287 const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
288 this.openContextMenu(event, {
294 onDialogOpen={this.handleProjectCreationDialogOpen}
295 onItemClick={item => {
296 this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
298 onItemDoubleClick={item => {
300 case ResourceKind.COLLECTION:
301 this.props.dispatch(loadCollection(item.uuid, item.kind as ResourceKind));
302 this.props.dispatch(push(getCollectionUrl(item.uuid)));
304 this.props.dispatch(loadDetails(item.uuid, ResourceKind.PROJECT));
305 this.props.dispatch(setProjectItem(item.uuid, ItemMode.ACTIVE));
306 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
312 mainAppBarActions: MainAppBarActionProps = {
313 onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
314 this.props.dispatch(setProjectItem(itemId, ItemMode.BOTH));
315 this.props.dispatch(loadDetails(itemId, ResourceKind.PROJECT));
317 onSearch: searchText => {
318 this.setState({ searchText });
319 this.props.dispatch(push(`/search?q=${searchText}`));
321 onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
322 onDetailsPanelToggle: () => {
323 this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
325 onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: NavBreadcrumb) => {
326 this.openContextMenu(event, {
327 uuid: breadcrumb.itemId,
328 name: breadcrumb.label,
329 kind: ContextMenuKind.PROJECT
334 toggleSidePanelOpen = (itemId: string) => {
335 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
338 toggleSidePanelActive = (itemId: string) => {
339 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
340 this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
341 const panelItem = this.props.sidePanelItems.find(it => it.id === itemId);
342 if (panelItem && panelItem.activeAction) {
343 panelItem.activeAction(this.props.dispatch);
347 handleProjectCreationDialogOpen = (itemUuid: string) => {
348 this.props.dispatch(projectActions.OPEN_PROJECT_CREATOR({ ownerUuid: itemUuid }));
351 handleCollectionCreationDialogOpen = (itemUuid: string) => {
352 this.props.dispatch(collectionCreateActions.OPEN_COLLECTION_CREATOR({ ownerUuid: itemUuid }));
355 openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: { name: string; uuid: string; kind: ContextMenuKind; }) => {
356 event.preventDefault();
358 contextMenuActions.OPEN_CONTEXT_MENU({
359 position: { x: event.clientX, y: event.clientY },
365 toggleCurrentTokenModal = () => {
366 this.setState({ isCurrentTokenDialogOpen: !this.state.isCurrentTokenDialogOpen });