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