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