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