All files / app/components/Navigation LoggedInMenu.tsx

0% Statements 0/36
0% Branches 0/2
0% Functions 0/11
0% Lines 0/30

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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144                                                                                                                                                                                                                                                                                               
import { NotificationsOutlined } from "@mui/icons-material";
import { Button, styled, Tooltip } from "@mui/material";
import Avatar from "components/Avatar";
import IconButton from "components/IconButton";
import { MenuIcon } from "components/Icons";
import Menu from "components/Menu";
import NotificationBadge from "components/NotificationBadge";
import NotificationsFeed from "features/notifications/NotificationsFeed/NotificationsFeed";
import useCurrentUser from "features/userQueries/useCurrentUser";
import { useTranslation } from "i18n";
import { GLOBAL } from "i18n/namespaces";
import { PingRes } from "proto/api_pb";
import React, { Dispatch, ReactNode, SetStateAction, useState } from "react";
import { theme } from "theme";
 
import ReportButton from "./ReportButton";
 
const StyledMenu = styled(Menu)(({ theme }) => ({
  "& .MuiPaper-root": {
    boxShadow: theme.shadows[1],
    minWidth: "12rem",
  },
 
  "& .MuiPopover-root": {
    transform: "translateY(1rem)",
  },
}));
 
const StyledMenuButton = styled(Button)(({ theme }) => ({
  display: "flex",
  flexDirection: "row",
  alignItems: "center",
  border: `1px solid ${theme.palette.grey[300]}`,
  borderRadius: 999,
  backgroundColor: theme.palette.grey[200],
  padding: theme.spacing(1),
  transition: `${theme.transitions.duration.short}ms ${theme.transitions.easing.easeInOut}`,
  "&:hover": {
    opacity: 0.8,
    backgroundColor: theme.palette.grey[300],
  },
}));
 
const StyledAvatar = styled(Avatar)(({ theme }) => ({
  height: "2rem",
  width: "2rem",
  marginLeft: theme.spacing(1),
}));
 
const ReportButtonContainer = styled("div")(({ theme }) => ({
  padding: theme.spacing(2),
}));
 
const NotificationMenuItemWrapper = styled("div")(({ theme }) => ({
  marginRight: theme.spacing(4),
}));
 
export default function LoggedInMenu({
  menuOpen,
  notificationCount,
  setMenuOpen,
  children,
}: {
  menuOpen: boolean;
  notificationCount: PingRes.AsObject["unseenNotificationCount"] | undefined;
  setMenuOpen: Dispatch<SetStateAction<boolean>>;
  children: ReactNode;
}) {
  const menuRef = React.useRef<HTMLButtonElement>(null);
  const { data: user } = useCurrentUser();
  const { t } = useTranslation([GLOBAL]);
 
  const [notificationsAnchorEl, setNotificationsAnchorEl] =
    useState<HTMLButtonElement | null>(null);
  const isNotificationsFeedOpen = Boolean(notificationsAnchorEl);
 
  const handleNotificationsFeedOpen = (
    event: React.MouseEvent<HTMLButtonElement>,
  ) => {
    setNotificationsAnchorEl(event.currentTarget);
  };
 
  const handleNotificationsFeedClose = () => {
    setNotificationsAnchorEl(null);
  };
 
  return (
    <>
      <ReportButtonContainer>
        <ReportButton />
      </ReportButtonContainer>
      <Tooltip title={t("global:nav.notifications")}>
        <NotificationMenuItemWrapper>
          <NotificationBadge count={notificationCount}>
            <IconButton
              id="notifications-feed-button"
              onClick={handleNotificationsFeedOpen}
              aria-label={t("global:nav.notifications")}
              aria-controls="notifications-feed"
              aria-haspopup="true"
              aria-expanded={isNotificationsFeedOpen ? "true" : undefined}
              sx={{
                backgroundColor: theme.palette.grey[300],
                "&:hover": {
                  opacity: 0.8,
                  backgroundColor: theme.palette.grey[300],
                },
              }}
            >
              <NotificationsOutlined />
            </IconButton>
          </NotificationBadge>
        </NotificationMenuItemWrapper>
      </Tooltip>
      <NotificationsFeed
        isOpen={isNotificationsFeedOpen}
        anchorEl={notificationsAnchorEl}
        onClose={handleNotificationsFeedClose}
      />
      <StyledMenuButton
        aria-controls="navigation-menu"
        aria-haspopup="true"
        onClick={() => setMenuOpen((prevMenuOpen: boolean) => !prevMenuOpen)}
        ref={menuRef}
      >
        <MenuIcon sx={{ color: theme.palette.text.primary }} />
        <StyledAvatar user={user} isProfileLink={false} />
      </StyledMenuButton>
      <StyledMenu
        id="navigation-menu"
        open={menuOpen}
        anchorEl={menuRef.current}
        onClose={() => setMenuOpen(false)}
        anchorOrigin={{
          vertical: "bottom",
          horizontal: "right",
        }}
      >
        {children}
      </StyledMenu>
    </>
  );
}