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