import { ChevronDownIcon, ChevronRightIcon } from "@chakra-ui/icons"; import { Box, Flex, IconButton, useDisclosure } from "@chakra-ui/react"; import { FunctionComponent, useMemo } from "react"; import { NavItemWrapper } from "./NavItemWrapper"; import type { GetIsActiveItemFunction, NavItemConfig } from "./types"; import { clickEvent, eventName, useAnalytics } from "../../contexts/Analytics"; const navTreeEvent = (scope: string, event: string) => eventName(scope, "NavTree", event); const iconProps = { color: "textTertiary", h: 4, w: 4, }; export interface NavItemProps extends NavItemConfig { // The following props don't need to be explicitly defined - they are passed internally getIsActiveItem?: GetIsActiveItemFunction; onOpen?: () => void; level: number; variant?: "sm" | "md"; } export const NavItem: FunctionComponent = ({ children, "data-event": dataEvent, getIsActiveItem, id, title, path, level, onOpen, variant, }) => { const { trackCustomEvent } = useAnalytics(); const defaultIsOpen = level < 2; // only show first two levels by default const disclosure = useDisclosure({ onOpen, defaultIsOpen }); const linkIsActive = getIsActiveItem?.({ id, title, path, children, }) ?? false; const showToggle = (children?.length ?? 0) > 0; const showChildren = disclosure.isOpen && showToggle; const nestedItems = useMemo( () => children?.map((item, idx) => { return ( ); }), [children, dataEvent, getIsActiveItem, level, disclosure.onOpen, variant] ); return ( {showToggle && ( ) : ( ) } ml={-1} onClick={() => { disclosure.onToggle(); if (dataEvent) { trackCustomEvent( clickEvent({ name: navTreeEvent(dataEvent, "Toggle"), }) ); } }} size="xs" variant="link" w={0} /> )} {title} {nestedItems} ); };