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