All files / components Navbar.tsx

100% Statements 15/15
80% Branches 4/5
100% Functions 4/4
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104    1x                       1x 1x 1x 1x   6x 8x 8x   8x 1x     8x 1x     8x 1x 1x                                                                                                                                            
"use client";
 
import { signOut, useSession } from "next-auth/react";
import {
  AppBar,
  Toolbar,
  Typography,
  Button,
  IconButton,
  Avatar,
  Menu,
  MenuItem,
  Box,
  Container,
} from "@mui/material";
import Link from "next/link";
import { useState } from "react";
import AddIcon from "@mui/icons-material/Add";
 
export default function Navbar() {
  const { data: session } = useSession();
  const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
 
  const handleMenuOpen = (event: React.MouseEvent<HTMLElement>) => {
    setAnchorEl(event.currentTarget);
  };
 
  const handleMenuClose = () => {
    setAnchorEl(null);
  };
 
  const handleSignOut = () => {
    handleMenuClose();
    signOut({ callbackUrl: "/auth/signin" });
  };
 
  return (
    <AppBar position="sticky">
      <Container maxWidth="lg">
        <Toolbar disableGutters>
          <Typography
            variant="h6"
            component={Link}
            href="/anotoki"
            sx={{
              mr: 2,
              fontWeight: 700,
              textDecoration: "none",
              color: "inherit",
            }}
          >
            anotoki
          </Typography>
 
          <Box sx={{ flexGrow: 1 }} />
 
          {session && (
            <>
              <Button
                component={Link}
                href="/posts/new"
                color="inherit"
                startIcon={<AddIcon />}
                sx={{ mr: 2 }}
              >
                投稿する
              </Button>
 
              <IconButton onClick={handleMenuOpen} size="small">
                <Avatar
                  src={session.user?.image || undefined}
                  alt={session.user?.name || "User"}
                  sx={{ width: 32, height: 32 }}
                />
              </IconButton>
 
              <Menu
                anchorEl={anchorEl}
                open={Boolean(anchorEl)}
                onClose={handleMenuClose}
                anchorOrigin={{
                  vertical: "bottom",
                  horizontal: "right",
                }}
                transformOrigin={{
                  vertical: "top",
                  horizontal: "right",
                }}
              >
                <MenuItem disabled>
                  <Typography variant="body2" color="text.secondary">
                    {session.user?.email}
                  </Typography>
                </MenuItem>
                <MenuItem onClick={handleSignOut}>サインアウト</MenuItem>
              </Menu>
            </>
          )}
        </Toolbar>
      </Container>
    </AppBar>
  );
}