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