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