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