Added data selector for workbench data explorer
[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
7 import { StyleRulesCallback, Theme, WithStyles, withStyles } from '@material-ui/core/styles';
8 import Drawer from '@material-ui/core/Drawer';
9 import { connect, DispatchProp } from "react-redux";
10 import { Route, Switch } from "react-router";
11 import authActions from "../../store/auth/auth-action";
12 import { User } from "../../models/user";
13 import { RootState } from "../../store/store";
14 import MainAppBar, { MainAppBarActionProps, MainAppBarMenuItem } from '../../views-components/main-app-bar/main-app-bar';
15 import { Breadcrumb } from '../../components/breadcrumbs/breadcrumbs';
16 import { push } from 'react-router-redux';
17 import projectActions, { getProjectList } from "../../store/project/project-action";
18 import ProjectTree from '../../views-components/project-tree/project-tree';
19 import { TreeItem, TreeItemStatus } from "../../components/tree/tree";
20 import { Project } from "../../models/project";
21 import { getTreePath } from '../../store/project/project-reducer';
22 import DataExplorer from '../data-explorer/data-explorer';
23 import { getCollectionList } from "../../store/collection/collection-action";
24
25 const drawerWidth = 240;
26 const appBarHeight = 102;
27
28 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
29
30 const styles: StyleRulesCallback<CssRules> = (theme: Theme) => ({
31     root: {
32         flexGrow: 1,
33         zIndex: 1,
34         overflow: 'hidden',
35         position: 'relative',
36         display: 'flex',
37         width: '100vw',
38         height: '100vh'
39     },
40     appBar: {
41         zIndex: theme.zIndex.drawer + 1,
42         backgroundColor: '#692498',
43         position: "absolute",
44         width: "100%"
45     },
46     drawerPaper: {
47         position: 'relative',
48         width: drawerWidth,
49     },
50     contentWrapper: {
51         backgroundColor: theme.palette.background.default,
52         display: "flex",
53         flexGrow: 1,
54         minWidth: 0,
55         paddingTop: appBarHeight
56     },
57     content: {
58         padding: theme.spacing.unit * 3,
59         overflowY: "auto",
60         flexGrow: 1
61     },
62     toolbar: theme.mixins.toolbar
63 });
64
65 interface WorkbenchDataProps {
66     projects: Array<TreeItem<Project>>;
67     user?: User;
68 }
69
70 interface WorkbenchActionProps {
71 }
72
73 type WorkbenchProps = WorkbenchDataProps & WorkbenchActionProps & DispatchProp & WithStyles<CssRules>;
74
75 interface NavBreadcrumb extends Breadcrumb {
76     itemId: string;
77     status: TreeItemStatus;
78 }
79
80 interface NavMenuItem extends MainAppBarMenuItem {
81     action: () => void;
82 }
83
84 interface WorkbenchState {
85     anchorEl: any;
86     breadcrumbs: NavBreadcrumb[];
87     searchText: string;
88     menuItems: {
89         accountMenu: NavMenuItem[],
90         helpMenu: NavMenuItem[],
91         anonymousMenu: NavMenuItem[]
92     };
93 }
94
95 class Workbench extends React.Component<WorkbenchProps, WorkbenchState> {
96     state = {
97         anchorEl: null,
98         searchText: "",
99         breadcrumbs: [],
100         menuItems: {
101             accountMenu: [
102                 {
103                     label: "Logout",
104                     action: () => this.props.dispatch(authActions.LOGOUT())
105                 },
106                 {
107                     label: "My account",
108                     action: () => this.props.dispatch(push("/my-account"))
109                 }
110             ],
111             helpMenu: [
112                 {
113                     label: "Help",
114                     action: () => this.props.dispatch(push("/help"))
115                 }
116             ],
117             anonymousMenu: [
118                 {
119                     label: "Sign in",
120                     action: () => this.props.dispatch(authActions.LOGIN())
121                 }
122             ]
123         }
124     };
125
126     mainAppBarActions: MainAppBarActionProps = {
127         onBreadcrumbClick: ({ itemId, status }: NavBreadcrumb) => {
128             this.toggleProjectTreeItem(itemId, status);
129         },
130         onSearch: searchText => {
131             this.setState({ searchText });
132             this.props.dispatch(push(`/search?q=${searchText}`));
133         },
134         onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action()
135     };
136
137     toggleProjectTreeItem = (itemId: string, status: TreeItemStatus) => {
138         if (status === TreeItemStatus.Loaded) {
139             this.openProjectItem(itemId);
140         } else {
141             this.props.dispatch<any>(getProjectList(itemId))
142                 .then(() => this.openProjectItem(itemId));
143         }
144         this.props.dispatch<any>(getCollectionList(itemId));
145     }
146
147     openProjectItem = (itemId: string) => {
148         const branch = getTreePath(this.props.projects, itemId);
149         this.setState({
150             breadcrumbs: branch.map(item => ({
151                 label: item.data.name,
152                 itemId: item.data.uuid,
153                 status: item.status
154             }))
155         });
156         this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM(itemId));
157         this.props.dispatch(push(`/project/${itemId}`));
158     }
159
160     render() {
161         const { classes, user } = this.props;
162         return (
163             <div className={classes.root}>
164                 <div className={classes.appBar}>
165                     <MainAppBar
166                         breadcrumbs={this.state.breadcrumbs}
167                         searchText={this.state.searchText}
168                         user={this.props.user}
169                         menuItems={this.state.menuItems}
170                         {...this.mainAppBarActions}
171                     />
172                 </div>
173                 {user &&
174                     <Drawer
175                         variant="permanent"
176                         classes={{
177                             paper: classes.drawerPaper,
178                         }}>
179                         <div className={classes.toolbar} />
180                         <ProjectTree
181                             projects={this.props.projects}
182                             toggleProjectTreeItem={this.toggleProjectTreeItem} />
183                     </Drawer>}
184                 <main className={classes.contentWrapper}>
185                     <div className={classes.content}>
186                         <Switch>
187                             <Route path="/project/:uuid" component={DataExplorer} />
188                         </Switch>
189                     </div>
190                 </main>
191             </div>
192         );
193     }
194 }
195
196 export default connect<WorkbenchDataProps>(
197     (state: RootState) => ({
198         projects: state.projects,
199         user: state.auth.user
200     })
201 )(
202     withStyles(styles)(Workbench)
203 );