Merge remote-tracking branch 'origin' into 13694-Data-operations-Project-creation
[arvados.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, 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
32 const drawerWidth = 240;
33 const appBarHeight = 102;
34
35 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
36
37 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
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         backgroundColor: '#692498',
50         position: "absolute",
51         width: "100%"
52     },
53     drawerPaper: {
54         position: 'relative',
55         width: drawerWidth,
56         display: 'flex',
57         flexDirection: 'column',
58     },
59     contentWrapper: {
60         backgroundColor: theme.palette.background.default,
61         display: "flex",
62         flexGrow: 1,
63         minWidth: 0,
64         paddingTop: appBarHeight
65     },
66     content: {
67         padding: theme.spacing.unit * 3,
68         overflowY: "auto",
69         flexGrow: 1
70     },
71     toolbar: theme.mixins.toolbar
72 });
73
74 interface WorkbenchDataProps {
75     projects: Array<TreeItem<Project>>;
76     currentProjectId: string;
77     user?: User;
78     sidePanelItems: SidePanelItem[];
79 }
80
81 interface WorkbenchActionProps {
82 }
83
84 type WorkbenchProps = WorkbenchDataProps & WorkbenchActionProps & DispatchProp & WithStyles<CssRules>;
85
86 interface NavBreadcrumb extends Breadcrumb {
87     itemId: string;
88 }
89
90 interface NavMenuItem extends MainAppBarMenuItem {
91     action: () => void;
92 }
93
94 interface WorkbenchState {
95     isCreationDialogOpen: boolean;
96     anchorEl: any;
97     searchText: string;
98     menuItems: {
99         accountMenu: NavMenuItem[],
100         helpMenu: NavMenuItem[],
101         anonymousMenu: NavMenuItem[]
102     };
103 }
104
105 class Workbench extends React.Component<WorkbenchProps, WorkbenchState> {
106     state = {
107         isCreationDialogOpen: false,
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     };
136
137     mainAppBarActions: MainAppBarActionProps = {
138         onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
139             this.props.dispatch<any>(setProjectItem(itemId, ItemMode.BOTH));
140         },
141         onSearch: searchText => {
142             this.setState({ searchText });
143             this.props.dispatch(push(`/search?q=${searchText}`));
144         },
145         onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action()
146     };
147
148     toggleSidePanelOpen = (itemId: string) => {
149         this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
150     }
151
152     toggleSidePanelActive = (itemId: string) => {
153         this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
154         this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
155     }
156
157     handleCreationDialogOpen = () => {
158         this.setState({ isCreationDialogOpen: true });
159     }
160
161     handleCreationDialogClose = () => {
162         this.setState({ isCreationDialogOpen: false });
163     }
164
165     render() {
166         const path = getTreePath(this.props.projects, this.props.currentProjectId);
167         const breadcrumbs = path.map(item => ({
168             label: item.data.name,
169             itemId: item.data.uuid,
170             status: item.status
171         }));
172
173         const { classes, user } = this.props;
174         return (
175             <div className={classes.root}>
176                 <div className={classes.appBar}>
177                     <MainAppBar
178                         breadcrumbs={breadcrumbs}
179                         searchText={this.state.searchText}
180                         user={this.props.user}
181                         menuItems={this.state.menuItems}
182                         {...this.mainAppBarActions}
183                     />
184                 </div>
185                 {user &&
186                     <Drawer
187                         variant="permanent"
188                         classes={{
189                             paper: classes.drawerPaper,
190                         }}>
191                         <div className={classes.toolbar} />
192                         <SidePanel
193                             toggleOpen={this.toggleSidePanelOpen}
194                             toggleActive={this.toggleSidePanelActive}
195                             sidePanelItems={this.props.sidePanelItems}
196                             handleCreationDialogOpen={this.handleCreationDialogOpen}
197                             handleCreationDialogClose={this.handleCreationDialogClose}>
198                             <ProjectTree
199                                 projects={this.props.projects}
200                                 toggleOpen={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.OPEN))}
201                                 toggleActive={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE))}
202                                 handleCreationDialogOpen={this.handleCreationDialogOpen}
203                                 handleCreationDialogClose={this.handleCreationDialogClose}
204                             />
205                         </SidePanel>
206                     </Drawer>}
207                 <main className={classes.contentWrapper}>
208                     <div className={classes.content}>
209                         <Switch>
210                             <Route path="/projects/:id" render={this.renderProjectPanel} />
211                         </Switch>
212                     </div>
213                 </main>
214             </div>
215         );
216     }
217
218     renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
219         onItemRouteChange={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE))}
220         onItemClick={item => this.props.dispatch<any>(setProjectItem(item.uuid, ItemMode.ACTIVE))}
221         handleCreationDialogOpen={this.handleCreationDialogOpen}
222         handleCreationDialogClose={this.handleCreationDialogClose}
223         isCreationDialogOpen={this.state.isCreationDialogOpen}
224         {...props} />
225     
226 }
227
228 export default connect<WorkbenchDataProps>(
229     (state: RootState) => ({
230         projects: state.projects.items,
231         currentProjectId: state.projects.currentItemId,
232         user: state.auth.user,
233         sidePanelItems: state.sidePanel
234     })
235 )(
236     withStyles(styles)(Workbench)
237 );