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