change chip styles
[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 { login, logout } 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, setProjectItem } from "../../store/navigation/navigation-action";
22 import { projectActions } from "../../store/project/project-action";
23 import { collectionCreateActions } from '../../store/collections/creator/collection-creator-action';
24 import { ProjectPanel } from "../project-panel/project-panel";
25 import { DetailsPanel } from '../../views-components/details-panel/details-panel';
26 import { ArvadosTheme } from '../../common/custom-theme';
27 import { CreateProjectDialog } from "../../views-components/create-project-dialog/create-project-dialog";
28
29 import { detailsPanelActions, loadDetails } from "../../store/details-panel/details-panel-action";
30 import { contextMenuActions } from "../../store/context-menu/context-menu-actions";
31 import { 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 import { CurrentTokenDialog } from '../../views-components/current-token-dialog/current-token-dialog';
37 import { dataExplorerActions } from '../../store/data-explorer/data-explorer-action';
38 import { Snackbar } from '../../views-components/snackbar/snackbar';
39 import { favoritePanelActions } from '../../store/favorite-panel/favorite-panel-action';
40 import { CreateCollectionDialog } from '../../views-components/create-collection-dialog/create-collection-dialog';
41 import { CollectionPanel } from '../collection-panel/collection-panel';
42 import { loadCollection, loadCollectionTags } from '../../store/collection-panel/collection-panel-action';
43 import { getCollectionUrl } from '../../models/collection';
44 import { UpdateCollectionDialog } from '../../views-components/update-collection-dialog/update-collection-dialog.';
45 import { AuthService } from "../../services/auth-service/auth-service";
46
47 const DRAWER_WITDH = 240;
48 const APP_BAR_HEIGHT = 100;
49
50 type CssRules = 'root' | 'appBar' | 'drawerPaper' | 'content' | 'contentWrapper' | 'toolbar';
51
52 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
53     root: {
54         flexGrow: 1,
55         zIndex: 1,
56         overflow: 'hidden',
57         position: 'relative',
58         display: 'flex',
59         width: '100vw',
60         height: '100vh'
61     },
62     appBar: {
63         zIndex: theme.zIndex.drawer + 1,
64         position: "absolute",
65         width: "100%"
66     },
67     drawerPaper: {
68         position: 'relative',
69         width: DRAWER_WITDH,
70         display: 'flex',
71         flexDirection: 'column',
72     },
73     contentWrapper: {
74         backgroundColor: theme.palette.background.default,
75         display: "flex",
76         flexGrow: 1,
77         minWidth: 0,
78         paddingTop: APP_BAR_HEIGHT
79     },
80     content: {
81         padding: `${theme.spacing.unit}px ${theme.spacing.unit * 3}px`,
82         overflowY: "auto",
83         flexGrow: 1
84     },
85     toolbar: theme.mixins.toolbar
86 });
87
88 interface WorkbenchDataProps {
89     projects: Array<TreeItem<ProjectResource>>;
90     currentProjectId: string;
91     user?: User;
92     currentToken?: string;
93     sidePanelItems: SidePanelItem[];
94 }
95
96 interface WorkbenchServiceProps {
97     authService: AuthService;
98 }
99
100 interface WorkbenchActionProps {
101 }
102
103 type WorkbenchProps = WorkbenchDataProps & WorkbenchServiceProps & WorkbenchActionProps & DispatchProp<any> & WithStyles<CssRules>;
104
105 interface NavBreadcrumb extends Breadcrumb {
106     itemId: string;
107 }
108
109 interface NavMenuItem extends MainAppBarMenuItem {
110     action: () => void;
111 }
112
113 interface WorkbenchState {
114     isCurrentTokenDialogOpen: boolean;
115     anchorEl: any;
116     searchText: string;
117     menuItems: {
118         accountMenu: NavMenuItem[],
119         helpMenu: NavMenuItem[],
120         anonymousMenu: NavMenuItem[]
121     };
122 }
123
124 export const Workbench = withStyles(styles)(
125     connect<WorkbenchDataProps>(
126         (state: RootState) => ({
127             projects: state.projects.items,
128             currentProjectId: state.projects.currentItemId,
129             user: state.auth.user,
130             currentToken: state.auth.apiToken,
131             sidePanelItems: state.sidePanel
132         })
133     )(
134         class extends React.Component<WorkbenchProps, WorkbenchState> {
135             state = {
136                 isCreationDialogOpen: false,
137                 isCurrentTokenDialogOpen: false,
138                 anchorEl: null,
139                 searchText: "",
140                 breadcrumbs: [],
141                 menuItems: {
142                     accountMenu: [
143                         {
144                             label: 'Current token',
145                             action: () => this.toggleCurrentTokenModal()
146                         },
147                         {
148                             label: "Logout",
149                             action: () => this.props.dispatch(logout())
150                         },
151                         {
152                             label: "My account",
153                             action: () => this.props.dispatch(push("/my-account"))
154                         }
155                     ],
156                     helpMenu: [
157                         {
158                             label: "Help",
159                             action: () => this.props.dispatch(push("/help"))
160                         }
161                     ],
162                     anonymousMenu: [
163                         {
164                             label: "Sign in",
165                             action: () => this.props.dispatch(login())
166                         }
167                     ]
168                 }
169             };
170
171             render() {
172                 const path = getTreePath(this.props.projects, this.props.currentProjectId);
173                 const breadcrumbs = path.map(item => ({
174                     label: item.data.name,
175                     itemId: item.data.uuid,
176                     status: item.status
177                 }));
178
179                 const { classes, user } = this.props;
180                 return (
181                     <div className={classes.root}>
182                         <div className={classes.appBar}>
183                             <MainAppBar
184                                 breadcrumbs={breadcrumbs}
185                                 searchText={this.state.searchText}
186                                 user={this.props.user}
187                                 menuItems={this.state.menuItems}
188                                 {...this.mainAppBarActions} />
189                         </div>
190                         {user &&
191                             <Drawer
192                                 variant="permanent"
193                                 classes={{
194                                     paper: classes.drawerPaper,
195                                 }}>
196                                 <div className={classes.toolbar} />
197                                 <SidePanel
198                                     toggleOpen={this.toggleSidePanelOpen}
199                                     toggleActive={this.toggleSidePanelActive}
200                                     sidePanelItems={this.props.sidePanelItems}
201                                     onContextMenu={(event) => this.openContextMenu(event, {
202                                         uuid: this.props.authService.getUuid() || "",
203                                         name: "",
204                                         kind: ContextMenuKind.ROOT_PROJECT
205                                     })}>
206                                     <ProjectTree
207                                         projects={this.props.projects}
208                                         toggleOpen={itemId => this.props.dispatch(setProjectItem(itemId, ItemMode.OPEN))}
209                                         onContextMenu={(event, item) => this.openContextMenu(event, {
210                                             uuid: item.data.uuid,
211                                             name: item.data.name,
212                                             kind: ContextMenuKind.PROJECT
213                                         })}
214                                         toggleActive={itemId => {
215                                             this.props.dispatch(setProjectItem(itemId, ItemMode.ACTIVE));
216                                             this.props.dispatch(loadDetails(itemId, ResourceKind.PROJECT));
217                                             this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
218                                         }} />
219                                 </SidePanel>
220                             </Drawer>}
221                         <main className={classes.contentWrapper}>
222                             <div className={classes.content}>
223                                 <Switch>
224                                     <Route path="/projects/:id" render={this.renderProjectPanel} />
225                                     <Route path="/favorites" render={this.renderFavoritePanel} />
226                                     <Route path="/collections/:id" render={this.renderCollectionPanel} />
227                                 </Switch>
228                             </div>
229                             {user && <DetailsPanel />}
230                         </main>
231                         <ContextMenu />
232                         <Snackbar />
233                         <CreateProjectDialog />
234                         <CreateCollectionDialog />
235                         <UpdateCollectionDialog />
236                         <CurrentTokenDialog
237                             currentToken={this.props.currentToken}
238                             open={this.state.isCurrentTokenDialogOpen}
239                             handleClose={this.toggleCurrentTokenModal} />
240                     </div>
241                 );
242             }
243
244             renderCollectionPanel = (props: RouteComponentProps<{ id: string }>) => <CollectionPanel 
245                 onItemRouteChange={(collectionId) => {
246                     this.props.dispatch<any>(loadCollection(collectionId, ResourceKind.COLLECTION));
247                     this.props.dispatch<any>(loadCollectionTags(collectionId));
248                 }}
249                 onContextMenu={(event, item) => {
250                     this.openContextMenu(event, {
251                         uuid: item.uuid,
252                         name: item.name,
253                         kind: ContextMenuKind.COLLECTION
254                     });
255                 }}
256                 {...props} />
257
258             renderProjectPanel = (props: RouteComponentProps<{ id: string }>) => <ProjectPanel
259                 onItemRouteChange={itemId => this.props.dispatch(setProjectItem(itemId, ItemMode.ACTIVE))}
260                 onContextMenu={(event, item) => {
261
262                     const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
263                     this.openContextMenu(event, {
264                         uuid: item.uuid,
265                         name: item.name,
266                         kind
267                     });
268                 }}
269                 onProjectCreationDialogOpen={this.handleProjectCreationDialogOpen}
270                 onCollectionCreationDialogOpen={this.handleCollectionCreationDialogOpen}
271                 onItemClick={item => {
272                     this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
273                 }}
274                 onItemDoubleClick={item => {
275                     switch (item.kind) {
276                         case ResourceKind.COLLECTION:
277                             this.props.dispatch(loadCollection(item.uuid, item.kind as ResourceKind));
278                             this.props.dispatch(push(getCollectionUrl(item.uuid)));
279                         default: 
280                             this.props.dispatch(setProjectItem(item.uuid, ItemMode.ACTIVE));
281                             this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
282                     }
283
284                 }}
285                 {...props} />
286
287             renderFavoritePanel = (props: RouteComponentProps<{ id: string }>) => <FavoritePanel
288                 onItemRouteChange={() => this.props.dispatch(favoritePanelActions.REQUEST_ITEMS())}
289                 onContextMenu={(event, item) => {
290                     const kind = item.kind === ResourceKind.PROJECT ? ContextMenuKind.PROJECT : ContextMenuKind.RESOURCE;
291                     this.openContextMenu(event, {
292                         uuid: item.uuid,
293                         name: item.name,
294                         kind,
295                     });
296                 }}
297                 onDialogOpen={this.handleProjectCreationDialogOpen}
298                 onItemClick={item => {
299                     this.props.dispatch(loadDetails(item.uuid, item.kind as ResourceKind));
300                 }}
301                 onItemDoubleClick={item => {
302                     switch (item.kind) {
303                         case ResourceKind.COLLECTION:
304                             this.props.dispatch(loadCollection(item.uuid, item.kind as ResourceKind));
305                             this.props.dispatch(push(getCollectionUrl(item.uuid)));
306                         default:
307                             this.props.dispatch(loadDetails(item.uuid, ResourceKind.PROJECT));
308                             this.props.dispatch(setProjectItem(item.uuid, ItemMode.ACTIVE));
309                             this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
310                     }
311
312                 }}
313                 {...props} />
314
315             mainAppBarActions: MainAppBarActionProps = {
316                 onBreadcrumbClick: ({ itemId }: NavBreadcrumb) => {
317                     this.props.dispatch(setProjectItem(itemId, ItemMode.BOTH));
318                     this.props.dispatch(loadDetails(itemId, ResourceKind.PROJECT));
319                 },
320                 onSearch: searchText => {
321                     this.setState({ searchText });
322                     this.props.dispatch(push(`/search?q=${searchText}`));
323                 },
324                 onMenuItemClick: (menuItem: NavMenuItem) => menuItem.action(),
325                 onDetailsPanelToggle: () => {
326                     this.props.dispatch(detailsPanelActions.TOGGLE_DETAILS_PANEL());
327                 },
328                 onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: NavBreadcrumb) => {
329                     this.openContextMenu(event, {
330                         uuid: breadcrumb.itemId,
331                         name: breadcrumb.label,
332                         kind: ContextMenuKind.PROJECT
333                     });
334                 }
335             };
336
337             toggleSidePanelOpen = (itemId: string) => {
338                 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(itemId));
339             }
340
341             toggleSidePanelActive = (itemId: string) => {
342                 this.props.dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(itemId));
343                 this.props.dispatch(projectActions.RESET_PROJECT_TREE_ACTIVITY(itemId));
344                 const panelItem = this.props.sidePanelItems.find(it => it.id === itemId);
345                 if (panelItem && panelItem.activeAction) {
346                     panelItem.activeAction(this.props.dispatch);
347                 }
348             }
349
350             handleProjectCreationDialogOpen = (itemUuid: string) => {
351                 this.props.dispatch(projectActions.OPEN_PROJECT_CREATOR({ ownerUuid: itemUuid }));
352             }
353
354             handleCollectionCreationDialogOpen = (itemUuid: string) => {
355                 this.props.dispatch(collectionCreateActions.OPEN_COLLECTION_CREATOR({ ownerUuid: itemUuid }));
356             }
357
358             openContextMenu = (event: React.MouseEvent<HTMLElement>, resource: { name: string; uuid: string; kind: ContextMenuKind; }) => {
359                 event.preventDefault();
360                 this.props.dispatch(
361                     contextMenuActions.OPEN_CONTEXT_MENU({
362                         position: { x: event.clientX, y: event.clientY },
363                         resource
364                     })
365                 );
366             }
367
368             toggleCurrentTokenModal = () => {
369                 this.setState({ isCurrentTokenDialogOpen: !this.state.isCurrentTokenDialogOpen });
370             }
371         }
372     )
373 );