refs #13901 Merge branch 'origin/13901-services-repo'
[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 { 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";
28
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 { dataExplorerActions } from '../../store/data-explorer/data-explorer-action';
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 } from '../../store/collection-panel/collection-panel-action';
43 import { getCollectionUrl } from '../../models/collection';
44 import { AuthService } from "../../services/auth-service/auth-service";
45
46 const DRAWER_WITDH = 240;
47 const APP_BAR_HEIGHT = 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: DRAWER_WITDH,
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: APP_BAR_HEIGHT
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 WorkbenchServiceProps {
96     authService: AuthService;
97 }
98
99 interface WorkbenchActionProps {
100 }
101
102 type WorkbenchProps = WorkbenchDataProps & WorkbenchServiceProps & WorkbenchActionProps & DispatchProp<any> & WithStyles<CssRules>;
103
104 interface NavBreadcrumb extends Breadcrumb {
105     itemId: string;
106 }
107
108 interface NavMenuItem extends MainAppBarMenuItem {
109     action: () => void;
110 }
111
112 interface WorkbenchState {
113     isCurrentTokenDialogOpen: boolean;
114     anchorEl: any;
115     searchText: string;
116     menuItems: {
117         accountMenu: NavMenuItem[],
118         helpMenu: NavMenuItem[],
119         anonymousMenu: NavMenuItem[]
120     };
121 }
122
123 export const Workbench = withStyles(styles)(
124     connect<WorkbenchDataProps>(
125         (state: RootState) => ({
126             projects: state.projects.items,
127             currentProjectId: state.projects.currentItemId,
128             user: state.auth.user,
129             currentToken: state.auth.apiToken,
130             sidePanelItems: state.sidePanel
131         })
132     )(
133         class extends React.Component<WorkbenchProps, WorkbenchState> {
134             state = {
135                 isCreationDialogOpen: false,
136                 isCurrentTokenDialogOpen: false,
137                 anchorEl: null,
138                 searchText: "",
139                 breadcrumbs: [],
140                 menuItems: {
141                     accountMenu: [
142                         {
143                             label: 'Current token',
144                             action: () => this.toggleCurrentTokenModal()
145                         },
146                         {
147                             label: "Logout",
148                             action: () => this.props.dispatch(logout())
149                         },
150                         {
151                             label: "My account",
152                             action: () => this.props.dispatch(push("/my-account"))
153                         }
154                     ],
155                     helpMenu: [
156                         {
157                             label: "Help",
158                             action: () => this.props.dispatch(push("/help"))
159                         }
160                     ],
161                     anonymousMenu: [
162                         {
163                             label: "Sign in",
164                             action: () => this.props.dispatch(login())
165                         }
166                     ]
167                 }
168             };
169
170             render() {
171                 const path = getTreePath(this.props.projects, this.props.currentProjectId);
172                 const breadcrumbs = path.map(item => ({
173                     label: item.data.name,
174                     itemId: item.data.uuid,
175                     status: item.status
176                 }));
177
178                 const { classes, user } = this.props;
179                 return (
180                     <div className={classes.root}>
181                         <div className={classes.appBar}>
182                             <MainAppBar
183                                 breadcrumbs={breadcrumbs}
184                                 searchText={this.state.searchText}
185                                 user={this.props.user}
186                                 menuItems={this.state.menuItems}
187                                 {...this.mainAppBarActions} />
188                         </div>
189                         {user &&
190                             <Drawer
191                                 variant="permanent"
192                                 classes={{
193                                     paper: classes.drawerPaper,
194                                 }}>
195                                 <div className={classes.toolbar} />
196                                 <SidePanel
197                                     toggleOpen={this.toggleSidePanelOpen}
198                                     toggleActive={this.toggleSidePanelActive}
199                                     sidePanelItems={this.props.sidePanelItems}
200                                     onContextMenu={(event) => this.openContextMenu(event, {
201                                         uuid: this.props.authService.getUuid() || "",
202                                         name: "",
203                                         kind: ContextMenuKind.ROOT_PROJECT
204                                     })}>
205                                     <ProjectTree
206                                         projects={this.props.projects}
207                                         toggleOpen={itemId => this.props.dispatch(setProjectItem(itemId, ItemMode.OPEN))}
208                                         onContextMenu={(event, item) => this.openContextMenu(event, {
209                                             uuid: item.data.uuid,
210                                             name: item.data.name,
211                                             kind: ContextMenuKind.PROJECT
212                                         })}
213                                         toggleActive={itemId => {
214                                             this.props.dispatch(setProjectItem(itemId, ItemMode.ACTIVE));
215                                             this.props.dispatch(loadDetails(itemId, ResourceKind.PROJECT));
216                                             this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
217                                         }} />
218                                 </SidePanel>
219                             </Drawer>}
220                         <main className={classes.contentWrapper}>
221                             <div className={classes.content}>
222                                 <Switch>
223                                     <Route path="/projects/:id" render={this.renderProjectPanel} />
224                                     <Route path="/favorites" render={this.renderFavoritePanel} />
225                                     <Route path="/collections/:id" render={this.renderCollectionPanel} />
226                                 </Switch>
227                             </div>
228                             {user && <DetailsPanel />}
229                         </main>
230                         <ContextMenu />
231                         <Snackbar />
232                         <CreateProjectDialog />
233                         <CreateCollectionDialog />
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(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(loadDetails(item.uuid, item.kind as ResourceKind));
268                 }}
269                 onItemDoubleClick={item => {
270                     switch (item.kind) {
271                         case ResourceKind.COLLECTION:
272                             this.props.dispatch(loadCollection(item.uuid, item.kind as ResourceKind));
273                             this.props.dispatch(push(getCollectionUrl(item.uuid)));
274                         default: 
275                             this.props.dispatch(setProjectItem(item.uuid, ItemMode.ACTIVE));
276                             this.props.dispatch(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(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(loadDetails(item.uuid, item.kind as ResourceKind));
295                 }}
296                 onItemDoubleClick={item => {
297                     switch (item.kind) {
298                         case ResourceKind.COLLECTION:
299                             this.props.dispatch(loadCollection(item.uuid, item.kind as ResourceKind));
300                             this.props.dispatch(push(getCollectionUrl(item.uuid)));
301                         default:
302                             this.props.dispatch(loadDetails(item.uuid, ResourceKind.PROJECT));
303                             this.props.dispatch(setProjectItem(item.uuid, ItemMode.ACTIVE));
304                             this.props.dispatch(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(setProjectItem(itemId, ItemMode.BOTH));
313                     this.props.dispatch(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 );