Add toolbar with project explorer actions
[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, Theme, 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 } from "react-router";
10 import authActions from "../../store/auth/auth-action";
11 import dataExplorerActions from "../../store/data-explorer/data-explorer-action";
12 import { User } from "../../models/user";
13 import { RootState } from "../../store/store";
14 import MainAppBar, { MainAppBarActionProps, MainAppBarMenuItem } from '../../views-components/main-app-bar/main-app-bar';
15 import { Breadcrumb } from '../../components/breadcrumbs/breadcrumbs';
16 import { push } from 'react-router-redux';
17 import projectActions, { getProjectList } from "../../store/project/project-action";
18 import ProjectTree from '../../views-components/project-tree/project-tree';
19 import { TreeItem, TreeItemStatus } from "../../components/tree/tree";
20 import { Project } from "../../models/project";
21 import { getTreePath, findTreeItem } from '../../store/project/project-reducer';
22 import ProjectExplorer, { PROJECT_EXPLORER_ID } from '../../views-components/project-explorer/project-explorer';
23 import { ProjectExplorerItem, mapProjectTreeItem } from '../../views-components/project-explorer/project-explorer-item';
24 import sidePanelActions from '../../store/side-panel/side-panel-action';
25 import SidePanel, { SidePanelItem } from '../../components/side-panel/side-panel';
26
27 const drawerWidth = 240;
28 const appBarHeight = 102;
29
30 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
31
32 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
33     root: {
34         flexGrow: 1,
35         zIndex: 1,
36         overflow: 'hidden',
37         position: 'relative',
38         display: 'flex',
39         width: '100vw',
40         height: '100vh'
41     },
42     appBar: {
43         zIndex: theme.zIndex.drawer + 1,
44         backgroundColor: '#692498',
45         position: "absolute",
46         width: "100%"
47     },
48     drawerPaper: {
49         position: 'relative',
50         width: drawerWidth,
51         display: 'flex',
52         flexDirection: 'column',
53     },
54     contentWrapper: {
55         backgroundColor: theme.palette.background.default,
56         display: "flex",
57         flexGrow: 1,
58         minWidth: 0,
59         paddingTop: appBarHeight
60     },
61     content: {
62         padding: theme.spacing.unit * 3,
63         overflowY: "auto",
64         flexGrow: 1
65     },
66     toolbar: theme.mixins.toolbar
67 });
68
69 interface WorkbenchDataProps {
70     projects: Array<TreeItem<Project>>;
71     user?: User;
72     sidePanelItems: SidePanelItem[];
73 }
74
75 interface WorkbenchActionProps {
76 }
77
78 type WorkbenchProps = WorkbenchDataProps & WorkbenchActionProps & DispatchProp & WithStyles<CssRules>;
79
80 interface NavBreadcrumb extends Breadcrumb {
81     itemId: string;
82     status: TreeItemStatus;
83 }
84
85 interface NavMenuItem extends MainAppBarMenuItem {
86     action: () => void;
87 }
88
89 interface WorkbenchState {
90     anchorEl: any;
91     breadcrumbs: NavBreadcrumb[];
92     searchText: string;
93     menuItems: {
94         accountMenu: NavMenuItem[],
95         helpMenu: NavMenuItem[],
96         anonymousMenu: NavMenuItem[]
97     };
98 }
99
100 class Workbench extends React.Component<WorkbenchProps, WorkbenchState> {
101     state = {
102         anchorEl: null,
103         searchText: "",
104         breadcrumbs: [],
105         menuItems: {
106             accountMenu: [
107                 {
108                     label: "Logout",
109                     action: () => this.props.dispatch(authActions.LOGOUT())
110                 },
111                 {
112                     label: "My account",
113                     action: () => this.props.dispatch(push("/my-account"))
114                 }
115             ],
116             helpMenu: [
117                 {
118                     label: "Help",
119                     action: () => this.props.dispatch(push("/help"))
120                 }
121             ],
122             anonymousMenu: [
123                 {
124                     label: "Sign in",
125                     action: () => this.props.dispatch(authActions.LOGIN())
126                 }
127             ]
128         }
129     };
130
131
132     mainAppBarActions: MainAppBarActionProps = {
133         onBreadcrumbClick: ({ itemId, status }: NavBreadcrumb) => {
134             this.toggleProjectTreeItemOpen(itemId, status);
135         },
136         onSearch: searchText => {
137             this.setState({ searchText });
138             this.props.dispatch(push(`/search?q=${searchText}`));
139         },
140         onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action()
141     };
142
143     toggleProjectTreeItemOpen = (itemId: string, status: TreeItemStatus) => {
144         if (status === TreeItemStatus.Loaded) {
145             this.openProjectItem(itemId);
146             this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(itemId));
147             this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(itemId));
148         } else {
149             this.props.dispatch<any>(getProjectList(itemId))
150                 .then(() => {
151                     this.openProjectItem(itemId);
152                     this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(itemId));
153                     this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(itemId));
154                 });
155         }
156     }
157
158     toggleProjectTreeItemActive = (itemId: string, status: TreeItemStatus) => {
159         if (status === TreeItemStatus.Loaded) {
160             this.openProjectItem(itemId);
161             this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(itemId));
162             this.props.dispatch(sidePanelActions.RESET_SIDE_PANEL_ACTIVITY(itemId));
163         } else {
164             this.props.dispatch<any>(getProjectList(itemId))
165                 .then(() => {
166                     this.openProjectItem(itemId);
167                     this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(itemId));
168                     this.props.dispatch(sidePanelActions.RESET_SIDE_PANEL_ACTIVITY(itemId));
169                 });
170         }
171     }
172
173     toggleSidePanelOpen = (itemId: string) => {
174         this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
175     }
176
177     toggleSidePanelActive = (itemId: string) => {
178         this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
179         this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
180     }
181
182     openProjectItem = (itemId: string) => {
183         const branch = getTreePath(this.props.projects, itemId);
184         this.setState({
185             breadcrumbs: branch.map(item => ({
186                 label: item.data.name,
187                 itemId: item.data.uuid,
188                 status: item.status
189             }))
190         });
191         this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(itemId));
192         this.props.dispatch(push(`/project/${itemId}`));
193
194         const project = findTreeItem(this.props.projects, itemId);
195         const items: ProjectExplorerItem[] = project && project.items
196             ? project.items.map(mapProjectTreeItem)
197             : [];
198         this.props.dispatch(dataExplorerActions.SET_ITEMS({ id: PROJECT_EXPLORER_ID, items }));
199     }
200
201     render() {
202         const { classes, user, projects, sidePanelItems } = this.props;
203         return (
204             <div className={classes.root}>
205                 <div className={classes.appBar}>
206                     <MainAppBar
207                         breadcrumbs={this.state.breadcrumbs}
208                         searchText={this.state.searchText}
209                         user={this.props.user}
210                         menuItems={this.state.menuItems}
211                         {...this.mainAppBarActions}
212                     />
213                 </div>
214                 {user &&
215                     <Drawer
216                         variant="permanent"
217                         classes={{
218                             paper: classes.drawerPaper,
219                         }}>
220                         <div className={classes.toolbar} />
221                         <SidePanel
222                             toggleOpen={this.toggleSidePanelOpen}
223                             toggleActive={this.toggleSidePanelActive}
224                             sidePanelItems={sidePanelItems}>
225                             <ProjectTree
226                                 projects={projects}
227                                 toggleOpen={this.toggleProjectTreeItemOpen}
228                                 toggleActive={this.toggleProjectTreeItemActive} />
229                         </SidePanel>
230                     </Drawer>}
231                 <main className={classes.contentWrapper}>
232                     <div className={classes.content}>
233                         <Switch>
234                             <Route path="/project/:name" component={ProjectExplorer} />
235                         </Switch>
236                     </div>
237                 </main>
238             </div>
239         );
240     }
241 }
242
243 export default connect<WorkbenchDataProps>(
244     (state: RootState) => ({
245         projects: state.projects,
246         user: state.auth.user,
247         sidePanelItems: state.sidePanel,
248     })
249 )(
250     withStyles(styles)(Workbench)
251 );