Merge branch 'master' into 13854-tags-card
[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 { login, logout } 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 { collectionCreateActions } from '../../store/collections/creator/collection-creator-action';
24 import { ProjectPanel } from "../project-panel/project-panel";
25 import { DetailsPanel } from '../../views-components/details-panel/details-panel';
26 import { ArvadosTheme } from '../../common/custom-theme';
27 import { CreateProjectDialog } from "../../views-components/create-project-dialog/create-project-dialog";
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 { FavoritePanel } from "../favorite-panel/favorite-panel";
36 import { CurrentTokenDialog } from '../../views-components/current-token-dialog/current-token-dialog';
37 import { Snackbar } from '../../views-components/snackbar/snackbar';
38 import { favoritePanelActions } from '../../store/favorite-panel/favorite-panel-action';
39 import { CreateCollectionDialog } from '../../views-components/create-collection-dialog/create-collection-dialog';
40 import { CollectionPanel } from '../collection-panel/collection-panel';
41 import { loadCollection, loadCollectionTags } from '../../store/collection-panel/collection-panel-action';
42 import { getCollectionUrl } from '../../models/collection';
43 import { RemoveDialog } from '../../views-components/remove-dialog/remove-dialog';
44 import { RenameDialog } from '../../views-components/rename-dialog/rename-dialog';
45 import { UpdateCollectionDialog } from '../../views-components/update-collection-dialog/update-collection-dialog.';
46 import { AuthService } from "../../services/auth-service/auth-service";
47
48 const DRAWER_WITDH = 240;
49 const APP_BAR_HEIGHT = 100;
50
51 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
52
53 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
54     root: {
55         flexGrow: 1,
56         zIndex: 1,
57         overflow: 'hidden',
58         position: 'relative',
59         display: 'flex',
60         width: '100vw',
61         height: '100vh'
62     },
63     appBar: {
64         zIndex: theme.zIndex.drawer + 1,
65         position: "absolute",
66         width: "100%"
67     },
68     drawerPaper: {
69         position: 'relative',
70         width: DRAWER_WITDH,
71         display: 'flex',
72         flexDirection: 'column',
73     },
74     contentWrapper: {
75         backgroundColor: theme.palette.background.default,
76         display: "flex",
77         flexGrow: 1,
78         minWidth: 0,
79         paddingTop: APP_BAR_HEIGHT
80     },
81     content: {
82         padding: `${theme.spacing.unit}px ${theme.spacing.unit * 3}px`,
83         overflowY: "auto",
84         flexGrow: 1
85     },
86     toolbar: theme.mixins.toolbar
87 });
88
89 interface WorkbenchDataProps {
90     projects: Array<TreeItem<ProjectResource>>;
91     currentProjectId: string;
92     user?: User;
93     currentToken?: string;
94     sidePanelItems: SidePanelItem[];
95 }
96
97 interface WorkbenchServiceProps {
98     authService: AuthService;
99 }
100
101 interface WorkbenchActionProps {
102 }
103
104 type WorkbenchProps = WorkbenchDataProps & WorkbenchServiceProps & WorkbenchActionProps & DispatchProp<any> & WithStyles<CssRules>;
105
106 interface NavBreadcrumb extends Breadcrumb {
107     itemId: string;
108 }
109
110 interface NavMenuItem extends MainAppBarMenuItem {
111     action: () => void;
112 }
113
114 interface WorkbenchState {
115     isCurrentTokenDialogOpen: boolean;
116     anchorEl: any;
117     searchText: string;
118     menuItems: {
119         accountMenu: NavMenuItem[],
120         helpMenu: NavMenuItem[],
121         anonymousMenu: NavMenuItem[]
122     };
123 }
124
125 export const Workbench = withStyles(styles)(
126     connect<WorkbenchDataProps>(
127         (state: RootState) => ({
128             projects: state.projects.items,
129             currentProjectId: state.projects.currentItemId,
130             user: state.auth.user,
131             currentToken: state.auth.apiToken,
132             sidePanelItems: state.sidePanel
133         })
134     )(
135         class extends React.Component<WorkbenchProps, WorkbenchState> {
136             state = {
137                 isCreationDialogOpen: false,
138                 isCurrentTokenDialogOpen: false,
139                 anchorEl: null,
140                 searchText: "",
141                 breadcrumbs: [],
142                 menuItems: {
143                     accountMenu: [
144                         {
145                             label: 'Current token',
146                             action: () => this.toggleCurrentTokenModal()
147                         },
148                         {
149                             label: "Logout",
150                             action: () => this.props.dispatch(logout())
151                         },
152                         {
153                             label: "My account",
154                             action: () => this.props.dispatch(push("/my-account"))
155                         }
156                     ],
157                     helpMenu: [
158                         {
159                             label: "Help",
160                             action: () => this.props.dispatch(push("/help"))
161                         }
162                     ],
163                     anonymousMenu: [
164                         {
165                             label: "Sign in",
166                             action: () => this.props.dispatch(login())
167                         }
168                     ]
169                 }
170             };
171
172             render() {
173                 const path = getTreePath(this.props.projects, this.props.currentProjectId);
174                 const breadcrumbs = path.map(item => ({
175                     label: item.data.name,
176                     itemId: item.data.uuid,
177                     status: item.status
178                 }));
179
180                 const { classes, user } = this.props;
181                 return (
182                     <div className={classes.root}>
183                         <div className={classes.appBar}>
184                             <MainAppBar
185                                 breadcrumbs={breadcrumbs}
186                                 searchText={this.state.searchText}
187                                 user={this.props.user}
188                                 menuItems={this.state.menuItems}
189                                 {...this.mainAppBarActions} />
190                         </div>
191                         {user &&
192                             <Drawer
193                                 variant="permanent"
194                                 classes={{
195                                     paper: classes.drawerPaper,
196                                 }}>
197                                 <div className={classes.toolbar} />
198                                 <SidePanel
199                                     toggleOpen={this.toggleSidePanelOpen}
200                                     toggleActive={this.toggleSidePanelActive}
201                                     sidePanelItems={this.props.sidePanelItems}
202                                     onContextMenu={(event) => this.openContextMenu(event, {
203                                         uuid: this.props.authService.getUuid() || "",
204                                         name: "",
205                                         kind: ContextMenuKind.ROOT_PROJECT
206                                     })}>
207                                     <ProjectTree
208                                         projects={this.props.projects}
209                                         toggleOpen={itemId => this.props.dispatch(setProjectItem(itemId, ItemMode.OPEN))}
210                                         onContextMenu={(event, item) => this.openContextMenu(event, {
211                                             uuid: item.data.uuid,
212                                             name: item.data.name,
213                                             kind: ContextMenuKind.PROJECT
214                                         })}
215                                         toggleActive={itemId => {
216                                             this.props.dispatch(setProjectItem(itemId, ItemMode.ACTIVE));
217                                             this.props.dispatch(loadDetails(itemId, ResourceKind.PROJECT));
218                                             this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
219                                         }} />
220                                 </SidePanel>
221                             </Drawer>}
222                         <main className={classes.contentWrapper}>
223                             <div className={classes.content}>
224                                 <Switch>
225                                     <Route path="/projects/:id" render={this.renderProjectPanel} />
226                                     <Route path="/favorites" render={this.renderFavoritePanel} />
227                                     <Route path="/collections/:id" render={this.renderCollectionPanel} />
228                                 </Switch>
229                             </div>
230                             {user && <DetailsPanel />}
231                         </main>
232                         <ContextMenu />
233                         <Snackbar />
234                         <CreateProjectDialog />
235                         <CreateCollectionDialog />
236                         <RemoveDialog />
237                         <RenameDialog />
238                         <UpdateCollectionDialog />
239                         <CurrentTokenDialog
240                             currentToken={this.props.currentToken}
241                             open={this.state.isCurrentTokenDialogOpen}
242                             handleClose={this.toggleCurrentTokenModal} />
243                     </div>
244                 );
245             }
246
247             renderCollectionPanel = (props: RouteComponentProps<{ id: string }>) => <CollectionPanel 
248                 onItemRouteChange={(collectionId) => {
249                     this.props.dispatch<any>(loadCollection(collectionId, ResourceKind.COLLECTION));
250                     this.props.dispatch<any>(loadCollectionTags(collectionId));
251                 }}
252                 onContextMenu={(event, item) => {
253                     this.openContextMenu(event, {
254                         uuid: item.uuid,
255                         name: item.name,
256                         kind: ContextMenuKind.COLLECTION
257                     });
258                 }}
259                 {...props} />
260
261             renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
262                 onItemRouteChange={itemId => this.props.dispatch(setProjectItem(itemId, ItemMode.ACTIVE))}
263                 onContextMenu={(event, item) => {
264
265                     const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
266                     this.openContextMenu(event, {
267                         uuid: item.uuid,
268                         name: item.name,
269                         kind
270                     });
271                 }}
272                 onProjectCreationDialogOpen={this.handleProjectCreationDialogOpen}
273                 onCollectionCreationDialogOpen={this.handleCollectionCreationDialogOpen}
274                 onItemClick={item => {
275                     this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
276                 }}
277                 onItemDoubleClick={item => {
278                     switch (item.kind) {
279                         case ResourceKind.COLLECTION:
280                             this.props.dispatch(loadCollection(item.uuid, item.kind as ResourceKind));
281                             this.props.dispatch(push(getCollectionUrl(item.uuid)));
282                         default: 
283                             this.props.dispatch(setProjectItem(item.uuid, ItemMode.ACTIVE));
284                             this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
285                     }
286
287                 }}
288                 {...props} />
289
290             renderFavoritePanel = (props: RouteComponentProps<{ id: string }>) => <FavoritePanel
291                 onItemRouteChange={() => this.props.dispatch(favoritePanelActions.REQUEST_ITEMS())}
292                 onContextMenu={(event, item) => {
293                     const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
294                     this.openContextMenu(event, {
295                         uuid: item.uuid,
296                         name: item.name,
297                         kind,
298                     });
299                 }}
300                 onDialogOpen={this.handleProjectCreationDialogOpen}
301                 onItemClick={item => {
302                     this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
303                 }}
304                 onItemDoubleClick={item => {
305                     switch (item.kind) {
306                         case ResourceKind.COLLECTION:
307                             this.props.dispatch(loadCollection(item.uuid, item.kind as ResourceKind));
308                             this.props.dispatch(push(getCollectionUrl(item.uuid)));
309                         default:
310                             this.props.dispatch(loadDetails(item.uuid, ResourceKind.PROJECT));
311                             this.props.dispatch(setProjectItem(item.uuid, ItemMode.ACTIVE));
312                             this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
313                     }
314
315                 }}
316                 {...props} />
317
318             mainAppBarActions: MainAppBarActionProps = {
319                 onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
320                     this.props.dispatch(setProjectItem(itemId, ItemMode.BOTH));
321                     this.props.dispatch(loadDetails(itemId, ResourceKind.PROJECT));
322                 },
323                 onSearch: searchText => {
324                     this.setState({ searchText });
325                     this.props.dispatch(push(`/search?q=${searchText}`));
326                 },
327                 onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
328                 onDetailsPanelToggle: () => {
329                     this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
330                 },
331                 onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: NavBreadcrumb) => {
332                     this.openContextMenu(event, {
333                         uuid: breadcrumb.itemId,
334                         name: breadcrumb.label,
335                         kind: ContextMenuKind.PROJECT
336                     });
337                 }
338             };
339
340             toggleSidePanelOpen = (itemId: string) => {
341                 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
342             }
343
344             toggleSidePanelActive = (itemId: string) => {
345                 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
346                 this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
347                 const panelItem = this.props.sidePanelItems.find(it => it.id === itemId);
348                 if (panelItem && panelItem.activeAction) {
349                     panelItem.activeAction(this.props.dispatch);
350                 }
351             }
352
353             handleProjectCreationDialogOpen = (itemUuid: string) => {
354                 this.props.dispatch(projectActions.OPEN_PROJECT_CREATOR({ ownerUuid: itemUuid }));
355             }
356
357             handleCollectionCreationDialogOpen = (itemUuid: string) => {
358                 this.props.dispatch(collectionCreateActions.OPEN_COLLECTION_CREATOR({ ownerUuid: itemUuid }));
359             }
360
361             openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: { name: string; uuid: string; kind: ContextMenuKind; }) => {
362                 event.preventDefault();
363                 this.props.dispatch(
364                     contextMenuActions.OPEN_CONTEXT_MENU({
365                         position: { x: event.clientX, y: event.clientY },
366                         resource
367                     })
368                 );
369             }
370
371             toggleCurrentTokenModal = () => {
372                 this.setState({ isCurrentTokenDialogOpen: !this.state.isCurrentTokenDialogOpen });
373             }
374         }
375     )
376 );