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