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