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