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