refs #master Merge branch 'origin/master' into 14007-ts-paths
[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 }
30
31 export interface MainAppBarActionProps {
32     onSearch: (searchText: string) => void;
33     onBreadcrumbClick: (breadcrumb: Breadcrumb) => void;
34     onMenuItemClick: (menuItem: MainAppBarMenuItem) => void;
35     onContextMenu: (event: React.MouseEvent<HTMLElement>, breadcrumb: Breadcrumb) => void;
36     onDetailsPanelToggle: () => void;
37 }
38
39 type MainAppBarProps = MainAppBarDataProps & MainAppBarActionProps;
40
41 export const MainAppBar: React.SFC<MainAppBarProps> = (props) => {
42     return <AppBar position="static">
43         <Toolbar>
44             <Grid container justify="space-between">
45                 <Grid item xs={3}>
46                     <Typography variant="headline" color="inherit" noWrap>
47                         Arvados
48                     </Typography>
49                     <Typography variant="body1" color="inherit" noWrap >
50                         Workbench 2
51                     </Typography>
52                 </Grid>
53                 <Grid item xs={6} container alignItems="center">
54                     {
55                         props.user && <SearchBar
56                             value={props.searchText}
57                             onSearch={props.onSearch}
58                             debounce={props.searchDebounce}
59                         />
60                     }
61                 </Grid>
62                 <Grid item xs={3} container alignItems="center" justify="flex-end">
63                     {
64                         props.user ? renderMenuForUser(props) : renderMenuForAnonymous(props)
65                     }
66                 </Grid>
67             </Grid>
68         </Toolbar>
69         <Toolbar >
70             {
71                 props.user && <Breadcrumbs
72                     items={props.breadcrumbs}
73                     onClick={props.onBreadcrumbClick}
74                     onContextMenu={props.onContextMenu} />
75             }
76             { props.user && <IconButton color="inherit" onClick={props.onDetailsPanelToggle}>
77                     <DetailsIcon />
78                 </IconButton>
79             }
80         </Toolbar>
81     </AppBar>;
82 };
83
84 const renderMenuForUser = ({ user, menuItems, onMenuItemClick }: MainAppBarProps) => {
85     return (
86         <>
87             <IconButton color="inherit">
88                 <Badge badgeContent={3} color="primary">
89                     <NotificationIcon />
90                 </Badge>
91             </IconButton>
92             <DropdownMenu icon={<UserPanelIcon />} id="account-menu">
93                 <MenuItem>
94                     {getUserFullname(user)}
95                 </MenuItem>
96                 {renderMenuItems(menuItems.accountMenu, onMenuItemClick)}
97             </DropdownMenu>
98             <DropdownMenu icon={<HelpIcon />} id="help-menu">
99                 {renderMenuItems(menuItems.helpMenu, onMenuItemClick)}
100             </DropdownMenu>
101         </>
102     );
103 };
104
105 const renderMenuForAnonymous = ({ onMenuItemClick, menuItems }: MainAppBarProps) => {
106     return menuItems.anonymousMenu.map((item, index) => (
107         <Button key={index} color="inherit" onClick={() => onMenuItemClick(item)}>
108             {item.label}
109         </Button>
110     ));
111 };
112
113 const renderMenuItems = (menuItems: MainAppBarMenuItem[], onMenuItemClick: (menuItem: MainAppBarMenuItem) => void) => {
114     return menuItems.map((item, index) => (
115         <MenuItem key={index} onClick={() => onMenuItemClick(item)}>
116             {item.label}
117         </MenuItem>
118     ));
119 };