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, restoreBranch } 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 { RemoveDialog } from '../../views-components/remove-dialog/remove-dialog';
45 import { RenameDialog } from '../../views-components/rename-dialog/rename-dialog';
46 import { UpdateCollectionDialog } from '../../views-components/update-collection-dialog/update-collection-dialog.';
47 import { AuthService } from "../../services/auth-service/auth-service";
49 const DRAWER_WITDH = 240;
50 const APP_BAR_HEIGHT = 100;
52 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
54 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
65 zIndex: theme.zIndex.drawer + 1,
73 flexDirection: 'column',
76 backgroundColor: theme.palette.background.default,
80 paddingTop: APP_BAR_HEIGHT
83 padding: `${theme.spacing.unit}px ${theme.spacing.unit * 3}px`,
87 toolbar: theme.mixins.toolbar
90 interface WorkbenchDataProps {
91 projects: Array<TreeItem<ProjectResource>>;
92 currentProjectId: string;
94 currentToken?: string;
95 sidePanelItems: SidePanelItem[];
99 interface WorkbenchServiceProps {
100 authService: AuthService;
103 interface WorkbenchActionProps {
106 type WorkbenchProps = WorkbenchDataProps & WorkbenchServiceProps & WorkbenchActionProps & DispatchProp<any> & WithStyles<CssRules>;
108 interface NavBreadcrumb extends Breadcrumb {
112 interface NavMenuItem extends MainAppBarMenuItem {
116 interface WorkbenchState {
117 isCurrentTokenDialogOpen: boolean;
121 accountMenu: NavMenuItem[],
122 helpMenu: NavMenuItem[],
123 anonymousMenu: NavMenuItem[]
127 export const Workbench = withStyles(styles)(
128 connect<WorkbenchDataProps>(
129 (state: RootState) => ({
130 projects: state.projects.items,
131 currentProjectId: state.projects.currentItemId,
132 user: state.auth.user,
133 currentToken: state.auth.apiToken,
134 sidePanelItems: state.sidePanel,
135 router: state.router.location
138 class extends React.Component<WorkbenchProps, WorkbenchState> {
140 isCreationDialogOpen: false,
141 isCurrentTokenDialogOpen: false,
148 label: 'Current token',
149 action: () => this.toggleCurrentTokenModal()
153 action: () => this.props.dispatch(logout())
157 action: () => this.props.dispatch(push("/my-account"))
163 action: () => this.props.dispatch(push("/help"))
169 action: () => this.props.dispatch(login())
175 componentDidMount() {
176 const PROJECT_URL_REGEX = /\/projects\/(.*)/;
177 const getProjectIdFromUrl = (url: string) => {
178 const match = PROJECT_URL_REGEX.exec(url);
179 return match ? match[1] : match;
182 const id = getProjectIdFromUrl(this.props.router.pathname);
184 this.props.dispatch<any>(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
185 this.props.dispatch<any>(restoreBranch(id));
190 const path = getTreePath(this.props.projects, this.props.currentProjectId);
191 const breadcrumbs = path.map(item => ({
192 label: item.data.name,
193 itemId: item.data.uuid,
197 const { classes, user } = this.props;
199 <div className={classes.root}>
200 <div className={classes.appBar}>
202 breadcrumbs={breadcrumbs}
203 searchText={this.state.searchText}
204 user={this.props.user}
205 menuItems={this.state.menuItems}
206 {...this.mainAppBarActions} />
212 paper: classes.drawerPaper,
214 <div className={classes.toolbar} />
216 toggleOpen={this.toggleSidePanelOpen}
217 toggleActive={this.toggleSidePanelActive}
218 sidePanelItems={this.props.sidePanelItems}
219 onContextMenu={(event) => this.openContextMenu(event, {
220 uuid: this.props.authService.getUuid() || "",
222 kind: ContextMenuKind.ROOT_PROJECT
225 projects={this.props.projects}
226 toggleOpen={itemId => this.props.dispatch(setProjectItem(itemId, ItemMode.OPEN))}
227 onContextMenu={(event, item) => this.openContextMenu(event, {
228 uuid: item.data.uuid,
229 name: item.data.name,
230 kind: ContextMenuKind.PROJECT
232 toggleActive={itemId => {
233 this.props.dispatch(setProjectItem(itemId, ItemMode.ACTIVE));
234 this.props.dispatch(loadDetails(itemId, ResourceKind.PROJECT));
235 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
239 <main className={classes.contentWrapper}>
240 <div className={classes.content}>
242 <Route path="/projects/:id" render={this.renderProjectPanel} />
243 <Route path="/favorites" render={this.renderFavoritePanel} />
244 <Route path="/collections/:id" render={this.renderCollectionPanel} />
247 {user && <DetailsPanel />}
251 <CreateProjectDialog />
252 <CreateCollectionDialog />
255 <UpdateCollectionDialog />
257 currentToken={this.props.currentToken}
258 open={this.state.isCurrentTokenDialogOpen}
259 handleClose={this.toggleCurrentTokenModal} />
264 renderCollectionPanel = (props: RouteComponentProps<{ id: string }>) => <CollectionPanel
265 onItemRouteChange={(collectionId) => this.props.dispatch<any>(loadCollection(collectionId, ResourceKind.COLLECTION))}
266 onContextMenu={(event, item) => {
267 this.openContextMenu(event, {
270 kind: ContextMenuKind.COLLECTION
275 renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
276 onItemRouteChange={itemId => this.props.dispatch(setProjectItem(itemId, ItemMode.ACTIVE))}
277 onContextMenu={(event, item) => {
279 const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
280 this.openContextMenu(event, {
286 onProjectCreationDialogOpen={this.handleProjectCreationDialogOpen}
287 onCollectionCreationDialogOpen={this.handleCollectionCreationDialogOpen}
288 onItemClick={item => {
289 this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
291 onItemDoubleClick={item => {
293 case ResourceKind.COLLECTION:
294 this.props.dispatch(loadCollection(item.uuid, item.kind as ResourceKind));
295 this.props.dispatch(push(getCollectionUrl(item.uuid)));
297 this.props.dispatch(setProjectItem(item.uuid, ItemMode.ACTIVE));
298 this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
304 renderFavoritePanel = (props: RouteComponentProps<{ id: string }>) => <FavoritePanel
305 onItemRouteChange={() => this.props.dispatch(favoritePanelActions.REQUEST_ITEMS())}
306 onContextMenu={(event, item) => {
307 const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
308 this.openContextMenu(event, {
314 onDialogOpen={this.handleProjectCreationDialogOpen}
315 onItemClick={item => {
316 this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
318 onItemDoubleClick={item => {
320 case ResourceKind.COLLECTION:
321 this.props.dispatch(loadCollection(item.uuid, item.kind as ResourceKind));
322 this.props.dispatch(push(getCollectionUrl(item.uuid)));
324 this.props.dispatch(loadDetails(item.uuid, ResourceKind.PROJECT));
325 this.props.dispatch(setProjectItem(item.uuid, ItemMode.ACTIVE));
326 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
332 mainAppBarActions: MainAppBarActionProps = {
333 onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
334 this.props.dispatch(setProjectItem(itemId, ItemMode.BOTH));
335 this.props.dispatch(loadDetails(itemId, ResourceKind.PROJECT));
337 onSearch: searchText => {
338 this.setState({ searchText });
339 this.props.dispatch(push(`/search?q=${searchText}`));
341 onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
342 onDetailsPanelToggle: () => {
343 this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
345 onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: NavBreadcrumb) => {
346 this.openContextMenu(event, {
347 uuid: breadcrumb.itemId,
348 name: breadcrumb.label,
349 kind: ContextMenuKind.PROJECT
354 toggleSidePanelOpen = (itemId: string) => {
355 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
358 toggleSidePanelActive = (itemId: string) => {
359 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
360 this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
361 const panelItem = this.props.sidePanelItems.find(it => it.id === itemId);
362 if (panelItem && panelItem.activeAction) {
363 panelItem.activeAction(this.props.dispatch);
367 handleProjectCreationDialogOpen = (itemUuid: string) => {
368 this.props.dispatch(projectActions.OPEN_PROJECT_CREATOR({ ownerUuid: itemUuid }));
371 handleCollectionCreationDialogOpen = (itemUuid: string) => {
372 this.props.dispatch(collectionCreateActions.OPEN_COLLECTION_CREATOR({ ownerUuid: itemUuid }));
375 openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: { name: string; uuid: string; kind: ContextMenuKind; }) => {
376 event.preventDefault();
378 contextMenuActions.OPEN_CONTEXT_MENU({
379 position: { x: event.clientX, y: event.clientY },
385 toggleCurrentTokenModal = () => {
386 this.setState({ isCurrentTokenDialogOpen: !this.state.isCurrentTokenDialogOpen });