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