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