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