2021-05-27 17:21:40 +03:00
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import cx from 'classnames'
|
|
|
|
|
2021-06-03 18:21:33 +03:00
|
|
|
import { Link } from 'components/Link'
|
|
|
|
import type { TocItem } from 'components/context/ProductLandingContext'
|
2021-05-27 17:21:40 +03:00
|
|
|
|
2021-06-02 19:28:39 +03:00
|
|
|
type Props = {
|
|
|
|
items: Array<TocItem>
|
|
|
|
variant?: 'compact' | 'expanded'
|
|
|
|
}
|
|
|
|
export const TableOfContents = (props: Props) => {
|
|
|
|
const { items, variant = 'expanded' } = props
|
2021-05-27 17:21:40 +03:00
|
|
|
const router = useRouter()
|
|
|
|
|
|
|
|
return (
|
2021-07-29 20:27:20 +03:00
|
|
|
<ul
|
|
|
|
data-testid="table-of-contents"
|
|
|
|
className={cx(variant === 'compact' ? 'list-style-inside pl-2' : 'list-style-none')}
|
|
|
|
>
|
2021-06-02 19:28:39 +03:00
|
|
|
{(items || []).map((item) => {
|
|
|
|
if (!item) {
|
2021-05-28 01:17:27 +03:00
|
|
|
return null
|
|
|
|
}
|
2021-06-02 19:28:39 +03:00
|
|
|
|
2021-07-17 00:54:25 +03:00
|
|
|
const { fullPath: href, title, intro, childTocItems } = item
|
2021-05-27 17:21:40 +03:00
|
|
|
const isActive = router.pathname === href
|
2021-06-02 19:28:39 +03:00
|
|
|
return variant === 'compact' ? (
|
|
|
|
<li key={href} className="f4 my-1">
|
|
|
|
<Link href={href}>{title}</Link>
|
2021-07-29 20:27:20 +03:00
|
|
|
{(childTocItems || []).length > 0 && (
|
|
|
|
<ul
|
|
|
|
className={cx(
|
|
|
|
variant === 'compact' ? 'list-style-circle pl-5 my-3' : 'list-style-none'
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{(childTocItems || []).map((childItem) => {
|
|
|
|
if (!childItem) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<li key={childItem.fullPath} className="f4 mt-1">
|
|
|
|
<Link
|
|
|
|
href={childItem.fullPath}
|
|
|
|
className="Bump-link--hover no-underline py-1 color-border-primary"
|
|
|
|
>
|
|
|
|
{childItem.title}
|
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
)}
|
2021-06-02 19:28:39 +03:00
|
|
|
</li>
|
|
|
|
) : (
|
|
|
|
<li key={href} className={cx('mb-5', isActive && 'color-auto-gray-4')}>
|
2021-06-03 18:21:33 +03:00
|
|
|
<Link
|
|
|
|
href={href}
|
|
|
|
className="Bump-link--hover no-underline d-block py-1 border-bottom color-border-primary"
|
|
|
|
>
|
2021-07-17 00:54:25 +03:00
|
|
|
<h2 className="h4">
|
2021-06-03 18:21:33 +03:00
|
|
|
{title}
|
|
|
|
<span className="Bump-link-symbol">→</span>
|
2021-07-17 00:54:25 +03:00
|
|
|
</h2>
|
2021-05-27 17:21:40 +03:00
|
|
|
</Link>
|
|
|
|
{intro && <p className="f4 mt-3" dangerouslySetInnerHTML={{ __html: intro }} />}
|
2021-06-02 19:28:39 +03:00
|
|
|
</li>
|
2021-05-27 17:21:40 +03:00
|
|
|
)
|
|
|
|
})}
|
2021-06-02 19:28:39 +03:00
|
|
|
</ul>
|
2021-05-27 17:21:40 +03:00
|
|
|
)
|
|
|
|
}
|