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