Merge branch 'master'
[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: Breadcrumb[];
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             {
72                 props.user && <Breadcrumbs
73                     items={props.breadcrumbs}
74                     onClick={props.onBreadcrumbClick}
75                     onContextMenu={props.onContextMenu} />
76             }
77             { props.user && <IconButton color="inherit" onClick={props.onDetailsPanelToggle}>
78                     <DetailsIcon />
79                 </IconButton>
80             }
81         </Toolbar>
82     </AppBar>;
83 };
84
85 const renderMenuForUser = ({ user, menuItems, onMenuItemClick }: MainAppBarProps) => {
86     return (
87         <>
88             <IconButton color="inherit">
89                 <Badge badgeContent={3} color="primary">
90                     <NotificationIcon />
91                 </Badge>
92             </IconButton>
93             <DropdownMenu icon={<UserPanelIcon />} id="account-menu">
94                 <MenuItem>
95                     {getUserFullname(user)}
96                 </MenuItem>
97                 {renderMenuItems(menuItems.accountMenu, onMenuItemClick)}
98             </DropdownMenu>
99             <DropdownMenu icon={<HelpIcon />} id="help-menu">
100                 {renderMenuItems(menuItems.helpMenu, onMenuItemClick)}
101             </DropdownMenu>
102         </>
103     );
104 };
105
106 const renderMenuForAnonymous = ({ onMenuItemClick, menuItems }: MainAppBarProps) => {
107     return menuItems.anonymousMenu.map((item, index) => (
108         <Button key={index} color="inherit" onClick={() => onMenuItemClick(item)}>
109             {item.label}
110         </Button>
111     ));
112 };
113
114 const renderMenuItems = (menuItems: MainAppBarMenuItem[], onMenuItemClick: (menuItem: MainAppBarMenuItem) => void) => {
115     return menuItems.map((item, index) => (
116         <MenuItem key={index} onClick={() => onMenuItemClick(item)}>
117             {item.label}
118         </MenuItem>
119     ));
120 };