Add refreshing panel list after toggle favorite
[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 Drawer from '@material-ui/core/Drawer';
8 import { connect, DispatchProp } from "react-redux";
9 import { Route, Switch, RouteComponentProps } from "react-router";
10 import { authActions } from "../../store/auth/auth-action";
11 import { User } from "../../models/user";
12 import { RootState } from "../../store/store";
13 import { MainAppBar, MainAppBarActionProps, MainAppBarMenuItem } from '../../views-components/main-app-bar/main-app-bar';
14 import { Breadcrumb } from '../../components/breadcrumbs/breadcrumbs';
15 import { push } from 'react-router-redux';
16 import { ProjectTree } from '../../views-components/project-tree/project-tree';
17 import { TreeItem } from "../../components/tree/tree";
18 import { getTreePath } from '../../store/project/project-reducer';
19 import { sidePanelActions } from '../../store/side-panel/side-panel-action';
20 import { SidePanel, SidePanelItem } from '../../components/side-panel/side-panel';
21 import { ItemMode, setFavoriteItem, setProjectItem } from "../../store/navigation/navigation-action";
22 import { projectActions } from "../../store/project/project-action";
23 import { ProjectPanel } from "../project-panel/project-panel";
24 import { DetailsPanel } from '../../views-components/details-panel/details-panel';
25 import { ArvadosTheme } from '../../common/custom-theme';
26 import { CreateProjectDialog } from "../../views-components/create-project-dialog/create-project-dialog";
27 import { authService } from '../../services/services';
28
29 import { detailsPanelActions, loadDetails } from "../../store/details-panel/details-panel-action";
30 import { contextMenuActions } from "../../store/context-menu/context-menu-actions";
31 import { sidePanelData, SidePanelIdentifiers } from '../../store/side-panel/side-panel-reducer';
32 import { ProjectResource } from '../../models/project';
33 import { ResourceKind } from '../../models/resource';
34 import { ContextMenu, ContextMenuKind } from "../../views-components/context-menu/context-menu";
35 import { FavoritePanel } from "../favorite-panel/favorite-panel";
36
37 const drawerWidth = 240;
38 const appBarHeight = 100;
39
40 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
41
42 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
43     root: {
44         flexGrow: 1,
45         zIndex: 1,
46         overflow: 'hidden',
47         position: 'relative',
48         display: 'flex',
49         width: '100vw',
50         height: '100vh'
51     },
52     appBar: {
53         zIndex: theme.zIndex.drawer + 1,
54         position: "absolute",
55         width: "100%"
56     },
57     drawerPaper: {
58         position: 'relative',
59         width: drawerWidth,
60         display: 'flex',
61         flexDirection: 'column',
62     },
63     contentWrapper: {
64         backgroundColor: theme.palette.background.default,
65         display: "flex",
66         flexGrow: 1,
67         minWidth: 0,
68         paddingTop: appBarHeight
69     },
70     content: {
71         padding: `${theme.spacing.unit}px ${theme.spacing.unit * 3}px`,
72         overflowY: "auto",
73         flexGrow: 1
74     },
75     toolbar: theme.mixins.toolbar
76 });
77
78 interface WorkbenchDataProps {
79     projects: Array<TreeItem<ProjectResource>>;
80     currentProjectId: string;
81     user?: User;
82     sidePanelItems: SidePanelItem[];
83 }
84
85 interface WorkbenchActionProps {
86 }
87
88 type WorkbenchProps = WorkbenchDataProps & WorkbenchActionProps & DispatchProp & WithStyles<CssRules>;
89
90 interface NavBreadcrumb extends Breadcrumb {
91     itemId: string;
92 }
93
94 interface NavMenuItem extends MainAppBarMenuItem {
95     action: () => void;
96 }
97
98 interface WorkbenchState {
99     anchorEl: any;
100     searchText: string;
101     menuItems: {
102         accountMenu: NavMenuItem[],
103         helpMenu: NavMenuItem[],
104         anonymousMenu: NavMenuItem[]
105     };
106 }
107
108 export const Workbench = withStyles(styles)(
109     connect<WorkbenchDataProps>(
110         (state: RootState) => ({
111             projects: state.projects.items,
112             currentProjectId: state.projects.currentItemId,
113             user: state.auth.user,
114             sidePanelItems: state.sidePanel
115         })
116     )(
117         class extends React.Component<WorkbenchProps, WorkbenchState> {
118             state = {
119                 isCreationDialogOpen: false,
120                 anchorEl: null,
121                 searchText: "",
122                 breadcrumbs: [],
123                 menuItems: {
124                     accountMenu: [
125                         {
126                             label: "Logout",
127                             action: () => this.props.dispatch(authActions.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(authActions.LOGIN())
144                         }
145                     ]
146                 }
147             };
148
149             render() {
150                 const path = getTreePath(this.props.projects, this.props.currentProjectId);
151                 const breadcrumbs = path.map(item => ({
152                     label: item.data.name,
153                     itemId: item.data.uuid,
154                     status: item.status
155                 }));
156
157                 const { classes, user } = this.props;
158                 return (
159                     <div className={classes.root}>
160                         <div className={classes.appBar}>
161                             <MainAppBar
162                                 breadcrumbs={breadcrumbs}
163                                 searchText={this.state.searchText}
164                                 user={this.props.user}
165                                 menuItems={this.state.menuItems}
166                                 {...this.mainAppBarActions} />
167                         </div>
168                         {user &&
169                             <Drawer
170                                 variant="permanent"
171                                 classes={{
172                                     paper: classes.drawerPaper,
173                                 }}>
174                                 <div className={classes.toolbar} />
175                                 <SidePanel
176                                     toggleOpen={this.toggleSidePanelOpen}
177                                     toggleActive={this.toggleSidePanelActive}
178                                     sidePanelItems={this.props.sidePanelItems}
179                                     onContextMenu={(event) => this.openContextMenu(event, {
180                                         uuid: authService.getUuid() || "",
181                                         name: "",
182                                         kind: ContextMenuKind.RootProject
183                                     })}>
184                                     <ProjectTree
185                                         projects={this.props.projects}
186                                         toggleOpen={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.OPEN))}
187                                         onContextMenu={(event, item) => this.openContextMenu(event, {
188                                             uuid: item.data.uuid,
189                                             name: item.data.name,
190                                             kind: ContextMenuKind.Project
191                                         })}
192                                         toggleActive={itemId => {
193                                             this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE));
194                                             this.props.dispatch<any>(loadDetails(itemId, ResourceKind.Project));
195                                             this.props.dispatch<any>(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.Projects));
196                                         }} />
197                                 </SidePanel>
198                             </Drawer>}
199                         <main className={classes.contentWrapper}>
200                             <div className={classes.content}>
201                                 <Switch>
202                                     <Route path="/projects/:id" render={this.renderProjectPanel} />
203                                     <Route path="/favorites" render={this.renderFavoritePanel} />
204                                 </Switch>
205                             </div>
206                             {user && <DetailsPanel />}
207                         </main>
208                         <ContextMenu />
209                         <CreateProjectDialog />
210                     </div>
211                 );
212             }
213
214             renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
215                 onItemRouteChange={itemId => this.props.dispatch<any>(setProjectItem(itemId, ItemMode.ACTIVE))}
216                 onContextMenu={(event, item) => {
217                     const kind = item.kind === ResourceKind.Project ? ContextMenuKind.Project : ContextMenuKind.Resource;
218                     this.openContextMenu(event, {
219                         uuid: item.uuid,
220                         name: item.name,
221                         kind
222                     });
223                 }}
224                 onDialogOpen={this.handleCreationDialogOpen}
225                 onItemClick={item => {
226                     this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
227                 }}
228                 onItemDoubleClick={item => {
229                     this.props.dispatch<any>(setProjectItem(item.uuid, ItemMode.ACTIVE));
230                     this.props.dispatch<any>(loadDetails(item.uuid, ResourceKind.Project));
231                 }}
232                 {...props} />
233
234             renderFavoritePanel = (props: RouteComponentProps<{ id: string }>) => <FavoritePanel
235                 onItemRouteChange={itemId => this.props.dispatch<any>(setFavoriteItem(itemId, ItemMode.ACTIVE))}
236                 onContextMenu={(event, item) => {
237                     this.openContextMenu(event, {
238                         uuid: item.uuid,
239                         name: item.name,
240                         kind: ContextMenuKind.Favorite,
241                     });
242                 }}
243                 onDialogOpen={this.handleCreationDialogOpen}
244                 onItemClick={item => {
245                     this.props.dispatch<any>(loadDetails(item.uuid, item.kind as ResourceKind));
246                 }}
247                 onItemDoubleClick={item => {
248                     this.props.dispatch<any>(setFavoriteItem(item.uuid, ItemMode.ACTIVE));
249                     this.props.dispatch<any>(loadDetails(item.uuid, ResourceKind.Project));
250                 }}
251                 {...props} />
252
253             mainAppBarActions: MainAppBarActionProps = {
254                 onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
255                     this.props.dispatch<any>(setProjectItem(itemId, ItemMode.BOTH));
256                     this.props.dispatch<any>(loadDetails(itemId, ResourceKind.Project));
257                 },
258                 onSearch: searchText => {
259                     this.setState({ searchText });
260                     this.props.dispatch(push(`/search?q=${searchText}`));
261                 },
262                 onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
263                 onDetailsPanelToggle: () => {
264                     this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
265                 },
266                 onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: NavBreadcrumb) => {
267                     this.openContextMenu(event, {
268                         uuid: breadcrumb.itemId,
269                         name: breadcrumb.label,
270                         kind: ContextMenuKind.Project
271                     });
272                 }
273             };
274
275             toggleSidePanelOpen = (itemId: string) => {
276                 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
277             }
278
279             toggleSidePanelActive = (itemId: string) => {
280                 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
281                 this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
282                 const panelItem = this.props.sidePanelItems.find(it => it.id === itemId);
283                 if (panelItem && panelItem.activeAction) {
284                     panelItem.activeAction(this.props.dispatch);
285                 }
286             }
287
288             handleCreationDialogOpen = (itemUuid: string) => {
289                 this.props.dispatch(projectActions.OPEN_PROJECT_CREATOR({ ownerUuid: itemUuid }));
290             }
291
292             openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: { name: string; uuid: string; kind: ContextMenuKind; }) => {
293                 event.preventDefault();
294                 this.props.dispatch(
295                     contextMenuActions.OPEN_CONTEXT_MENU({
296                         position: { x: event.clientX, y: event.clientY },
297                         resource
298                     })
299                 );
300             }
301         }
302     )
303 );