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