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