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