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, RouteComponentProps, Switch, Redirect } 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 { reset } from 'redux-form';
17 import { ProjectTree } from '~/views-components/project-tree/project-tree';
18 import { TreeItem } from "~/components/tree/tree";
19 import { getTreePath } from '~/store/project/project-reducer';
20 import { sidePanelActions } from '~/store/side-panel/side-panel-action';
21 import { SidePanel, SidePanelItem } from '~/components/side-panel/side-panel';
22 import { ItemMode, setProjectItem } from "~/store/navigation/navigation-action";
23 import { projectActions } from "~/store/project/project-action";
24 import { collectionCreateActions } from '~/store/collections/creator/collection-creator-action';
25 import { ProjectPanel } from "~/views/project-panel/project-panel";
26 import { DetailsPanel } from '~/views-components/details-panel/details-panel';
27 import { ArvadosTheme } from '~/common/custom-theme';
28 import { CreateProjectDialog } from "~/views-components/create-project-dialog/create-project-dialog";
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 { 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, loadCollectionTags } 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 { UpdateProjectDialog } from '~/views-components/update-project-dialog/update-project-dialog';
46 import { AuthService } from "~/services/auth-service/auth-service";
47 import { RenameFileDialog } from '~/views-components/rename-file-dialog/rename-file-dialog';
48 import { FileRemoveDialog } from '~/views-components/file-remove-dialog/file-remove-dialog';
49 import { MultipleFilesRemoveDialog } from '~/views-components/file-remove-dialog/multiple-files-remove-dialog';
50 import { DialogCollectionCreateWithSelectedFile } from '~/views-components/create-collection-dialog-with-selected/create-collection-dialog-with-selected';
51 import { COLLECTION_CREATE_DIALOG } from '~/views-components/dialog-create/dialog-collection-create';
52 import { PROJECT_CREATE_DIALOG } from '~/views-components/dialog-create/dialog-project-create';
54 const DRAWER_WITDH = 240;
55 const APP_BAR_HEIGHT = 100;
57 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
59 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
70 zIndex: theme.zIndex.drawer + 1,
78 flexDirection: 'column',
81 backgroundColor: theme.palette.background.default,
85 paddingTop: APP_BAR_HEIGHT
88 padding: `${theme.spacing.unit}px ${theme.spacing.unit * 3}px`,
93 toolbar: theme.mixins.toolbar
96 interface WorkbenchDataProps {
97 projects: Array<TreeItem<ProjectResource>>;
98 currentProjectId: string;
100 currentToken?: string;
101 sidePanelItems: SidePanelItem[];
104 interface WorkbenchServiceProps {
105 authService: AuthService;
108 interface WorkbenchActionProps {
111 type WorkbenchProps = WorkbenchDataProps & WorkbenchServiceProps & WorkbenchActionProps & DispatchProp<any> & WithStyles<CssRules>;
113 interface NavBreadcrumb extends Breadcrumb {
117 interface NavMenuItem extends MainAppBarMenuItem {
121 interface WorkbenchState {
122 isCurrentTokenDialogOpen: boolean;
126 accountMenu: NavMenuItem[],
127 helpMenu: NavMenuItem[],
128 anonymousMenu: NavMenuItem[]
132 export const Workbench = withStyles(styles)(
133 connect<WorkbenchDataProps>(
134 (state: RootState) => ({
135 projects: state.projects.items,
136 currentProjectId: state.projects.currentItemId,
137 user: state.auth.user,
138 currentToken: state.auth.apiToken,
139 sidePanelItems: state.sidePanel
142 class extends React.Component<WorkbenchProps, WorkbenchState> {
144 isCreationDialogOpen: false,
145 isCurrentTokenDialogOpen: false,
152 label: 'Current token',
153 action: () => this.toggleCurrentTokenModal()
157 action: () => this.props.dispatch(logout())
161 action: () => this.props.dispatch(push("/my-account"))
167 action: () => this.props.dispatch(push("/help"))
173 action: () => this.props.dispatch(login())
180 const path = getTreePath(this.props.projects, this.props.currentProjectId);
181 const breadcrumbs = path.map(item => ({
182 label: item.data.name,
183 itemId: item.data.uuid,
187 const { classes, user } = this.props;
189 <div className={classes.root}>
190 <div className={classes.appBar}>
192 breadcrumbs={breadcrumbs}
193 searchText={this.state.searchText}
194 user={this.props.user}
195 menuItems={this.state.menuItems}
196 {...this.mainAppBarActions} />
202 paper: classes.drawerPaper,
204 <div className={classes.toolbar} />
206 toggleOpen={this.toggleSidePanelOpen}
207 toggleActive={this.toggleSidePanelActive}
208 sidePanelItems={this.props.sidePanelItems}
209 onContextMenu={(event) => this.openContextMenu(event, {
210 uuid: this.props.authService.getUuid() || "",
212 kind: ContextMenuKind.ROOT_PROJECT
215 projects={this.props.projects}
216 toggleOpen={itemId => this.props.dispatch(setProjectItem(itemId, ItemMode.OPEN))}
217 onContextMenu={(event, item) => this.openContextMenu(event, {
218 uuid: item.data.uuid,
219 name: item.data.name,
220 kind: ContextMenuKind.PROJECT
222 toggleActive={itemId => {
223 this.props.dispatch(setProjectItem(itemId, ItemMode.ACTIVE));
224 this.props.dispatch(loadDetails(itemId, ResourceKind.PROJECT));
225 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
229 <main className={classes.contentWrapper}>
230 <div className={classes.content}>
232 <Route path='/' exact render={() => <Redirect to={`/projects/${this.props.authService.getUuid()}`} />} />
233 <Route path="/projects/:id" render={this.renderProjectPanel} />
234 <Route path="/favorites" render={this.renderFavoritePanel} />
235 <Route path="/collections/:id" render={this.renderCollectionPanel} />
238 {user && <DetailsPanel />}
242 <CreateProjectDialog />
243 <CreateCollectionDialog />
245 <DialogCollectionCreateWithSelectedFile />
247 <MultipleFilesRemoveDialog />
248 <UpdateCollectionDialog />
249 <UpdateProjectDialog />
251 currentToken={this.props.currentToken}
252 open={this.state.isCurrentTokenDialogOpen}
253 handleClose={this.toggleCurrentTokenModal} />
258 renderCollectionPanel = (props: RouteComponentProps<{ id: string }>) => <CollectionPanel
259 onItemRouteChange={(collectionId) => {
260 this.props.dispatch<any>(loadCollection(collectionId, ResourceKind.COLLECTION));
261 this.props.dispatch<any>(loadCollectionTags(collectionId));
263 onContextMenu={(event, item) => {
264 this.openContextMenu(event, {
267 description: item.description,
268 kind: ContextMenuKind.COLLECTION
273 renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
274 onItemRouteChange={itemId => this.props.dispatch(setProjectItem(itemId, ItemMode.ACTIVE))}
275 onContextMenu={(event, item) => {
276 let kind: ContextMenuKind;
278 if (item.kind === ResourceKind.PROJECT) {
279 kind = ContextMenuKind.PROJECT;
280 } else if (item.kind === ResourceKind.COLLECTION) {
281 kind = ContextMenuKind.COLLECTION_RESOURCE;
283 kind = ContextMenuKind.RESOURCE;
286 this.openContextMenu(event, {
289 description: item.description,
293 onProjectCreationDialogOpen={this.handleProjectCreationDialogOpen}
294 onCollectionCreationDialogOpen={this.handleCollectionCreationDialogOpen}
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(setProjectItem(item.uuid, ItemMode.ACTIVE));
305 this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
311 renderFavoritePanel = (props: RouteComponentProps<{ id: string }>) => <FavoritePanel
312 onItemRouteChange={() => this.props.dispatch(favoritePanelActions.REQUEST_ITEMS())}
313 onContextMenu={(event, item) => {
314 const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
315 this.openContextMenu(event, {
321 onDialogOpen={this.handleProjectCreationDialogOpen}
322 onItemClick={item => {
323 this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
325 onItemDoubleClick={item => {
327 case ResourceKind.COLLECTION:
328 this.props.dispatch(loadCollection(item.uuid, item.kind as ResourceKind));
329 this.props.dispatch(push(getCollectionUrl(item.uuid)));
331 this.props.dispatch(loadDetails(item.uuid, ResourceKind.PROJECT));
332 this.props.dispatch(setProjectItem(item.uuid, ItemMode.ACTIVE));
333 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
339 mainAppBarActions: MainAppBarActionProps = {
340 onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
341 this.props.dispatch(setProjectItem(itemId, ItemMode.BOTH));
342 this.props.dispatch(loadDetails(itemId, ResourceKind.PROJECT));
344 onSearch: searchText => {
345 this.setState({ searchText });
346 this.props.dispatch(push(`/search?q=${searchText}`));
348 onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
349 onDetailsPanelToggle: () => {
350 this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
352 onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: NavBreadcrumb) => {
353 this.openContextMenu(event, {
354 uuid: breadcrumb.itemId,
355 name: breadcrumb.label,
356 kind: ContextMenuKind.PROJECT
361 toggleSidePanelOpen = (itemId: string) => {
362 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
365 toggleSidePanelActive = (itemId: string) => {
366 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
367 this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
369 const panelItem = this.props.sidePanelItems.find(it => it.id === itemId);
370 if (panelItem && panelItem.activeAction) {
371 panelItem.activeAction(this.props.dispatch, this.props.authService.getUuid());
375 handleProjectCreationDialogOpen = (itemUuid: string) => {
376 this.props.dispatch(reset(PROJECT_CREATE_DIALOG));
377 this.props.dispatch(projectActions.OPEN_PROJECT_CREATOR({ ownerUuid: itemUuid }));
380 handleCollectionCreationDialogOpen = (itemUuid: string) => {
381 this.props.dispatch(reset(COLLECTION_CREATE_DIALOG));
382 this.props.dispatch(collectionCreateActions.OPEN_COLLECTION_CREATOR({ ownerUuid: itemUuid }));
385 openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: { name: string; uuid: string; description?: string; kind: ContextMenuKind; }) => {
386 event.preventDefault();
388 contextMenuActions.OPEN_CONTEXT_MENU({
389 position: { x: event.clientX, y: event.clientY },
395 toggleCurrentTokenModal = () => {
396 this.setState({ isCurrentTokenDialogOpen: !this.state.isCurrentTokenDialogOpen });