import { useRouter } from 'next/router' import cx from 'classnames' import { useState, useEffect, SyntheticEvent } from 'react' import { ChevronDownIcon } from '@primer/octicons-react' import { ActionList } from '@primer/react' import { Link } from 'components/Link' import { ProductTreeNode, useMainContext } from 'components/context/MainContext' import { AllProductsLink } from 'components/sidebar/AllProductsLink' import { EventType, sendEvent } from 'components/lib/events' import styles from './SidebarProduct.module.scss' export const SidebarProduct = () => { const router = useRouter() const { currentProductTree } = useMainContext() useEffect(() => { const activeArticle = document.querySelector('[data-is-current-page=true]') // Setting to the top doesn't give enough context of surrounding categories activeArticle?.scrollIntoView({ block: 'center' }) // scrollIntoView affects some articles that are very low in the sidebar // The content scrolls down a bit. This sets the article content back up // top unless the route contains a link heading. if (!router.asPath.includes('#')) window?.scrollTo(0, 0) }, []) if (!currentProductTree) { return null } const productTitle = currentProductTree.renderedShortTitle || currentProductTree.renderedFullTitle // remove query string and hash const routePath = `/${router.locale}${router.asPath.split('?')[0].split('#')[0]}` const hasExactCategory = !!currentProductTree.childPages.find(({ href }) => routePath.includes(href) ) return ( ) } type SectionProps = { routePath: string page: ProductTreeNode title: string defaultOpen: boolean } const CollapsibleSection = (props: SectionProps) => { const { routePath, defaultOpen, title, page } = props const [isOpen, setIsOpen] = useState(defaultOpen) const onToggle = (e: SyntheticEvent) => { const newIsOpen = (e.target as HTMLDetailsElement).open setIsOpen(newIsOpen) sendEvent({ type: EventType.navigate, navigate_label: `details ${newIsOpen ? 'open' : 'close'}: ${title}`, }) } // The lowest level page link displayed in the tree const renderTerminalPageLink = (page: ProductTreeNode) => { const title = page.renderedShortTitle || page.renderedFullTitle const isCurrent = routePath === page.href return { text: title, renderItem: () => ( {title} ), } } return (
{title}
{ <> {/* */} {page.childPages[0]?.page.documentType === 'mapTopic' ? ( ) : page.childPages[0]?.page.documentType === 'article' ? (
) : null} }
) }