Restore context menu in side bar and breadcrumbs
[arvados-workbench2.git] / src / views-components / main-app-bar / main-app-bar.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 { AppBar, Toolbar, Typography, Grid, IconButton, Badge, Button, MenuItem } from "@material-ui/core";
7 import { User, getUserFullname } from "~/models/user";
8 import { SearchBar } from "~/components/search-bar/search-bar";
9 import { Breadcrumbs, Breadcrumb } from "~/components/breadcrumbs/breadcrumbs";
10 import { DropdownMenu } from "~/components/dropdown-menu/dropdown-menu";
11 import { DetailsIcon, NotificationIcon, UserPanelIcon, HelpIcon } from "~/components/icon/icon";
12
13 export interface MainAppBarMenuItem {
14     label: string;
15 }
16
17 export interface MainAppBarMenuItems {
18     accountMenu: MainAppBarMenuItem[];
19     helpMenu: MainAppBarMenuItem[];
20     anonymousMenu: MainAppBarMenuItem[];
21 }
22
23 interface MainAppBarDataProps {
24     searchText: string;
25     searchDebounce?: number;
26     breadcrumbs: React.ComponentType<any>;
27     user?: User;
28     menuItems: MainAppBarMenuItems;
29     buildInfo: string;
30 }
31
32 export interface MainAppBarActionProps {
33     onSearch: (searchText: string) => void;
34     onBreadcrumbClick: (breadcrumb: Breadcrumb) => void;
35     onMenuItemClick: (menuItem: MainAppBarMenuItem) => void;
36     onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: Breadcrumb) => void;
37     onDetailsPanelToggle: () => void;
38 }
39
40 type MainAppBarProps = MainAppBarDataProps & MainAppBarActionProps;
41
42 export const MainAppBar: React.SFC<MainAppBarProps> = (props) => {
43     return <AppBar position="static">
44         <Toolbar>
45             <Grid container justify="space-between">
46                 <Grid item xs={3}>
47                     <Typography variant="headline" color="inherit" noWrap>
48                         Arvados 2
49                     </Typography>
50                     <Typography variant="body1" color="inherit" noWrap >
51                         {props.buildInfo}
52                     </Typography>
53                 </Grid>
54                 <Grid item xs={6} container alignItems="center">
55                     {
56                         props.user && <SearchBar
57                             value={props.searchText}
58                             onSearch={props.onSearch}
59                             debounce={props.searchDebounce}
60                         />
61                     }
62                 </Grid>
63                 <Grid item xs={3} container alignItems="center" justify="flex-end">
64                     {
65                         props.user ? renderMenuForUser(props) : renderMenuForAnonymous(props)
66                     }
67                 </Grid>
68             </Grid>
69         </Toolbar>
70         <Toolbar >
71             {props.user && <props.breadcrumbs />}
72             {props.user && <IconButton color="inherit" onClick={props.onDetailsPanelToggle}>
73                 <DetailsIcon />
74             </IconButton>
75             }
76         </Toolbar>
77     </AppBar>;
78 };
79
80 const renderMenuForUser = ({ user, menuItems, onMenuItemClick }: MainAppBarProps) => {
81     return (
82         <>
83             <IconButton color="inherit">
84                 <Badge badgeContent={3} color="primary">
85                     <NotificationIcon />
86                 </Badge>
87             </IconButton>
88             <DropdownMenu icon={<UserPanelIcon />} id="account-menu">
89                 <MenuItem>
90                     {getUserFullname(user)}
91                 </MenuItem>
92                 {renderMenuItems(menuItems.accountMenu, onMenuItemClick)}
93             </DropdownMenu>
94             <DropdownMenu icon={<HelpIcon />} id="help-menu">
95                 {renderMenuItems(menuItems.helpMenu, onMenuItemClick)}
96             </DropdownMenu>
97         </>
98     );
99 };
100
101 const renderMenuForAnonymous = ({ onMenuItemClick, menuItems }: MainAppBarProps) => {
102     return menuItems.anonymousMenu.map((item, index) => (
103         <Button key={index} color="inherit" onClick={() => onMenuItemClick(item)}>
104             {item.label}
105         </Button>
106     ));
107 };
108
109 const renderMenuItems = (menuItems: MainAppBarMenuItem[], onMenuItemClick: (menuItem: MainAppBarMenuItem) => void) => {
110     return menuItems.map((item, index) => (
111         <MenuItem key={index} onClick={() => onMenuItemClick(item)}>
112             {item.label}
113         </MenuItem>
114     ));
115 };