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