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