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