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