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