160e12f84017e5a99e19c6e0084d3546dbdd59c9
[arvados-workbench2.git] / src / views / workbench / workbench.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
29
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, FAVORITE_PANEL_ID } 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 { 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
45 const drawerWidth = 240;
46 const appBarHeight = 100;
47
48 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
49
50 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
51     root: {
52         flexGrow: 1,
53         zIndex: 1,
54         overflow: 'hidden',
55         position: 'relative',
56         display: 'flex',
57         width: '100vw',
58         height: '100vh'
59     },
60     appBar: {
61         zIndex: theme.zIndex.drawer + 1,
62         position: "absolute",
63         width: "100%"
64     },
65     drawerPaper: {
66         position: 'relative',
67         width: drawerWidth,
68         display: 'flex',
69         flexDirection: 'column',
70     },
71     contentWrapper: {
72         backgroundColor: theme.palette.background.default,
73         display: "flex",
74         flexGrow: 1,
75         minWidth: 0,
76         paddingTop: appBarHeight
77     },
78     content: {
79         padding: `${theme.spacing.unit}px ${theme.spacing.unit * 3}px`,
80         overflowY: "auto",
81         flexGrow: 1
82     },
83     toolbar: theme.mixins.toolbar
84 });
85
86 interface WorkbenchDataProps {
87     projects: Array<TreeItem<ProjectResource>>;
88     currentProjectId: string;
89     user?: User;
90     currentToken?: string;
91     sidePanelItems: SidePanelItem[];
92 }
93
94 interface WorkbenchActionProps {
95 }
96
97 type WorkbenchProps = WorkbenchDataProps & WorkbenchActionProps & DispatchProp & WithStyles<CssRules>;
98
99 interface NavBreadcrumb extends Breadcrumb {
100     itemId: string;
101 }
102
103 interface NavMenuItem extends MainAppBarMenuItem {
104     action: () => void;
105 }
106
107 interface WorkbenchState {
108     isCurrentTokenDialogOpen: boolean;
109     anchorEl: any;
110     searchText: string;
111     menuItems: {
112         accountMenu: NavMenuItem[],
113         helpMenu: NavMenuItem[],
114         anonymousMenu: NavMenuItem[]
115     };
116 }
117
118 export const Workbench = withStyles(styles)(
119     connect<WorkbenchDataProps>(
120         (state: RootState) => ({
121             projects: state.projects.items,
122             currentProjectId: state.projects.currentItemId,
123             user: state.auth.user,
124             currentToken: state.auth.apiToken,
125             sidePanelItems: state.sidePanel
126         })
127     )(
128         class extends React.Component<WorkbenchProps, WorkbenchState> {
129             state = {
130                 isCreationDialogOpen: false,
131                 isCurrentTokenDialogOpen: false,
132                 anchorEl: null,
133                 searchText: "",
134                 breadcrumbs: [],
135                 menuItems: {
136                     accountMenu: [
137                         {
138                             label: 'Current token',
139                             action: () => this.toggleCurrentTokenModal()
140                         },
141                         {
142                             label: "Logout",
143                             action: () => this.props.dispatch(authActions.LOGOUT())
144                         },
145                         {
146                             label: "My account",
147                             action: () => this.props.dispatch(push("/my-account"))
148                         }
149                     ],
150                     helpMenu: [
151                         {
152                             label: "Help",
153                             action: () => this.props.dispatch(push("/help"))
154                         }
155                     ],
156                     anonymousMenu: [
157                         {
158                             label: "Sign in",
159                             action: () => this.props.dispatch(authActions.LOGIN())
160                         }
161                     ]
162                 }
163             };
164
165             render() {
166                 const path = getTreePath(this.props.projects, this.props.currentProjectId);
167                 const breadcrumbs = path.map(item => ({
168                     label: item.data.name,
169                     itemId: item.data.uuid,
170                     status: item.status
171                 }));
172
173                 const { classes, user } = this.props;
174                 return (
175                     <div className={classes.root}>
176                         <div className={classes.appBar}>
177                             <MainAppBar
178                                 breadcrumbs={breadcrumbs}
179                                 searchText={this.state.searchText}
180                                 user={this.props.user}
181                                 menuItems={this.state.menuItems}
182                                 {...this.mainAppBarActions} />
183                         </div>
184                         {user &&
185                             <Drawer
186                                 variant="permanent"
187                                 classes={{
188                                     paper: classes.drawerPaper,
189                                 }}>
190                                 <div className={classes.toolbar} />
191                                 <SidePanel
192                                     toggleOpen={this.toggleSidePanelOpen}
193                                     toggleActive={this.toggleSidePanelActive}
194                                     sidePanelItems={this.props.sidePanelItems}
195                                     onContextMenu={(event) => this.openContextMenu(event, {
196                                         uuid: authService.getUuid() || "",
197                                         name: "",
198                                         kind: ContextMenuKind.ROOT_PROJECT
199                                     })}>
200                                     <ProjectTree
201                                         projects={this.props.projects}
202                                         toggleOpen={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.OPEN))}
203                                         onContextMenu={(event, item) => this.openContextMenu(event, {
204                                             uuid: item.data.uuid,
205                                             name: item.data.name,
206                                             kind: ContextMenuKind.PROJECT
207                                         })}
208                                         toggleActive={itemId => {
209                                             this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE));
210                                             this.props.dispatch<any>(loadDetails(itemId, ResourceKind.PROJECT));
211                                             this.props.dispatch<any>(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
212                                         }} />
213                                 </SidePanel>
214                             </Drawer>}
215                         <main className={classes.contentWrapper}>
216                             <div className={classes.content}>
217                                 <Switch>
218                                     <Route path="/projects/:id" render={this.renderProjectPanel} />
219                                     <Route path="/favorites" render={this.renderFavoritePanel} />
220                                     <Route path="/collections/:id" render={this.renderCollectionPanel} />
221                                 </Switch>
222                             </div>
223                             {user && <DetailsPanel />}
224                         </main>
225                         <ContextMenu />
226                         <Snackbar />
227                         <CreateProjectDialog />
228                         <CreateCollectionDialog />
229                         <CurrentTokenDialog
230                             currentToken={this.props.currentToken}
231                             open={this.state.isCurrentTokenDialogOpen}
232                             handleClose={this.toggleCurrentTokenModal} />
233                     </div>
234                 );
235             }
236
237             renderCollectionPanel = (props: RouteComponentProps<{ id: string }>) => <CollectionPanel 
238                 onItemRouteChange={(collectionId) => this.props.dispatch<any>(loadCollection(collectionId, ResourceKind.COLLECTION))}
239                 onContextMenu={(event, item) => {
240                     this.openContextMenu(event, {
241                         uuid: item.uuid,
242                         name: item.name,
243                         kind: ContextMenuKind.COLLECTION
244                     });
245                 }}
246                 {...props} />
247
248             renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
249                 onItemRouteChange={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE))}
250                 onContextMenu={(event, item) => {
251
252                     const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
253                     this.openContextMenu(event, {
254                         uuid: item.uuid,
255                         name: item.name,
256                         kind
257                     });
258                 }}
259                 onProjectCreationDialogOpen={this.handleProjectCreationDialogOpen}
260                 onCollectionCreationDialogOpen={this.handleCollectionCreationDialogOpen}
261                 onItemClick={item => {
262                     this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
263                 }}
264                 onItemDoubleClick={item => {
265                     switch (item.kind) {
266                         case ResourceKind.COLLECTION:
267                             this.props.dispatch<any>(loadCollection(item.uuid, item.kind as ResourceKind));
268                             this.props.dispatch(push(getCollectionUrl(item.uuid)));
269                         default: 
270                             this.props.dispatch<any>(setProjectItem(item.uuid, ItemMode.ACTIVE));
271                             this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
272                     }
273
274                 }}
275                 {...props} />
276
277             renderFavoritePanel = (props: RouteComponentProps<{ id: string }>) => <FavoritePanel
278                 onItemRouteChange={() => this.props.dispatch<any>(dataExplorerActions.REQUEST_ITEMS({ id: FAVORITE_PANEL_ID }))}
279                 onContextMenu={(event, item) => {
280                     const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
281                     this.openContextMenu(event, {
282                         uuid: item.uuid,
283                         name: item.name,
284                         kind,
285                     });
286                 }}
287                 onDialogOpen={this.handleProjectCreationDialogOpen}
288                 onItemClick={item => {
289                     this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
290                 }}
291                 onItemDoubleClick={item => {
292                     switch (item.kind) {
293                         case ResourceKind.COLLECTION:
294                             this.props.dispatch<any>(loadCollection(item.uuid, item.kind as ResourceKind));
295                             this.props.dispatch(push(getCollectionUrl(item.uuid)));
296                         default:
297                             this.props.dispatch<any>(loadDetails(item.uuid, ResourceKind.PROJECT));
298                             this.props.dispatch<any>(setProjectItem(item.uuid, ItemMode.ACTIVE));
299                             this.props.dispatch<any>(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
300                     }
301
302                 }}
303                 {...props} />
304
305             mainAppBarActions: MainAppBarActionProps = {
306                 onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
307                     this.props.dispatch<any>(setProjectItem(itemId, ItemMode.BOTH));
308                     this.props.dispatch<any>(loadDetails(itemId, ResourceKind.PROJECT));
309                 },
310                 onSearch: searchText => {
311                     this.setState({ searchText });
312                     this.props.dispatch(push(`/search?q=${searchText}`));
313                 },
314                 onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
315                 onDetailsPanelToggle: () => {
316                     this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
317                 },
318                 onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: NavBreadcrumb) => {
319                     this.openContextMenu(event, {
320                         uuid: breadcrumb.itemId,
321                         name: breadcrumb.label,
322                         kind: ContextMenuKind.PROJECT
323                     });
324                 }
325             };
326
327             toggleSidePanelOpen = (itemId: string) => {
328                 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
329             }
330
331             toggleSidePanelActive = (itemId: string) => {
332                 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
333                 this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
334                 const panelItem = this.props.sidePanelItems.find(it => it.id === itemId);
335                 if (panelItem && panelItem.activeAction) {
336                     panelItem.activeAction(this.props.dispatch);
337                 }
338             }
339
340             handleProjectCreationDialogOpen = (itemUuid: string) => {
341                 this.props.dispatch(projectActions.OPEN_PROJECT_CREATOR({ ownerUuid: itemUuid }));
342             }
343
344             handleCollectionCreationDialogOpen = (itemUuid: string) => {
345                 this.props.dispatch(collectionCreateActions.OPEN_COLLECTION_CREATOR({ ownerUuid: itemUuid }));
346             }
347
348             openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: { name: string; uuid: string; kind: ContextMenuKind; }) => {
349                 event.preventDefault();
350                 this.props.dispatch(
351                     contextMenuActions.OPEN_CONTEXT_MENU({
352                         position: { x: event.clientX, y: event.clientY },
353                         resource
354                     })
355                 );
356             }
357
358             toggleCurrentTokenModal = () => {
359                 this.setState({ isCurrentTokenDialogOpen: !this.state.isCurrentTokenDialogOpen });
360             }
361         }
362     )
363 );