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