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 { Snackbar } from '../../views-components/snackbar/snackbar';
38 import { favoritePanelActions } from '../../store/favorite-panel/favorite-panel-action';
39 import { CreateCollectionDialog } from '../../views-components/create-collection-dialog/create-collection-dialog';
40 import { CollectionPanel } from '../collection-panel/collection-panel';
41 import { loadCollection, loadCollectionTags } from '../../store/collection-panel/collection-panel-action';
42 import { getCollectionUrl } from '../../models/collection';
43 import { UpdateCollectionDialog } from '../../views-components/update-collection-dialog/update-collection-dialog.';
44 import { AuthService } from "../../services/auth-service/auth-service";
45 import { RenameFileDialog } from '../../views-components/rename-file-dialog/rename-file-dialog';
46 import { FileRemoveDialog } from '../../views-components/file-remove-dialog/file-remove-dialog';
47 import { MultipleFilesRemoveDialog } from '../../views-components/file-remove-dialog/multiple-files-remove-dialog';
48 import { DialogCollectionCreateWithSelectedFile } from '../../views-components/create-collection-dialog-with-selected/create-collection-dialog-with-selected';
50 const DRAWER_WITDH = 240;
51 const APP_BAR_HEIGHT = 100;
53 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
55 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
66 zIndex: theme.zIndex.drawer + 1,
74 flexDirection: 'column',
77 backgroundColor: theme.palette.background.default,
81 paddingTop: APP_BAR_HEIGHT
84 padding: `${theme.spacing.unit}px ${theme.spacing.unit * 3}px`,
88 toolbar: theme.mixins.toolbar
91 interface WorkbenchDataProps {
92 projects: Array<TreeItem<ProjectResource>>;
93 currentProjectId: string;
95 currentToken?: string;
96 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
137 class extends React.Component<WorkbenchProps, WorkbenchState> {
139 isCreationDialogOpen: false,
140 isCurrentTokenDialogOpen: false,
147 label: 'Current token',
148 action: () => this.toggleCurrentTokenModal()
152 action: () => this.props.dispatch(logout())
156 action: () => this.props.dispatch(push("/my-account"))
162 action: () => this.props.dispatch(push("/help"))
168 action: () => this.props.dispatch(login())
175 const path = getTreePath(this.props.projects, this.props.currentProjectId);
176 const breadcrumbs = path.map(item => ({
177 label: item.data.name,
178 itemId: item.data.uuid,
182 const { classes, user } = this.props;
184 <div className={classes.root}>
185 <div className={classes.appBar}>
187 breadcrumbs={breadcrumbs}
188 searchText={this.state.searchText}
189 user={this.props.user}
190 menuItems={this.state.menuItems}
191 {...this.mainAppBarActions} />
197 paper: classes.drawerPaper,
199 <div className={classes.toolbar} />
201 toggleOpen={this.toggleSidePanelOpen}
202 toggleActive={this.toggleSidePanelActive}
203 sidePanelItems={this.props.sidePanelItems}
204 onContextMenu={(event) => this.openContextMenu(event, {
205 uuid: this.props.authService.getUuid() || "",
207 kind: ContextMenuKind.ROOT_PROJECT
210 projects={this.props.projects}
211 toggleOpen={itemId => this.props.dispatch(setProjectItem(itemId, ItemMode.OPEN))}
212 onContextMenu={(event, item) => this.openContextMenu(event, {
213 uuid: item.data.uuid,
214 name: item.data.name,
215 kind: ContextMenuKind.PROJECT
217 toggleActive={itemId => {
218 this.props.dispatch(setProjectItem(itemId, ItemMode.ACTIVE));
219 this.props.dispatch(loadDetails(itemId, ResourceKind.PROJECT));
220 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
224 <main className={classes.contentWrapper}>
225 <div className={classes.content}>
227 <Route path="/projects/:id" render={this.renderProjectPanel} />
228 <Route path="/favorites" render={this.renderFavoritePanel} />
229 <Route path="/collections/:id" render={this.renderCollectionPanel} />
232 {user && <DetailsPanel />}
236 <CreateProjectDialog />
237 <CreateCollectionDialog />
239 <DialogCollectionCreateWithSelectedFile />
241 <MultipleFilesRemoveDialog />
242 <UpdateCollectionDialog />
244 currentToken={this.props.currentToken}
245 open={this.state.isCurrentTokenDialogOpen}
246 handleClose={this.toggleCurrentTokenModal} />
251 renderCollectionPanel = (props: RouteComponentProps<{ id: string }>) => <CollectionPanel
252 onItemRouteChange={(collectionId) => {
253 this.props.dispatch<any>(loadCollection(collectionId));
254 this.props.dispatch<any>(loadCollectionTags(collectionId));
256 onContextMenu={(event, item) => {
257 this.openContextMenu(event, {
260 kind: ContextMenuKind.COLLECTION
265 renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
266 onItemRouteChange={itemId => this.props.dispatch(setProjectItem(itemId, ItemMode.ACTIVE))}
267 onContextMenu={(event, item) => {
269 const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
270 this.openContextMenu(event, {
276 onProjectCreationDialogOpen={this.handleProjectCreationDialogOpen}
277 onCollectionCreationDialogOpen={this.handleCollectionCreationDialogOpen}
278 onItemClick={item => {
279 this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
281 onItemDoubleClick={item => {
283 case ResourceKind.COLLECTION:
284 this.props.dispatch(loadCollection(item.uuid));
285 this.props.dispatch(push(getCollectionUrl(item.uuid)));
287 this.props.dispatch(setProjectItem(item.uuid, ItemMode.ACTIVE));
288 this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
294 renderFavoritePanel = (props: RouteComponentProps<{ id: string }>) => <FavoritePanel
295 onItemRouteChange={() => this.props.dispatch(favoritePanelActions.REQUEST_ITEMS())}
296 onContextMenu={(event, item) => {
297 const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
298 this.openContextMenu(event, {
304 onDialogOpen={this.handleProjectCreationDialogOpen}
305 onItemClick={item => {
306 this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
308 onItemDoubleClick={item => {
310 case ResourceKind.COLLECTION:
311 this.props.dispatch(loadCollection(item.uuid));
312 this.props.dispatch(push(getCollectionUrl(item.uuid)));
314 this.props.dispatch(loadDetails(item.uuid, ResourceKind.PROJECT));
315 this.props.dispatch(setProjectItem(item.uuid, ItemMode.ACTIVE));
316 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
322 mainAppBarActions: MainAppBarActionProps = {
323 onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
324 this.props.dispatch(setProjectItem(itemId, ItemMode.BOTH));
325 this.props.dispatch(loadDetails(itemId, ResourceKind.PROJECT));
327 onSearch: searchText => {
328 this.setState({ searchText });
329 this.props.dispatch(push(`/search?q=${searchText}`));
331 onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
332 onDetailsPanelToggle: () => {
333 this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
335 onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: NavBreadcrumb) => {
336 this.openContextMenu(event, {
337 uuid: breadcrumb.itemId,
338 name: breadcrumb.label,
339 kind: ContextMenuKind.PROJECT
344 toggleSidePanelOpen = (itemId: string) => {
345 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
348 toggleSidePanelActive = (itemId: string) => {
349 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
350 this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
351 const panelItem = this.props.sidePanelItems.find(it => it.id === itemId);
352 if (panelItem && panelItem.activeAction) {
353 panelItem.activeAction(this.props.dispatch);
357 handleProjectCreationDialogOpen = (itemUuid: string) => {
358 this.props.dispatch(projectActions.OPEN_PROJECT_CREATOR({ ownerUuid: itemUuid }));
361 handleCollectionCreationDialogOpen = (itemUuid: string) => {
362 this.props.dispatch(collectionCreateActions.OPEN_COLLECTION_CREATOR({ ownerUuid: itemUuid }));
365 openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: { name: string; uuid: string; kind: ContextMenuKind; }) => {
366 event.preventDefault();
368 contextMenuActions.OPEN_CONTEXT_MENU({
369 position: { x: event.clientX, y: event.clientY },
375 toggleCurrentTokenModal = () => {
376 this.setState({ isCurrentTokenDialogOpen: !this.state.isCurrentTokenDialogOpen });