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 } 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 { favoritePanelActions } from '../../store/favorite-panel/favorite-panel-action';
41 import { CreateCollectionDialog } from '../../views-components/create-collection-dialog/create-collection-dialog';
42 import { CollectionPanel } from '../collection-panel/collection-panel';
43 import { loadCollection } from '../../store/collection-panel/collection-panel-action';
44 import { getCollectionUrl } from '../../models/collection';
46 const drawerWidth = 240;
47 const appBarHeight = 100;
49 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
51 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
62 zIndex: theme.zIndex.drawer + 1,
70 flexDirection: 'column',
73 backgroundColor: theme.palette.background.default,
77 paddingTop: appBarHeight
80 padding: `${theme.spacing.unit}px ${theme.spacing.unit * 3}px`,
84 toolbar: theme.mixins.toolbar
87 interface WorkbenchDataProps {
88 projects: Array<TreeItem<ProjectResource>>;
89 currentProjectId: string;
91 currentToken?: string;
92 sidePanelItems: SidePanelItem[];
95 interface WorkbenchActionProps {
98 type WorkbenchProps = WorkbenchDataProps & WorkbenchActionProps & DispatchProp & WithStyles<CssRules>;
100 interface NavBreadcrumb extends Breadcrumb {
104 interface NavMenuItem extends MainAppBarMenuItem {
108 interface WorkbenchState {
109 isCurrentTokenDialogOpen: boolean;
113 accountMenu: NavMenuItem[],
114 helpMenu: NavMenuItem[],
115 anonymousMenu: NavMenuItem[]
119 export const Workbench = withStyles(styles)(
120 connect<WorkbenchDataProps>(
121 (state: RootState) => ({
122 projects: state.projects.items,
123 currentProjectId: state.projects.currentItemId,
124 user: state.auth.user,
125 currentToken: state.auth.apiToken,
126 sidePanelItems: state.sidePanel
129 class extends React.Component<WorkbenchProps, WorkbenchState> {
131 isCreationDialogOpen: false,
132 isCurrentTokenDialogOpen: false,
139 label: 'Current token',
140 action: () => this.toggleCurrentTokenModal()
144 action: () => this.props.dispatch(authActions.LOGOUT())
148 action: () => this.props.dispatch(push("/my-account"))
154 action: () => this.props.dispatch(push("/help"))
160 action: () => this.props.dispatch(authActions.LOGIN())
167 const path = getTreePath(this.props.projects, this.props.currentProjectId);
168 const breadcrumbs = path.map(item => ({
169 label: item.data.name,
170 itemId: item.data.uuid,
174 const { classes, user } = this.props;
176 <div className={classes.root}>
177 <div className={classes.appBar}>
179 breadcrumbs={breadcrumbs}
180 searchText={this.state.searchText}
181 user={this.props.user}
182 menuItems={this.state.menuItems}
183 {...this.mainAppBarActions} />
189 paper: classes.drawerPaper,
191 <div className={classes.toolbar} />
193 toggleOpen={this.toggleSidePanelOpen}
194 toggleActive={this.toggleSidePanelActive}
195 sidePanelItems={this.props.sidePanelItems}
196 onContextMenu={(event) => this.openContextMenu(event, {
197 uuid: authService.getUuid() || "",
199 kind: ContextMenuKind.ROOT_PROJECT
202 projects={this.props.projects}
203 toggleOpen={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.OPEN))}
204 onContextMenu={(event, item) => this.openContextMenu(event, {
205 uuid: item.data.uuid,
206 name: item.data.name,
207 kind: ContextMenuKind.PROJECT
209 toggleActive={itemId => {
210 this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE));
211 this.props.dispatch<any>(loadDetails(itemId, ResourceKind.PROJECT));
212 this.props.dispatch<any>(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
216 <main className={classes.contentWrapper}>
217 <div className={classes.content}>
219 <Route path="/projects/:id" render={this.renderProjectPanel} />
220 <Route path="/favorites" render={this.renderFavoritePanel} />
221 <Route path="/collections/:id" render={this.renderCollectionPanel} />
224 {user && <DetailsPanel />}
228 <CreateProjectDialog />
229 <CreateCollectionDialog />
231 currentToken={this.props.currentToken}
232 open={this.state.isCurrentTokenDialogOpen}
233 handleClose={this.toggleCurrentTokenModal} />
238 renderCollectionPanel = (props: RouteComponentProps<{ id: string }>) => <CollectionPanel
239 onItemRouteChange={(collectionId) => this.props.dispatch<any>(loadCollection(collectionId, ResourceKind.COLLECTION))}
240 onContextMenu={(event, item) => {
241 this.openContextMenu(event, {
244 kind: ContextMenuKind.COLLECTION
249 renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
250 onItemRouteChange={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE))}
251 onContextMenu={(event, item) => {
253 const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
254 this.openContextMenu(event, {
260 onProjectCreationDialogOpen={this.handleProjectCreationDialogOpen}
261 onCollectionCreationDialogOpen={this.handleCollectionCreationDialogOpen}
262 onItemClick={item => {
263 this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
265 onItemDoubleClick={item => {
267 case ResourceKind.COLLECTION:
268 this.props.dispatch<any>(loadCollection(item.uuid, item.kind as ResourceKind));
269 this.props.dispatch(push(getCollectionUrl(item.uuid)));
271 this.props.dispatch<any>(setProjectItem(item.uuid, ItemMode.ACTIVE));
272 this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
278 renderFavoritePanel = (props: RouteComponentProps<{ id: string }>) => <FavoritePanel
279 onItemRouteChange={() => this.props.dispatch<any>(favoritePanelActions.REQUEST_ITEMS())}
280 onContextMenu={(event, item) => {
281 const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
282 this.openContextMenu(event, {
288 onDialogOpen={this.handleProjectCreationDialogOpen}
289 onItemClick={item => {
290 this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
292 onItemDoubleClick={item => {
294 case ResourceKind.COLLECTION:
295 this.props.dispatch<any>(loadCollection(item.uuid, item.kind as ResourceKind));
296 this.props.dispatch(push(getCollectionUrl(item.uuid)));
298 this.props.dispatch<any>(loadDetails(item.uuid, ResourceKind.PROJECT));
299 this.props.dispatch<any>(setProjectItem(item.uuid, ItemMode.ACTIVE));
300 this.props.dispatch<any>(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
306 mainAppBarActions: MainAppBarActionProps = {
307 onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
308 this.props.dispatch<any>(setProjectItem(itemId, ItemMode.BOTH));
309 this.props.dispatch<any>(loadDetails(itemId, ResourceKind.PROJECT));
311 onSearch: searchText => {
312 this.setState({ searchText });
313 this.props.dispatch(push(`/search?q=${searchText}`));
315 onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
316 onDetailsPanelToggle: () => {
317 this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
319 onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: NavBreadcrumb) => {
320 this.openContextMenu(event, {
321 uuid: breadcrumb.itemId,
322 name: breadcrumb.label,
323 kind: ContextMenuKind.PROJECT
328 toggleSidePanelOpen = (itemId: string) => {
329 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
332 toggleSidePanelActive = (itemId: string) => {
333 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
334 this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
335 const panelItem = this.props.sidePanelItems.find(it => it.id === itemId);
336 if (panelItem && panelItem.activeAction) {
337 panelItem.activeAction(this.props.dispatch);
341 handleProjectCreationDialogOpen = (itemUuid: string) => {
342 this.props.dispatch(projectActions.OPEN_PROJECT_CREATOR({ ownerUuid: itemUuid }));
345 handleCollectionCreationDialogOpen = (itemUuid: string) => {
346 this.props.dispatch(collectionCreateActions.OPEN_COLLECTION_CREATOR({ ownerUuid: itemUuid }));
349 openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: { name: string; uuid: string; kind: ContextMenuKind; }) => {
350 event.preventDefault();
352 contextMenuActions.OPEN_CONTEXT_MENU({
353 position: { x: event.clientX, y: event.clientY },
359 toggleCurrentTokenModal = () => {
360 this.setState({ isCurrentTokenDialogOpen: !this.state.isCurrentTokenDialogOpen });