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