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