Merge branch 'master' into 13590-main-app-bar-component-1
[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
7 import { StyleRulesCallback, Theme, WithStyles, withStyles } from '@material-ui/core/styles';
8 import Drawer from '@material-ui/core/Drawer';
9 import AppBar from '@material-ui/core/AppBar';
10 import Toolbar from '@material-ui/core/Toolbar';
11 import Typography from '@material-ui/core/Typography';
12 import { connect, DispatchProp } from "react-redux";
13 import Tree from "../../components/tree/tree";
14 import { Project } from "../../models/project";
15 import ProjectList from "../../components/project-list/project-list";
16 import { Route, Switch } from "react-router";
17 import { Link } from "react-router-dom";
18 import Button from "@material-ui/core/Button/Button";
19 import authActions from "../../store/auth/auth-action";
20 import IconButton from "@material-ui/core/IconButton/IconButton";
21 import Menu from "@material-ui/core/Menu/Menu";
22 import MenuItem from "@material-ui/core/MenuItem/MenuItem";
23 import { AccountCircle } from "@material-ui/icons";
24 import { User } from "../../models/user";
25 import Grid from "@material-ui/core/Grid/Grid";
26 import { RootState } from "../../store/store";
27 import MainAppBar, { MainAppBarActionProps, MainAppBarMenuItems, MainAppBarMenuItem } from '../../components/main-app-bar/main-app-bar';
28 import { Breadcrumb } from '../../components/breadcrumbs/breadcrumbs';
29 import { push } from 'react-router-redux';
30
31 const drawerWidth = 240;
32
33 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'toolbar';
34
35 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
36     root: {
37         flexGrow: 1,
38         zIndex: 1,
39         overflow: 'hidden',
40         position: 'relative',
41         display: 'flex',
42         width: '100vw',
43         height: '100vh'
44     },
45     appBar: {
46         zIndex: theme.zIndex.drawer + 1,
47         backgroundColor: '#692498',
48         position: "absolute",
49         width: "100%"
50     },
51     drawerPaper: {
52         position: 'relative',
53         width: drawerWidth,
54     },
55     content: {
56         flexGrow: 1,
57         backgroundColor: theme.palette.background.default,
58         padding: theme.spacing.unit * 3,
59         height: '100%',
60         minWidth: 0,
61     },
62     toolbar: theme.mixins.toolbar
63 });
64
65 interface WorkbenchDataProps {
66     projects: Project[];
67     user?: User;
68 }
69
70 interface WorkbenchActionProps {
71 }
72
73 type WorkbenchProps = WorkbenchDataProps & WorkbenchActionProps & DispatchProp & WithStyles<CssRules>;
74
75 interface NavBreadcrumb extends Breadcrumb {
76     path: string
77 }
78
79 interface NavMenuItem extends MainAppBarMenuItem {
80     action: () => void
81 }
82
83 interface WorkbenchState {
84     anchorEl: any;
85     breadcrumbs: NavBreadcrumb[];
86     searchText: string;
87     menuItems: {
88         accountMenu: NavMenuItem[],
89         helpMenu: NavMenuItem[],
90         anonymousMenu: NavMenuItem[]
91     };
92 }
93
94 class Workbench extends React.Component<WorkbenchProps, WorkbenchState> {
95     state = {
96         anchorEl: null,
97         searchText: "",
98         breadcrumbs: [
99             {
100                 label: "Projects",
101                 path: "/projects"
102             }, {
103                 label: "Project 1",
104                 path: "/projects/project-1"
105             }
106         ],
107         menuItems: {
108             accountMenu: [
109                 {
110                     label: "Logout",
111                     action: () => this.props.dispatch(authActions.LOGOUT())
112                 },
113                 {
114                     label: "My account",
115                     action: () => this.props.dispatch(push("/my-account"))
116                 }
117             ],
118             helpMenu: [
119                 {
120                     label: "Help",
121                     action: () => this.props.dispatch(push("/help"))
122                 }
123             ],
124             anonymousMenu: [
125                 {
126                     label: "Sign in",
127                     action: () => this.props.dispatch(authActions.LOGIN())
128                 }
129             ]
130         }
131     }
132
133
134     mainAppBarActions: MainAppBarActionProps = {
135         onBreadcrumbClick: (breadcrumb: NavBreadcrumb) => this.props.dispatch(push(breadcrumb.path)),
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     render() {
144         const { classes, user } = this.props;
145         return (
146             <div className={classes.root}>
147                 <div className={classes.appBar}>
148                     <MainAppBar
149                         breadcrumbs={this.state.breadcrumbs}
150                         searchText={this.state.searchText}
151                         user={this.props.user}
152                         menuItems={this.state.menuItems}
153                         {...this.mainAppBarActions}
154                     />
155                 </div>
156                 {user &&
157                     <Drawer
158                         variant="permanent"
159                         classes={{
160                             paper: classes.drawerPaper,
161                         }}>
162                         <div className={classes.toolbar} />
163                         <div className={classes.toolbar} />
164                         <Tree items={this.props.projects} render={(p: Project) =>
165                             <Link to={`/project/${p.name}`}>{p.name}</Link>
166                         } />
167                     </Drawer>}
168                 <main className={classes.content}>
169                     <div className={classes.toolbar} />
170                     <div className={classes.toolbar} />
171                     <Switch>
172                         <Route path="/project/:name" component={ProjectList} />
173                     </Switch>
174                 </main>
175             </div>
176         );
177     }
178 }
179
180 export default connect<WorkbenchDataProps>(
181     (state: RootState) => ({
182         projects: state.projects,
183         user: state.auth.user
184     })
185 )(
186     withStyles(styles)(Workbench)
187 );