Rename findTreeBranch to getTreePath
[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
27 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | '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     content: {
50         flexGrow: 1,
51         backgroundColor: theme.palette.background.default,
52         padding: theme.spacing.unit * 3,
53         height: '100%',
54         minWidth: 0,
55     },
56     toolbar: theme.mixins.toolbar
57 });
58
59 interface WorkbenchDataProps {
60     projects: Array<TreeItem<Project>>;
61     user?: User;
62 }
63
64 interface WorkbenchActionProps {
65 }
66
67 type WorkbenchProps = WorkbenchDataProps & WorkbenchActionProps & DispatchProp & WithStyles<CssRules>;
68
69 interface NavBreadcrumb extends Breadcrumb {
70     itemId: string;
71     status: TreeItemStatus;
72 }
73
74 interface NavMenuItem extends MainAppBarMenuItem {
75     action: () => void;
76 }
77
78 interface WorkbenchState {
79     anchorEl: any;
80     breadcrumbs: NavBreadcrumb[];
81     searchText: string;
82     menuItems: {
83         accountMenu: NavMenuItem[],
84         helpMenu: NavMenuItem[],
85         anonymousMenu: NavMenuItem[]
86     };
87 }
88
89 class Workbench extends React.Component<WorkbenchProps, WorkbenchState> {
90     state = {
91         anchorEl: null,
92         searchText: "",
93         breadcrumbs: [],
94         menuItems: {
95             accountMenu: [
96                 {
97                     label: "Logout",
98                     action: () => this.props.dispatch(authActions.LOGOUT())
99                 },
100                 {
101                     label: "My account",
102                     action: () => this.props.dispatch(push("/my-account"))
103                 }
104             ],
105             helpMenu: [
106                 {
107                     label: "Help",
108                     action: () => this.props.dispatch(push("/help"))
109                 }
110             ],
111             anonymousMenu: [
112                 {
113                     label: "Sign in",
114                     action: () => this.props.dispatch(authActions.LOGIN())
115                 }
116             ]
117         }
118     };
119
120
121     mainAppBarActions: MainAppBarActionProps = {
122         onBreadcrumbClick: ({ itemId, status }: NavBreadcrumb) => {
123             this.toggleProjectTreeItem(itemId, status);
124         },
125         onSearch: searchText => {
126             this.setState({ searchText });
127             this.props.dispatch(push(`/search?q=${searchText}`));
128         },
129         onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action()
130     };
131
132     toggleProjectTreeItem = (itemId: string, status: TreeItemStatus) => {
133         if (status === TreeItemStatus.Loaded) {
134             this.openProjectItem(itemId);
135         } else {
136             this.props.dispatch<any>(projectService.getProjectList(itemId)).then(() => this.openProjectItem(itemId));
137         }
138     }
139
140     openProjectItem = (itemId: string) => {
141         const branch = getTreePath(this.props.projects, itemId);
142         this.setState({
143             breadcrumbs: branch.map(item => ({
144                 label: item.data.name,
145                 itemId: item.data.uuid,
146                 status: item.status
147             }))
148         });
149         this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM(itemId));
150         this.props.dispatch(push(`/project/${itemId}`));
151     }
152
153     render() {
154         const { classes, user } = this.props;
155         return (
156             <div className={classes.root}>
157                 <div className={classes.appBar}>
158                     <MainAppBar
159                         breadcrumbs={this.state.breadcrumbs}
160                         searchText={this.state.searchText}
161                         user={this.props.user}
162                         menuItems={this.state.menuItems}
163                         {...this.mainAppBarActions}
164                     />
165                 </div>
166                 {user &&
167                     <Drawer
168                         variant="permanent"
169                         classes={{
170                             paper: classes.drawerPaper,
171                         }}>
172                         <div className={classes.toolbar} />
173                         <ProjectTree
174                             projects={this.props.projects}
175                             toggleProjectTreeItem={this.toggleProjectTreeItem} />
176                     </Drawer>}
177                 <main className={classes.content}>
178                     <div className={classes.toolbar} />
179                     <div className={classes.toolbar} />
180                     <Switch>
181                         <Route path="/project/:name" component={DataExplorer} />
182                     </Switch>
183                 </main>
184             </div>
185         );
186     }
187 }
188
189 export default connect<WorkbenchDataProps>(
190     (state: RootState) => ({
191         projects: state.projects,
192         user: state.auth.user
193     })
194 )(
195     withStyles(styles)(Workbench)
196 );