import cx from 'classnames'
import { useState, SyntheticEvent } from 'react'
import { ChevronDownIcon } from '@primer/octicons-react'
import { ActionList } from '@primer/react'
import { Link } from 'components/Link'
import { ProductTreeNode } from 'components/context/MainContext'
import { EventType, sendEvent } from 'src/events/browser'
import styles from './SidebarProduct.module.scss'
type SectionProps = {
routePath: string
page: ProductTreeNode
title: string
defaultOpen: boolean
}
export const ProductCollapsibleSection = (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.shortTitle || page.title
const isCurrent = routePath === page.href
return (
{title}
)
}
return (
{
<>
{/* */}
{page.childPages[0]?.documentType === 'mapTopic' ? (
{page.childPages.map((childPage, i) => {
const childTitle = childPage.shortTitle || childPage.title
const isActive = routePath.includes(childPage.href)
const isCurrent = routePath === childPage.href
return (
-
e.stopPropagation()}
className="details-reset"
>
{childTitle}
{childPage.childPages.map((cp) => {
return renderTerminalPageLink(cp)
})}
)
})}
) : page.childPages[0]?.documentType === 'article' ? (
{page.childPages.map(renderTerminalPageLink)}
) : null}
>
}
)
}