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