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