refs #master Merge branch 'origin/master' into 13828-trash-view
[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 { connect, DispatchProp } from "react-redux";
8 import { Route, Switch } from "react-router";
9 import { login, logout } from "~/store/auth/auth-action";
10 import { User } from "~/models/user";
11 import { RootState } from "~/store/store";
12 import { MainAppBar, MainAppBarActionProps, MainAppBarMenuItem } from '~/views-components/main-app-bar/main-app-bar';
13 import { push } from 'react-router-redux';
14 import { ProjectPanel } from "~/views/project-panel/project-panel";
15 import { DetailsPanel } from '~/views-components/details-panel/details-panel';
16 import { ArvadosTheme } from '~/common/custom-theme';
17 import { detailsPanelActions } from "~/store/details-panel/details-panel-action";
18 import { ContextMenu } from "~/views-components/context-menu/context-menu";
19 import { FavoritePanel } from "../favorite-panel/favorite-panel";
20 import { CurrentTokenDialog } from '~/views-components/current-token-dialog/current-token-dialog';
21 import { Snackbar } from '~/views-components/snackbar/snackbar';
22 import { CollectionPanel } from '../collection-panel/collection-panel';
23 import { AuthService } from "~/services/auth-service/auth-service";
24 import { RenameFileDialog } from '~/views-components/rename-file-dialog/rename-file-dialog';
25 import { FileRemoveDialog } from '~/views-components/file-remove-dialog/file-remove-dialog';
26 import { MultipleFilesRemoveDialog } from '~/views-components/file-remove-dialog/multiple-files-remove-dialog';
27 import { Routes } from '~/routes/routes';
28 import { SidePanel } from '~/views-components/side-panel/side-panel';
29 import { ProcessPanel } from '~/views/process-panel/process-panel';
30 import { Breadcrumbs } from '~/views-components/breadcrumbs/breadcrumbs';
31 import { CreateProjectDialog } from '~/views-components/dialog-forms/create-project-dialog';
32 import { CreateCollectionDialog } from '~/views-components/dialog-forms/create-collection-dialog';
33 import { CopyCollectionDialog } from '~/views-components/dialog-forms/copy-collection-dialog';
34 import { UpdateCollectionDialog } from '~/views-components/dialog-forms/update-collection-dialog';
35 import { UpdateProjectDialog } from '~/views-components/dialog-forms/update-project-dialog';
36 import { MoveProjectDialog } from '~/views-components/dialog-forms/move-project-dialog';
37 import { MoveCollectionDialog } from '~/views-components/dialog-forms/move-collection-dialog';
38
39 import { FilesUploadCollectionDialog } from '~/views-components/dialog-forms/files-upload-collection-dialog';
40 import { PartialCopyCollectionDialog } from '~/views-components/dialog-forms/partial-copy-collection-dialog';
41
42 import { TrashPanel } from "~/views/trash-panel/trash-panel";
43 import { trashPanelActions } from "~/store/trash-panel/trash-panel-action";
44
45 const APP_BAR_HEIGHT = 100;
46
47 type CssRules = 'root' | 'appBar' | 'content' | 'contentWrapper';
48
49 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
50     root: {
51         flexGrow: 1,
52         zIndex: 1,
53         overflow: 'hidden',
54         position: 'relative',
55         display: 'flex',
56         width: '100vw',
57         height: '100vh'
58     },
59     appBar: {
60         zIndex: theme.zIndex.drawer + 1,
61         position: "absolute",
62         width: "100%"
63     },
64     contentWrapper: {
65         backgroundColor: theme.palette.background.default,
66         display: "flex",
67         flexGrow: 1,
68         minWidth: 0,
69         paddingTop: APP_BAR_HEIGHT
70     },
71     content: {
72         padding: `${theme.spacing.unit}px ${theme.spacing.unit * 3}px`,
73         overflowY: "auto",
74         flexGrow: 1,
75         position: 'relative'
76     },
77 });
78
79 interface WorkbenchDataProps {
80     user?: User;
81     currentToken?: string;
82 }
83
84 interface WorkbenchGeneralProps {
85     authService: AuthService;
86     buildInfo: string;
87 }
88
89 interface WorkbenchActionProps {
90 }
91
92 type WorkbenchProps = WorkbenchDataProps & WorkbenchGeneralProps & WorkbenchActionProps & DispatchProp<any> & WithStyles<CssRules>;
93
94 interface NavMenuItem extends MainAppBarMenuItem {
95     action: () => void;
96 }
97
98 interface WorkbenchState {
99     isCurrentTokenDialogOpen: boolean;
100     anchorEl: any;
101     searchText: string;
102     menuItems: {
103         accountMenu: NavMenuItem[],
104         helpMenu: NavMenuItem[],
105         anonymousMenu: NavMenuItem[]
106     };
107 }
108
109 export const Workbench = withStyles(styles)(
110     connect<WorkbenchDataProps>(
111         (state: RootState) => ({
112             user: state.auth.user,
113             currentToken: state.auth.apiToken,
114         })
115     )(
116         class extends React.Component<WorkbenchProps, WorkbenchState> {
117             state = {
118                 isCurrentTokenDialogOpen: false,
119                 anchorEl: null,
120                 searchText: "",
121                 breadcrumbs: [],
122                 menuItems: {
123                     accountMenu: [
124                         {
125                             label: 'Current token',
126                             action: () => this.toggleCurrentTokenModal()
127                         },
128                         {
129                             label: "Logout",
130                             action: () => this.props.dispatch(logout())
131                         },
132                         {
133                             label: "My account",
134                             action: () => this.props.dispatch(push("/my-account"))
135                         }
136                     ],
137                     helpMenu: [
138                         {
139                             label: "Help",
140                             action: () => this.props.dispatch(push("/help"))
141                         }
142                     ],
143                     anonymousMenu: [
144                         {
145                             label: "Sign in",
146                             action: () => this.props.dispatch(login())
147                         }
148                     ]
149                 }
150             };
151
152             render() {
153                 const { classes, user } = this.props;
154                 return (
155                     <div className={classes.root}>
156                         <div className={classes.appBar}>
157                             <MainAppBar
158                                 breadcrumbs={Breadcrumbs}
159                                 searchText={this.state.searchText}
160                                 user={this.props.user}
161                                 menuItems={this.state.menuItems}
162                                 buildInfo={this.props.buildInfo}
163                                 {...this.mainAppBarActions} />
164                         </div>
165                         {user && <SidePanel />}
166                         <main className={classes.contentWrapper}>
167                             <div className={classes.content}>
168                                 <Switch>
169                                     <Route path={Routes.PROJECTS} component={ProjectPanel} />
170                                     <Route path={Routes.COLLECTIONS} component={CollectionPanel} />
171                                     <Route path={Routes.FAVORITES} component={FavoritePanel} />
172                                     <Route path={Routes.PROCESSES} component={ProcessPanel} />
173                                     <Route path="/trash" render={this.renderTrashPanel} />
174                                 </Switch>
175                             </div>
176                             {user && <DetailsPanel />}
177                         </main>
178                         <ContextMenu />
179                         <Snackbar />
180                         <CreateProjectDialog />
181                         <CreateCollectionDialog />
182                         <RenameFileDialog />
183                         <PartialCopyCollectionDialog />
184                         <FileRemoveDialog />
185                         <CopyCollectionDialog />
186                         <FileRemoveDialog />
187                         <MultipleFilesRemoveDialog />
188                         <UpdateCollectionDialog />
189                         <FilesUploadCollectionDialog />
190                         <UpdateProjectDialog />
191                         <MoveCollectionDialog />
192                         <MoveProjectDialog />
193                         <CurrentTokenDialog
194                             currentToken={this.props.currentToken}
195                             open={this.state.isCurrentTokenDialogOpen}
196                             handleClose={this.toggleCurrentTokenModal} />
197                     </div>
198                 );
199             }
200
201             mainAppBarActions: MainAppBarActionProps = {
202                 onSearch: searchText => {
203                     this.setState({ searchText });
204                     this.props.dispatch(push(`/search?q=${searchText}`));
205                 },
206                 onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
207                 onDetailsPanelToggle: () => {
208                     this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
209                 },
210             };
211
212             toggleCurrentTokenModal = () => {
213                 this.setState({ isCurrentTokenDialogOpen: !this.state.isCurrentTokenDialogOpen });
214             }
215         }
216     )
217 );