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