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