2021-01-31 21:55:25 -08:00
|
|
|
// This file is part of Moonfire NVR, a security camera network video recorder.
|
|
|
|
// Copyright (C) 2021 The Moonfire NVR Authors; see AUTHORS and LICENSE.txt.
|
|
|
|
// SPDX-License-Identifier: GPL-v3.0-or-later WITH GPL-3.0-linking-exception
|
|
|
|
|
2021-09-24 10:57:29 -07:00
|
|
|
import Button from "@mui/material/Button";
|
|
|
|
import IconButton from "@mui/material/IconButton";
|
|
|
|
import Menu from "@mui/material/Menu";
|
|
|
|
import MenuItem from "@mui/material/MenuItem";
|
|
|
|
import { Theme } from "@mui/material/styles";
|
|
|
|
import { createStyles, makeStyles } from "@mui/styles";
|
|
|
|
import Toolbar from "@mui/material/Toolbar";
|
|
|
|
import Typography from "@mui/material/Typography";
|
|
|
|
import AccountCircle from "@mui/icons-material/AccountCircle";
|
|
|
|
import MenuIcon from "@mui/icons-material/Menu";
|
2021-01-31 21:55:25 -08:00
|
|
|
import React from "react";
|
2021-03-26 13:43:04 -07:00
|
|
|
import { LoginState } from "./App";
|
2021-01-31 21:55:25 -08:00
|
|
|
import { Session } from "./types";
|
|
|
|
|
|
|
|
const useStyles = makeStyles((theme: Theme) =>
|
|
|
|
createStyles({
|
|
|
|
title: {
|
|
|
|
flexGrow: 1,
|
|
|
|
},
|
2021-03-26 13:43:04 -07:00
|
|
|
activity: {
|
|
|
|
marginRight: theme.spacing(2),
|
|
|
|
},
|
2021-01-31 21:55:25 -08:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
interface Props {
|
2021-03-26 13:43:04 -07:00
|
|
|
loginState: LoginState;
|
2021-01-31 21:55:25 -08:00
|
|
|
setSession: (session: Session | null) => void;
|
|
|
|
requestLogin: () => void;
|
|
|
|
logout: () => void;
|
2021-03-05 16:56:51 -08:00
|
|
|
menuClick?: () => void;
|
2021-03-26 13:43:04 -07:00
|
|
|
activityMenuPart: JSX.Element | null;
|
2021-01-31 21:55:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://material-ui.com/components/app-bar/
|
|
|
|
function MoonfireMenu(props: Props) {
|
|
|
|
const classes = useStyles();
|
2021-08-10 11:57:16 -07:00
|
|
|
const [accountMenuAnchor, setAccountMenuAnchor] =
|
|
|
|
React.useState<null | HTMLElement>(null);
|
2021-01-31 21:55:25 -08:00
|
|
|
|
|
|
|
const handleMenu = (event: React.MouseEvent<HTMLElement>) => {
|
|
|
|
setAccountMenuAnchor(event.currentTarget);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleClose = () => {
|
|
|
|
setAccountMenuAnchor(null);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleLogout = () => {
|
|
|
|
// Note this close should happen before `auth` toggles, or material-ui will
|
|
|
|
// be unhappy about the anchor element not being part of the layout.
|
|
|
|
handleClose();
|
|
|
|
props.logout();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2021-03-05 16:56:51 -08:00
|
|
|
<>
|
2021-01-31 21:55:25 -08:00
|
|
|
<Toolbar variant="dense">
|
2021-03-05 16:56:51 -08:00
|
|
|
<IconButton
|
|
|
|
edge="start"
|
|
|
|
color="inherit"
|
|
|
|
aria-label="menu"
|
|
|
|
onClick={props.menuClick}
|
|
|
|
>
|
2021-01-31 21:55:25 -08:00
|
|
|
<MenuIcon />
|
|
|
|
</IconButton>
|
|
|
|
<Typography variant="h6" className={classes.title}>
|
|
|
|
Moonfire NVR
|
|
|
|
</Typography>
|
2021-03-26 13:43:04 -07:00
|
|
|
{props.activityMenuPart !== null && (
|
|
|
|
<div className={classes.activity}>{props.activityMenuPart}</div>
|
|
|
|
)}
|
|
|
|
{props.loginState !== "unknown" && props.loginState !== "logged-in" && (
|
2021-01-31 21:55:25 -08:00
|
|
|
<Button color="inherit" onClick={props.requestLogin}>
|
|
|
|
Log in
|
|
|
|
</Button>
|
|
|
|
)}
|
2021-03-26 13:43:04 -07:00
|
|
|
{props.loginState === "logged-in" && (
|
2021-01-31 21:55:25 -08:00
|
|
|
<div>
|
|
|
|
<IconButton
|
|
|
|
aria-label="account of current user"
|
|
|
|
aria-controls="primary-search-account-menu"
|
|
|
|
aria-haspopup="true"
|
|
|
|
onClick={handleMenu}
|
|
|
|
color="inherit"
|
|
|
|
size="small"
|
|
|
|
>
|
|
|
|
<AccountCircle />
|
|
|
|
</IconButton>
|
|
|
|
<Menu
|
|
|
|
anchorEl={accountMenuAnchor}
|
|
|
|
keepMounted
|
|
|
|
anchorOrigin={{
|
|
|
|
vertical: "bottom",
|
|
|
|
horizontal: "right",
|
|
|
|
}}
|
|
|
|
transformOrigin={{
|
|
|
|
vertical: "top",
|
|
|
|
horizontal: "right",
|
|
|
|
}}
|
|
|
|
open={Boolean(accountMenuAnchor)}
|
|
|
|
onClose={handleClose}
|
|
|
|
>
|
|
|
|
<MenuItem onClick={handleLogout}>Logout</MenuItem>
|
|
|
|
</Menu>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Toolbar>
|
2021-03-05 16:56:51 -08:00
|
|
|
</>
|
2021-01-31 21:55:25 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MoonfireMenu;
|