Merge branch '13797-refactoring'
[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 { getTreePath } from '../../store/project/project-reducer';
22 import sidePanelActions from '../../store/side-panel/side-panel-action';
23 import SidePanel, { SidePanelItem } from '../../components/side-panel/side-panel';
24 import { ItemMode, setProjectItem } from "../../store/navigation/navigation-action";
25 import projectActions from "../../store/project/project-action";
26 import ProjectPanel from "../project-panel/project-panel";
27 import DetailsPanel from '../../views-components/details-panel/details-panel';
28 import { ArvadosTheme } from '../../common/custom-theme';
29 import ContextMenu, { ContextMenuKind } from "../../views-components/context-menu";
30 import CreateProjectDialog from "../../views-components/create-project-dialog/create-project-dialog";
31 import { authService } from '../../services/services';
32
33 import detailsPanelActions, { loadDetails } from "../../store/details-panel/details-panel-action";
34 import contextMenuActions from "../../store/context-menu/context-menu-actions";
35 import { SidePanelIdentifiers } from '../../store/side-panel/side-panel-reducer';
36 import { ProjectResource } from '../../models/project';
37 import { ResourceKind } from '../../models/resource';
38
39 interface WorkbenchDataProps {
40     projects: Array<TreeItem<ProjectResource>>;
41     currentProjectId: string;
42     user?: User;
43     sidePanelItems: SidePanelItem[];
44 }
45
46 interface WorkbenchActionProps {
47 }
48
49 type WorkbenchProps = WorkbenchDataProps & WorkbenchActionProps & DispatchProp & WithStyles<CssRules>;
50
51 interface NavBreadcrumb extends Breadcrumb {
52     itemId: string;
53 }
54
55 interface NavMenuItem extends MainAppBarMenuItem {
56     action: () => void;
57 }
58
59 interface WorkbenchState {
60     anchorEl: any;
61     searchText: string;
62     menuItems: {
63         accountMenu: NavMenuItem[],
64         helpMenu: NavMenuItem[],
65         anonymousMenu: NavMenuItem[]
66     };
67 }
68
69
70 class Workbench extends React.Component<WorkbenchProps, WorkbenchState> {
71     state = {
72         isCreationDialogOpen: false,
73         anchorEl: null,
74         searchText: "",
75         breadcrumbs: [],
76         menuItems: {
77             accountMenu: [
78                 {
79                     label: "Logout",
80                     action: () => this.props.dispatch(authActions.LOGOUT())
81                 },
82                 {
83                     label: "My account",
84                     action: () => this.props.dispatch(push("/my-account"))
85                 }
86             ],
87             helpMenu: [
88                 {
89                     label: "Help",
90                     action: () => this.props.dispatch(push("/help"))
91                 }
92             ],
93             anonymousMenu: [
94                 {
95                     label: "Sign in",
96                     action: () => this.props.dispatch(authActions.LOGIN())
97                 }
98             ]
99         }
100     };
101
102     render() {
103         const path = getTreePath(this.props.projects, this.props.currentProjectId);
104         const breadcrumbs = path.map(item => ({
105             label: item.data.name,
106             itemId: item.data.uuid,
107             status: item.status
108         }));
109
110         const { classes, user } = this.props;
111         return (
112             <div className={classes.root}>
113                 <div className={classes.appBar}>
114                     <MainAppBar
115                         breadcrumbs={breadcrumbs}
116                         searchText={this.state.searchText}
117                         user={this.props.user}
118                         menuItems={this.state.menuItems}
119                         {...this.mainAppBarActions} />
120                 </div>
121                 {user &&
122                     <Drawer
123                         variant="permanent"
124                         classes={{
125                             paper: classes.drawerPaper,
126                         }}>
127                         <div className={classes.toolbar} />
128                         <SidePanel
129                             toggleOpen={this.toggleSidePanelOpen}
130                             toggleActive={this.toggleSidePanelActive}
131                             sidePanelItems={this.props.sidePanelItems}
132                             onContextMenu={(event) => this.openContextMenu(event, authService.getUuid() || "", ContextMenuKind.RootProject)}>
133                             <ProjectTree
134                                 projects={this.props.projects}
135                                 toggleOpen={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.OPEN))}
136                                 onContextMenu={(event, item) => this.openContextMenu(event, item.data.uuid, ContextMenuKind.Project)}
137                                 toggleActive={itemId => {
138                                     this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE));
139                                     this.props.dispatch<any>(loadDetails(itemId, ResourceKind.Project));
140                                     this.props.dispatch<any>(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.Projects));
141                                 }} />
142                         </SidePanel>
143                     </Drawer>}
144                 <main className={classes.contentWrapper}>
145                     <div className={classes.content}>
146                         <Switch>
147                             <Route path="/projects/:id" render={this.renderProjectPanel} />
148                         </Switch>
149                     </div>
150                     { user && <DetailsPanel /> }
151                 </main>
152                 <ContextMenu />
153                 <CreateProjectDialog />
154             </div>
155         );
156     }
157
158     renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
159         onItemRouteChange={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE))}
160         onContextMenu={(event, item) => this.openContextMenu(event, item.uuid, ContextMenuKind.Project)}
161         onDialogOpen={this.handleCreationDialogOpen}
162         onItemClick={item => {
163             this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
164         }}
165         onItemDoubleClick={item => {
166             this.props.dispatch<any>(setProjectItem(item.uuid, ItemMode.ACTIVE));
167             this.props.dispatch<any>(loadDetails(item.uuid, ResourceKind.Project));
168         }}
169         {...props} />
170
171     mainAppBarActions: MainAppBarActionProps = {
172         onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
173             this.props.dispatch<any>(setProjectItem(itemId, ItemMode.BOTH));
174             this.props.dispatch<any>(loadDetails(itemId, ResourceKind.Project));
175         },
176         onSearch: searchText => {
177             this.setState({ searchText });
178             this.props.dispatch(push(`/search?q=${searchText}`));
179         },
180         onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
181         onDetailsPanelToggle: () => {
182             this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
183         },
184         onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: NavBreadcrumb) => {
185             this.openContextMenu(event, breadcrumb.itemId, ContextMenuKind.Project);
186         }
187     };
188
189     toggleSidePanelOpen = (itemId: string) => {
190         this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
191     }
192
193     toggleSidePanelActive = (itemId: string) => {
194         this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
195         this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
196         this.props.dispatch(push("/"));
197     }
198
199     handleCreationDialogOpen = (itemUuid: string) => {
200         this.props.dispatch(projectActions.OPEN_PROJECT_CREATOR({ ownerUuid: itemUuid }));
201     }
202
203     openContextMenu = (event: React.MouseEvent<HTMLElement>, itemUuid: string, kind: ContextMenuKind) => {
204         event.preventDefault();
205         this.props.dispatch(
206             contextMenuActions.OPEN_CONTEXT_MENU({
207                 position: { x: event.clientX, y: event.clientY },
208                 resource: { uuid: itemUuid, kind }
209             })
210         );
211     }
212
213
214 }
215
216 const drawerWidth = 240;
217 const appBarHeight = 100;
218
219 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
220
221 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
222     root: {
223         flexGrow: 1,
224         zIndex: 1,
225         overflow: 'hidden',
226         position: 'relative',
227         display: 'flex',
228         width: '100vw',
229         height: '100vh'
230     },
231     appBar: {
232         zIndex: theme.zIndex.drawer + 1,
233         position: "absolute",
234         width: "100%"
235     },
236     drawerPaper: {
237         position: 'relative',
238         width: drawerWidth,
239         display: 'flex',
240         flexDirection: 'column',
241     },
242     contentWrapper: {
243         backgroundColor: theme.palette.background.default,
244         display: "flex",
245         flexGrow: 1,
246         minWidth: 0,
247         paddingTop: appBarHeight
248     },
249     content: {
250         padding: `${theme.spacing.unit}px ${theme.spacing.unit * 3}px`,
251         overflowY: "auto",
252         flexGrow: 1
253     },
254     toolbar: theme.mixins.toolbar
255 });
256
257 export default connect<WorkbenchDataProps>(
258     (state: RootState) => ({
259         projects: state.projects.items,
260         currentProjectId: state.projects.currentItemId,
261         user: state.auth.user,
262         sidePanelItems: state.sidePanel
263     })
264 )(
265     withStyles(styles)(Workbench)
266 );