All files / src/components/Header MobileNavLinks.tsx

0% Statements 0/16
0% Branches 0/8
0% Functions 0/7
0% Lines 0/16

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                                                                                                                                                                                                                                                                             
import {
  Accordion,
  AccordionItem,
  AccordionButton,
  AccordionPanel,
  AccordionIcon,
  Box,
  Flex,
  Heading,
} from "@chakra-ui/react";
import { Fragment, FunctionComponent } from "react";
import { useAnalytics } from "../../contexts/Analytics";
import { clickEvent, eventName } from "../../contexts/Analytics/util";
import { ExternalLink } from "../ExternalLink";
import { NavLink } from "../NavLink";
import type { IMenuItems, ILink } from "../NavPopover";
 
const linkProps = {
  color: "link",
  py: 2,
  w: "full",
};
 
const Link: FunctionComponent<ILink & { "data-event"?: string }> = ({
  "data-event": dataEvent,
  display,
  isNavLink,
  url,
}) => {
  const event = dataEvent ? eventName(dataEvent, "Link", display) : undefined;
 
  return isNavLink ? (
    <NavLink data-event={event} to={url} {...linkProps}>
      {display}
    </NavLink>
  ) : (
    <ExternalLink
      alignItems="center"
      data-event={event}
      display="flex"
      hasWarning={false}
      href={url}
      justifyContent="space-between"
      {...linkProps}
    >
      {display}
    </ExternalLink>
  );
};
 
interface MobileNavLinksContentProps {
  dataEvent?: string;
  testId?: string;
  title: string;
  items: IMenuItems;
}
 
const MobileNavLinksContent: FunctionComponent<MobileNavLinksContentProps> = ({
  title,
  items,
  testId,
  dataEvent,
}) => {
  const { trackCustomEvent } = useAnalytics();
  return (
    <AccordionItem data-testid={testId} w="full">
      <AccordionButton
        onClick={() => {
          if (dataEvent) {
            trackCustomEvent(
              clickEvent({ name: eventName(dataEvent, "Menu") })
            );
          }
        }}
        px={0}
        py={4}
      >
        <Box flex="1" textAlign="left">
          <Heading as="h3" color="textPrimary" size="sm">
            {title}
          </Heading>
        </Box>
 
        <AccordionIcon />
      </AccordionButton>
 
      <AccordionPanel p={0} w="full">
        <Flex direction="column" w="full">
          {items.map((item, itemIdx) => {
            if ("links" in item) {
              return (
                <Fragment key={`${item.display}-${itemIdx}`}>
                  <Heading as="h4" borderBottom="base" pb={2} pt={4} size="xs">
                    {item.display}
                  </Heading>
 
                  {item.links.map((link, linkIdx) => (
                    <Link
                      data-event={dataEvent}
                      {...link}
                      key={`${link.display}-${linkIdx}`}
                    />
                  ))}
                </Fragment>
              );
            }
 
            return (
              <Link
                data-event={dataEvent}
                {...item}
                key={`${item.display}-${itemIdx}`}
              />
            );
          })}
        </Flex>
      </AccordionPanel>
    </AccordionItem>
  );
};
 
interface MobileNavLinksProps {
  sections: MobileNavLinksContentProps[];
}
 
export const MobileNavLinks: FunctionComponent<MobileNavLinksProps> = ({
  sections,
}) => (
  <Accordion allowMultiple allowToggle w="full">
    {sections.map((section, idx) => (
      <MobileNavLinksContent key={`section-${idx}`} {...section} />
    ))}
  </Accordion>
);