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