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