41725b53ef427aa613e657d37d5f6d88cb024738
[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 '../../views-components/main-app-bar/main-app-bar';
15 import { Breadcrumb } from '../../components/breadcrumbs/breadcrumbs';
16 import { push } from 'react-router-redux';
17 import projectActions, { getProjectList } from "../../store/project/project-action";
18 import ProjectTree from '../../views-components/project-tree/project-tree';
19 import { TreeItem, TreeItemStatus } from "../../components/tree/tree";
20 import { Project } from "../../models/project";
21 import { getTreePath } from '../../store/project/project-reducer';
22 import DataExplorer from '../data-explorer/data-explorer';
23
24 const drawerWidth = 240;
25 const appBarHeight = 102;
26
27 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
28
29 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
30     root: {
31         flexGrow: 1,
32         zIndex: 1,
33         overflow: 'hidden',
34         position: 'relative',
35         display: 'flex',
36         width: '100vw',
37         height: '100vh'
38     },
39     appBar: {
40         zIndex: theme.zIndex.drawer + 1,
41         backgroundColor: '#692498',
42         position: "absolute",
43         width: "100%"
44     },
45     drawerPaper: {
46         position: 'relative',
47         width: drawerWidth,
48     },
49     contentWrapper: {
50         backgroundColor: theme.palette.background.default,
51         display: "flex",
52         flexGrow: 1,
53         minWidth: 0,
54         paddingTop: appBarHeight
55     },
56     content: {
57         padding: theme.spacing.unit * 3,
58         overflowY: "auto",
59         flexGrow: 1
60     },
61     toolbar: theme.mixins.toolbar
62 });
63
64 interface WorkbenchDataProps {
65     projects: Array<TreeItem<Project>>;
66     user?: User;
67 }
68
69 interface WorkbenchActionProps {
70 }
71
72 type WorkbenchProps = WorkbenchDataProps & WorkbenchActionProps & DispatchProp & WithStyles<CssRules>;
73
74 interface NavBreadcrumb extends Breadcrumb {
75     itemId: string;
76     status: TreeItemStatus;
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         menuItems: {
100             accountMenu: [
101                 {
102                     label: "Logout",
103                     action: () => this.props.dispatch(authActions.LOGOUT())
104                 },
105                 {
106                     label: "My account",
107                     action: () => this.props.dispatch(push("/my-account"))
108                 }
109             ],
110             helpMenu: [
111                 {
112                     label: "Help",
113                     action: () => this.props.dispatch(push("/help"))
114                 }
115             ],
116             anonymousMenu: [
117                 {
118                     label: "Sign in",
119                     action: () => this.props.dispatch(authActions.LOGIN())
120                 }
121             ]
122         }
123     };
124
125
126     mainAppBarActions: MainAppBarActionProps = {
127         onBreadcrumbClick: ({ itemId, status }: NavBreadcrumb) => {
128             this.toggleProjectTreeItem(itemId, status);
129         },
130         onSearch: searchText => {
131             this.setState({ searchText });
132             this.props.dispatch(push(`/search?q=${searchText}`));
133         },
134         onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action()
135     };
136
137     toggleProjectTreeItem = (itemId: string, status: TreeItemStatus) => {
138         if (status === TreeItemStatus.Loaded) {
139             this.openProjectItem(itemId);
140         } else {
141             this.props.dispatch<any>(getProjectList(itemId))
142                 .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 );