merge-conflicts
[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 { 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
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 WorkbenchGeneralProps {
106     authService: AuthService;
107     buildInfo: string;
108 }
109
110 interface WorkbenchActionProps {
111 }
112
113 type WorkbenchProps = WorkbenchDataProps & WorkbenchGeneralProps & WorkbenchActionProps & DispatchProp<any> & WithStyles<CssRules>;
114
115 interface NavBreadcrumb extends Breadcrumb {
116     itemId: string;
117 }
118
119 interface NavMenuItem extends MainAppBarMenuItem {
120     action: () => void;
121 }
122
123 interface WorkbenchState {
124     isCurrentTokenDialogOpen: boolean;
125     anchorEl: any;
126     searchText: string;
127     menuItems: {
128         accountMenu: NavMenuItem[],
129         helpMenu: NavMenuItem[],
130         anonymousMenu: NavMenuItem[]
131     };
132 }
133
134 export const Workbench = withStyles(styles)(
135     connect<WorkbenchDataProps>(
136         (state: RootState) => ({
137             projects: state.projects.items,
138             currentProjectId: state.projects.currentItemId,
139             user: state.auth.user,
140             currentToken: state.auth.apiToken,
141             sidePanelItems: state.sidePanel
142         })
143     )(
144         class extends React.Component<WorkbenchProps, WorkbenchState> {
145             state = {
146                 isCreationDialogOpen: false,
147                 isCurrentTokenDialogOpen: false,
148                 anchorEl: null,
149                 searchText: "",
150                 breadcrumbs: [],
151                 menuItems: {
152                     accountMenu: [
153                         {
154                             label: 'Current token',
155                             action: () => this.toggleCurrentTokenModal()
156                         },
157                         {
158                             label: "Logout",
159                             action: () => this.props.dispatch(logout())
160                         },
161                         {
162                             label: "My account",
163                             action: () => this.props.dispatch(push("/my-account"))
164                         }
165                     ],
166                     helpMenu: [
167                         {
168                             label: "Help",
169                             action: () => this.props.dispatch(push("/help"))
170                         }
171                     ],
172                     anonymousMenu: [
173                         {
174                             label: "Sign in",
175                             action: () => this.props.dispatch(login())
176                         }
177                     ]
178                 }
179             };
180
181             render() {
182                 const path = getTreePath(this.props.projects, this.props.currentProjectId);
183                 const breadcrumbs = path.map(item => ({
184                     label: item.data.name,
185                     itemId: item.data.uuid,
186                     status: item.status
187                 }));
188
189                 const { classes, user } = this.props;
190                 return (
191                     <div className={classes.root}>
192                         <div className={classes.appBar}>
193                             <MainAppBar
194                                 breadcrumbs={breadcrumbs}
195                                 searchText={this.state.searchText}
196                                 user={this.props.user}
197                                 menuItems={this.state.menuItems}
198                                 buildInfo={this.props.buildInfo}
199                                 {...this.mainAppBarActions} />
200                         </div>
201                         {user &&
202                             <Drawer
203                                 variant="permanent"
204                                 classes={{
205                                     paper: classes.drawerPaper,
206                                 }}>
207                                 <div className={classes.toolbar} />
208                                 <SidePanel
209                                     toggleOpen={this.toggleSidePanelOpen}
210                                     toggleActive={this.toggleSidePanelActive}
211                                     sidePanelItems={this.props.sidePanelItems}
212                                     onContextMenu={(event) => this.openContextMenu(event, {
213                                         uuid: this.props.authService.getUuid() || "",
214                                         name: "",
215                                         kind: ContextMenuKind.ROOT_PROJECT
216                                     })}>
217                                     <ProjectTree
218                                         projects={this.props.projects}
219                                         toggleOpen={itemId => this.props.dispatch(setProjectItem(itemId, ItemMode.OPEN))}
220                                         onContextMenu={(event, item) => this.openContextMenu(event, {
221                                             uuid: item.data.uuid,
222                                             name: item.data.name,
223                                             kind: ContextMenuKind.PROJECT
224                                         })}
225                                         toggleActive={itemId => {
226                                             this.props.dispatch(setProjectItem(itemId, ItemMode.ACTIVE));
227                                             this.props.dispatch(loadDetails(itemId, ResourceKind.PROJECT));
228                                         }} />
229                                 </SidePanel>
230                             </Drawer>}
231                         <main className={classes.contentWrapper}>
232                             <div className={classes.content}>
233                                 <Switch>
234                                     <Route path='/' exact render={() => <Redirect to={`/projects/${this.props.authService.getUuid()}`} />} />
235                                     <Route path="/projects/:id" render={this.renderProjectPanel} />
236                                     <Route path="/favorites" render={this.renderFavoritePanel} />
237                                     <Route path="/collections/:id" render={this.renderCollectionPanel} />
238                                 </Switch>
239                             </div>
240                             {user && <DetailsPanel />}
241                         </main>
242                         <ContextMenu />
243                         <Snackbar />
244                         <CreateProjectDialog />
245                         <CreateCollectionDialog />
246                         <RenameFileDialog />
247                         <MoveToProjectDialog />
248                         <DialogCollectionCreateWithSelectedFile />
249                         <FileRemoveDialog />
250                         <MultipleFilesRemoveDialog />
251                         <UpdateCollectionDialog />
252                         <UploadCollectionFilesDialog />
253                         <UpdateProjectDialog />
254                         <CurrentTokenDialog
255                             currentToken={this.props.currentToken}
256                             open={this.state.isCurrentTokenDialogOpen}
257                             handleClose={this.toggleCurrentTokenModal} />
258                     </div>
259                 );
260             }
261
262             renderCollectionPanel = (props: RouteComponentProps<{ id: string }>) => <CollectionPanel
263                 onItemRouteChange={(collectionId) => {
264                     this.props.dispatch<any>(loadCollection(collectionId));
265                     this.props.dispatch<any>(loadCollectionTags(collectionId));
266                 }}
267                 onContextMenu={(event, item) => {
268                     this.openContextMenu(event, {
269                         uuid: item.uuid,
270                         name: item.name,
271                         description: item.description,
272                         kind: ContextMenuKind.COLLECTION
273                     });
274                 }}
275                 {...props} />
276
277             renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
278                 onItemRouteChange={itemId => this.props.dispatch(setProjectItem(itemId, ItemMode.ACTIVE))}
279                 onContextMenu={(event, item) => {
280                     let kind: ContextMenuKind;
281
282                     if (item.kind === ResourceKind.PROJECT) {
283                         kind = ContextMenuKind.PROJECT;
284                     } else if (item.kind === ResourceKind.COLLECTION) {
285                         kind = ContextMenuKind.COLLECTION_RESOURCE;
286                     } else {
287                         kind = ContextMenuKind.RESOURCE;
288                     }
289
290                     this.openContextMenu(event, {
291                         uuid: item.uuid,
292                         name: item.name,
293                         description: item.description,
294                         kind
295                     });
296                 }}
297                 onProjectCreationDialogOpen={this.handleProjectCreationDialogOpen}
298                 onCollectionCreationDialogOpen={this.handleCollectionCreationDialogOpen}
299                 onItemClick={item => {
300                     this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
301                 }}
302                 onItemDoubleClick={item => {
303                     switch (item.kind) {
304                         case ResourceKind.COLLECTION:
305                             this.props.dispatch(loadCollection(item.uuid));
306                             this.props.dispatch(push(getCollectionUrl(item.uuid)));
307                         default:
308                             this.props.dispatch(setProjectItem(item.uuid, ItemMode.ACTIVE));
309                             this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
310                     }
311
312                 }}
313                 {...props} />
314
315             renderFavoritePanel = (props: RouteComponentProps<{ id: string }>) => <FavoritePanel
316                 onItemRouteChange={() => this.props.dispatch(favoritePanelActions.REQUEST_ITEMS())}
317                 onContextMenu={(event, item) => {
318                     const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
319                     this.openContextMenu(event, {
320                         uuid: item.uuid,
321                         name: item.name,
322                         kind,
323                     });
324                 }}
325                 onDialogOpen={this.handleProjectCreationDialogOpen}
326                 onItemClick={item => {
327                     this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
328                 }}
329                 onItemDoubleClick={item => {
330                     switch (item.kind) {
331                         case ResourceKind.COLLECTION:
332                             this.props.dispatch(loadCollection(item.uuid));
333                             this.props.dispatch(push(getCollectionUrl(item.uuid)));
334                         default:
335                             this.props.dispatch(loadDetails(item.uuid, ResourceKind.PROJECT));
336                             this.props.dispatch(setProjectItem(item.uuid, ItemMode.ACTIVE));
337                     }
338
339                 }}
340                 {...props} />
341
342             mainAppBarActions: MainAppBarActionProps = {
343                 onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
344                     this.props.dispatch(setProjectItem(itemId, ItemMode.BOTH));
345                     this.props.dispatch(loadDetails(itemId, ResourceKind.PROJECT));
346                 },
347                 onSearch: searchText => {
348                     this.setState({ searchText });
349                     this.props.dispatch(push(`/search?q=${searchText}`));
350                 },
351                 onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
352                 onDetailsPanelToggle: () => {
353                     this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
354                 },
355                 onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: NavBreadcrumb) => {
356                     this.openContextMenu(event, {
357                         uuid: breadcrumb.itemId,
358                         name: breadcrumb.label,
359                         kind: ContextMenuKind.PROJECT
360                     });
361                 }
362             };
363
364             toggleSidePanelOpen = (itemId: string) => {
365                 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
366             }
367
368             toggleSidePanelActive = (itemId: string) => {
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 );