All files / app/features/messages MessagesHeader.tsx

83.33% Statements 25/30
0% Branches 0/3
50% Functions 3/6
86.2% Lines 25/29

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 1061x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x   1x   1x         1x         1x       1x 3x 3x                 1x 3x 3x                 1x 3x 3x                 1x                                                                                      
import { TabContext } from "@mui/lab";
import { styled } from "@mui/material";
import HtmlMeta from "components/HtmlMeta";
import NotificationBadge from "components/NotificationBadge";
import PageTitle from "components/PageTitle";
import TabBar from "components/TabBar";
import MarkAllReadButton from "features/messages/requests/MarkAllReadButton";
import { useTranslation } from "i18n";
import { MESSAGES } from "i18n/namespaces";
import { useRouter } from "next/router";
import { ReactNode } from "react";
import { messagesRoute, MessageType } from "routes";
 
import useNotifications from "../useNotifications";
 
const StyledRoot = styled("div")(({ theme }) => ({
  paddingLeft: theme.spacing(2),
  paddingRight: theme.spacing(2),
}));
 
const StyledTabBarContainer = styled("div")({
  display: "flex",
  justifyContent: "flex-start",
});
 
const StyledLabelWrapper = styled("span")({
  paddingRight: "1.8rem", // visually compensate for NotificationBadge's right offset
});
 
export function MessagesNotification() {
  const { t } = useTranslation(MESSAGES);
  const { data } = useNotifications();
 
  return (
    <NotificationBadge count={data?.unseenMessageCount}>
      {t("messages_page.tabs.chats")}
    </NotificationBadge>
  );
}
 
export function HostRequestsReceivedNotification() {
  const { t } = useTranslation(MESSAGES);
  const { data } = useNotifications();
 
  return (
    <NotificationBadge count={data?.unseenReceivedHostRequestCount}>
      {t("messages_page.tabs.hosting")}
    </NotificationBadge>
  );
}
 
export function HostRequestsSentNotification() {
  const { t } = useTranslation(MESSAGES);
  const { data } = useNotifications();
 
  return (
    <NotificationBadge count={data?.unseenSentHostRequestCount}>
      {t("messages_page.tabs.surfing")}
    </NotificationBadge>
  );
}
 
const labels: Record<MessageType, ReactNode> = {
  chats: (
    <StyledLabelWrapper>
      <MessagesNotification />
    </StyledLabelWrapper>
  ),
  hosting: (
    <StyledLabelWrapper>
      <HostRequestsReceivedNotification />
    </StyledLabelWrapper>
  ),
  surfing: (
    <StyledLabelWrapper>
      <HostRequestsSentNotification />
    </StyledLabelWrapper>
  ),
};
 
export default function MessagesHeader({
  tab,
}: {
  tab: MessageType | undefined;
}) {
  const { t } = useTranslation(MESSAGES);
  const router = useRouter();
 
  return (
    <StyledRoot>
      <HtmlMeta title={t("messages_page.title")} />
      <PageTitle>{t("messages_page.title")}</PageTitle>
      {tab && <MarkAllReadButton type={tab} />}
      <StyledTabBarContainer>
        <TabContext value={tab ?? ""}>
          <TabBar
            ariaLabel={t("messages_page.tabs.aria_label")}
            setValue={(newTab) => router.push(`${messagesRoute}/${newTab}`)}
            labels={labels}
          />
        </TabContext>
      </StyledTabBarContainer>
    </StyledRoot>
  );
}