Replace go back item with browser back button support
[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 import { StyleRulesCallback, Theme, WithStyles, withStyles } from '@material-ui/core/styles';
7 import Drawer from '@material-ui/core/Drawer';
8 import { connect, DispatchProp } from "react-redux";
9 import { Route, Switch, RouteComponentProps } from "react-router";
10 import authActions from "../../store/auth/auth-action";
11 import dataExplorerActions from "../../store/data-explorer/data-explorer-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, findTreeItem } from '../../store/project/project-reducer';
24 import sidePanelActions from '../../store/side-panel/side-panel-action';
25 import SidePanel, { SidePanelItem } from '../../components/side-panel/side-panel';
26 import { ResourceKind } from "../../models/resource";
27 import { ItemMode, setProjectItem } from "../../store/navigation/navigation-action";
28 import projectActions from "../../store/project/project-action";
29 import ProjectPanel from "../project-panel/project-panel";
30
31 const drawerWidth = 240;
32 const appBarHeight = 102;
33
34 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
35
36 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
37     root: {
38         flexGrow: 1,
39         zIndex: 1,
40         overflow: 'hidden',
41         position: 'relative',
42         display: 'flex',
43         width: '100vw',
44         height: '100vh'
45     },
46     appBar: {
47         zIndex: theme.zIndex.drawer + 1,
48         backgroundColor: '#692498',
49         position: "absolute",
50         width: "100%"
51     },
52     drawerPaper: {
53         position: 'relative',
54         width: drawerWidth,
55         display: 'flex',
56         flexDirection: 'column',
57     },
58     contentWrapper: {
59         backgroundColor: theme.palette.background.default,
60         display: "flex",
61         flexGrow: 1,
62         minWidth: 0,
63         paddingTop: appBarHeight
64     },
65     content: {
66         padding: theme.spacing.unit * 3,
67         overflowY: "auto",
68         flexGrow: 1
69     },
70     toolbar: theme.mixins.toolbar
71 });
72
73 interface WorkbenchDataProps {
74     projects: Array<TreeItem<Project>>;
75     currentProjectId: string;
76     user?: User;
77     sidePanelItems: SidePanelItem[];
78 }
79
80 interface WorkbenchActionProps {
81 }
82
83 type WorkbenchProps = WorkbenchDataProps & WorkbenchActionProps & DispatchProp & WithStyles<CssRules>;
84
85 interface NavBreadcrumb extends Breadcrumb {
86     itemId: string;
87 }
88
89 interface NavMenuItem extends MainAppBarMenuItem {
90     action: () => void;
91 }
92
93 interface WorkbenchState {
94     anchorEl: any;
95     searchText: string;
96     menuItems: {
97         accountMenu: NavMenuItem[],
98         helpMenu: NavMenuItem[],
99         anonymousMenu: NavMenuItem[]
100     };
101 }
102
103 class Workbench extends React.Component<WorkbenchProps, WorkbenchState> {
104     state = {
105         anchorEl: null,
106         searchText: "",
107         breadcrumbs: [],
108         menuItems: {
109             accountMenu: [
110                 {
111                     label: "Logout",
112                     action: () => this.props.dispatch(authActions.LOGOUT())
113                 },
114                 {
115                     label: "My account",
116                     action: () => this.props.dispatch(push("/my-account"))
117                 }
118             ],
119             helpMenu: [
120                 {
121                     label: "Help",
122                     action: () => this.props.dispatch(push("/help"))
123                 }
124             ],
125             anonymousMenu: [
126                 {
127                     label: "Sign in",
128                     action: () => this.props.dispatch(authActions.LOGIN())
129                 }
130             ]
131         }
132     };
133
134     mainAppBarActions: MainAppBarActionProps = {
135         onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
136             this.props.dispatch<any>(setProjectItem(itemId, ItemMode.BOTH));
137         },
138         onSearch: searchText => {
139             this.setState({ searchText });
140             this.props.dispatch(push(`/search?q=${searchText}`));
141         },
142         onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action()
143     };
144
145     toggleSidePanelOpen = (itemId: string) => {
146         this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
147     }
148
149     toggleSidePanelActive = (itemId: string) => {
150         this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
151         this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
152     }
153
154     render() {
155         const path = getTreePath(this.props.projects, this.props.currentProjectId);
156         const breadcrumbs = path.map(item => ({
157             label: item.data.name,
158             itemId: item.data.uuid,
159             status: item.status
160         }));  
161
162         const { classes, user } = this.props;
163         return (
164             <div className={classes.root}>
165                 <div className={classes.appBar}>
166                     <MainAppBar
167                         breadcrumbs={breadcrumbs}
168                         searchText={this.state.searchText}
169                         user={this.props.user}
170                         menuItems={this.state.menuItems}
171                         {...this.mainAppBarActions}
172                     />
173                 </div>
174                 {user &&
175                     <Drawer
176                         variant="permanent"
177                         classes={{
178                             paper: classes.drawerPaper,
179                         }}>
180                         <div className={classes.toolbar} />
181                         <SidePanel
182                             toggleOpen={this.toggleSidePanelOpen}
183                             toggleActive={this.toggleSidePanelActive}
184                             sidePanelItems={this.props.sidePanelItems}>
185                             <ProjectTree
186                                 projects={this.props.projects}
187                                 toggleOpen={itemId =>
188                                     this.props.dispatch<any>(
189                                         setProjectItem(itemId, ItemMode.OPEN)
190                                     )}
191                                 toggleActive={itemId =>
192                                     this.props.dispatch<any>(
193                                         setProjectItem(itemId, ItemMode.ACTIVE)
194                                     )}
195                             />
196                         </SidePanel>
197                     </Drawer>}
198                 <main className={classes.contentWrapper}>
199                     <div className={classes.content}>
200                         <Switch>
201                             <Route path="/projects/:id" render={this.renderProjectPanel} />
202                         </Switch>
203                     </div>
204                 </main>
205             </div>
206         );
207     }
208
209     renderProjectPanel = (props: RouteComponentProps<any>) =>
210         <ProjectPanel
211             onItemOpen={itemId => this.props.dispatch<any>(
212                 setProjectItem(itemId, ItemMode.ACTIVE)
213             )}
214             {...props} />
215 }
216
217 export default connect<WorkbenchDataProps>(
218     (state: RootState) => ({
219         projects: state.projects.items,
220         currentProjectId: state.projects.currentItemId,
221         user: state.auth.user,
222         sidePanelItems: state.sidePanel
223     })
224 )(
225     withStyles(styles)(Workbench)
226 );