e848784591914bad4809e7e264bebe9dabe9990c
[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 } 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 sidePanelActions from '../../store/side-panel/side-panel-action';
24 import SidePanel, { SidePanelItem } from '../../components/side-panel/side-panel';
25 import { ItemMode, setProjectItem } from "../../store/navigation/navigation-action";
26 import projectActions from "../../store/project/project-action";
27 import ProjectPanel from "../project-panel/project-panel";
28 import DetailsPanel from '../../views-components/details-panel/details-panel';
29 import { ArvadosTheme } from '../../common/custom-theme';
30 import detailsPanelActions from "../../store/details-panel/details-panel-action";
31
32 const drawerWidth = 240;
33 const appBarHeight = 100;
34
35 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
36
37 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
38     root: {
39         flexGrow: 1,
40         zIndex: 1,
41         overflow: 'hidden',
42         position: 'relative',
43         display: 'flex',
44         width: '100vw',
45         height: '100vh'
46     },
47     appBar: {
48         zIndex: theme.zIndex.drawer + 1,
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}px ${theme.spacing.unit * 3}px`,
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
104 class Workbench extends React.Component<WorkbenchProps, WorkbenchState> {
105     state = {
106         anchorEl: null,
107         searchText: "",
108         breadcrumbs: [],
109         menuItems: {
110             accountMenu: [
111                 {
112                     label: "Logout",
113                     action: () => this.props.dispatch(authActions.LOGOUT())
114                 },
115                 {
116                     label: "My account",
117                     action: () => this.props.dispatch(push("/my-account"))
118                 }
119             ],
120             helpMenu: [
121                 {
122                     label: "Help",
123                     action: () => this.props.dispatch(push("/help"))
124                 }
125             ],
126             anonymousMenu: [
127                 {
128                     label: "Sign in",
129                     action: () => this.props.dispatch(authActions.LOGIN())
130                 }
131             ]
132         }
133     };
134
135     mainAppBarActions: MainAppBarActionProps = {
136         onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
137             this.props.dispatch<any>(setProjectItem(itemId, ItemMode.BOTH));
138         },
139         onSearch: searchText => {
140             this.setState({ searchText });
141             this.props.dispatch(push(`/search?q=${searchText}`));
142         },
143         onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
144         onDetailsPanelToggle: () => {
145             this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
146         }
147     };
148
149     toggleSidePanelOpen = (itemId: string) => {
150         this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
151     }
152
153     toggleSidePanelActive = (itemId: string) => {
154         this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
155         this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
156     }
157
158     render() {
159         const path = getTreePath(this.props.projects, this.props.currentProjectId);
160         const breadcrumbs = path.map(item => ({
161             label: item.data.name,
162             itemId: item.data.uuid,
163             status: item.status
164         }));
165
166         const { classes, user } = this.props;
167         return (
168             <div className={classes.root}>
169                 <div className={classes.appBar}>
170                     <MainAppBar
171                         breadcrumbs={breadcrumbs}
172                         searchText={this.state.searchText}
173                         user={this.props.user}
174                         menuItems={this.state.menuItems}
175                         {...this.mainAppBarActions}
176                     />
177                 </div>
178                 {user &&
179                     <Drawer
180                         variant="permanent"
181                         classes={{
182                             paper: classes.drawerPaper,
183                         }}>
184                         <div className={classes.toolbar} />
185                         <SidePanel
186                             toggleOpen={this.toggleSidePanelOpen}
187                             toggleActive={this.toggleSidePanelActive}
188                             sidePanelItems={this.props.sidePanelItems}>
189                             <ProjectTree
190                                 projects={this.props.projects}
191                                 toggleOpen={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.OPEN))}
192                                 toggleActive={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE))}
193                             />
194                         </SidePanel>
195                     </Drawer>}
196                 <main className={classes.contentWrapper}>
197                     <div className={classes.content}>
198                         <Switch>
199                             <Route path="/projects/:id" render={this.renderProjectPanel} />
200                         </Switch>
201                     </div>
202                     <DetailsPanel/>
203                 </main>
204             </div>
205         );
206     }
207
208     renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
209         onItemRouteChange={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE))}
210         onItemClick={item => this.props.dispatch<any>(setProjectItem(item.uuid, ItemMode.ACTIVE))}
211         {...props} />
212
213 }
214
215 export default connect<WorkbenchDataProps>(
216     (state: RootState) => ({
217         projects: state.projects.items,
218         currentProjectId: state.projects.currentItemId,
219         user: state.auth.user,
220         sidePanelItems: state.sidePanel
221     })
222 )(
223     withStyles(styles)(Workbench)
224 );