2024-01-15 12:33:15 +00:00
|
|
|
'use client'
|
|
|
|
|
|
|
|
import {
|
2024-02-01 14:16:17 +00:00
|
|
|
APIGroup,
|
2024-01-30 14:19:25 +00:00
|
|
|
ArticleHeadings,
|
2024-01-15 12:33:15 +00:00
|
|
|
SidebarContentArticleLink,
|
|
|
|
SidebarContentCategoryLink,
|
|
|
|
SidebarContentLink,
|
|
|
|
SidebarContentList,
|
|
|
|
SidebarContentSectionLink,
|
|
|
|
} from '@/types/content-types'
|
2024-01-30 14:19:25 +00:00
|
|
|
import * as Accordion from '@radix-ui/react-accordion'
|
2024-01-15 12:33:15 +00:00
|
|
|
import Link from 'next/link'
|
|
|
|
import { usePathname } from 'next/navigation'
|
2024-02-09 11:54:13 +00:00
|
|
|
import { UIEvent, createContext, useContext, useEffect, useRef } from 'react'
|
2024-01-30 14:19:25 +00:00
|
|
|
import { SectionLinks } from './Header'
|
|
|
|
import { Chevron } from './Icons'
|
2024-01-15 12:33:15 +00:00
|
|
|
import { Search } from './Search'
|
|
|
|
import { SidebarCloseButton } from './SidebarCloseButton'
|
|
|
|
import { ToggleMenuButton } from './ToggleMenuButton'
|
|
|
|
|
|
|
|
type SidebarProps = SidebarContentList
|
|
|
|
|
2024-01-30 14:19:25 +00:00
|
|
|
const linkContext = createContext<{
|
|
|
|
activeId: string | null
|
|
|
|
articleId: string | null
|
|
|
|
categoryId: string | null
|
|
|
|
sectionId: string | null
|
|
|
|
} | null>(null)
|
2024-01-15 12:33:15 +00:00
|
|
|
|
2024-02-09 11:54:13 +00:00
|
|
|
// N.B. UGH, the sidebar's scroll position keeps getting lost when navigation occurs
|
|
|
|
// and we keep track of the last known position here outside of the component because
|
|
|
|
// it keeps re-rendering.
|
|
|
|
let scrollPosition = 0
|
|
|
|
|
2024-02-29 16:28:45 +00:00
|
|
|
export function Sidebar({ links, sectionId, categoryId, articleId }: SidebarProps) {
|
2024-01-15 12:33:15 +00:00
|
|
|
const activeId = articleId ?? categoryId ?? sectionId
|
2024-02-09 11:54:13 +00:00
|
|
|
const sidebarRef = useRef<HTMLDivElement>(null)
|
2024-01-15 12:33:15 +00:00
|
|
|
|
|
|
|
const pathName = usePathname()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
document.body.classList.remove('sidebar-open')
|
2024-01-30 14:19:25 +00:00
|
|
|
|
2024-02-09 11:54:13 +00:00
|
|
|
const sidebarEl = sidebarRef.current
|
|
|
|
if (!sidebarEl) return
|
|
|
|
|
|
|
|
sidebarEl.scrollTo(0, scrollPosition)
|
|
|
|
|
|
|
|
const activeLink = document.querySelector('.sidebar__nav [data-active=true]') as HTMLElement
|
|
|
|
if (
|
|
|
|
activeLink &&
|
2024-02-12 14:30:55 +00:00
|
|
|
(activeLink.offsetTop < sidebarEl.scrollTop ||
|
|
|
|
activeLink.offsetTop > sidebarEl.scrollTop + sidebarEl.clientHeight)
|
2024-02-09 11:54:13 +00:00
|
|
|
) {
|
|
|
|
// The above will *mostly* work to keep the position but we have some accordions that will collapse
|
|
|
|
// (like in the Reference docs) and we need to scroll to the active item.
|
|
|
|
activeLink.scrollIntoView({ block: 'center' })
|
|
|
|
}
|
2024-01-15 12:33:15 +00:00
|
|
|
}, [pathName])
|
|
|
|
|
2024-02-09 11:54:13 +00:00
|
|
|
const handleScroll = (e: UIEvent) => {
|
|
|
|
e.stopPropagation()
|
|
|
|
scrollPosition = sidebarRef.current?.scrollTop ?? 0
|
|
|
|
}
|
|
|
|
|
2024-01-15 12:33:15 +00:00
|
|
|
return (
|
|
|
|
<>
|
2024-01-30 14:19:25 +00:00
|
|
|
<linkContext.Provider value={{ activeId, articleId, categoryId, sectionId }}>
|
2024-02-09 11:54:13 +00:00
|
|
|
<div ref={sidebarRef} className="sidebar scroll-light" onScroll={handleScroll}>
|
2024-02-05 14:32:50 +00:00
|
|
|
<Search />
|
2024-01-30 14:19:25 +00:00
|
|
|
<div className="sidebar__section__links">
|
|
|
|
<SectionLinks sectionId={sectionId} />
|
|
|
|
</div>
|
2024-02-29 16:28:45 +00:00
|
|
|
<SidebarLinks links={links} />
|
2024-01-15 12:33:15 +00:00
|
|
|
<SidebarCloseButton />
|
|
|
|
</div>
|
|
|
|
<ToggleMenuButton />
|
2024-01-30 14:19:25 +00:00
|
|
|
</linkContext.Provider>
|
2024-01-15 12:33:15 +00:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-02-29 16:28:45 +00:00
|
|
|
export function SidebarLinks({ links }: { links: SidebarContentLink[] }) {
|
2024-01-15 12:33:15 +00:00
|
|
|
return (
|
|
|
|
<nav className="sidebar__nav">
|
|
|
|
<ul className="sidebar__list sidebar__sections__list">
|
|
|
|
{links.map((link) => (
|
2024-02-29 16:28:45 +00:00
|
|
|
<SidebarLink key={link.url} {...link} />
|
2024-01-15 12:33:15 +00:00
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</nav>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-02-29 16:28:45 +00:00
|
|
|
function SidebarLink(props: SidebarContentLink) {
|
2024-01-15 12:33:15 +00:00
|
|
|
switch (props.type) {
|
|
|
|
case 'section': {
|
2024-02-29 16:28:45 +00:00
|
|
|
return <SidebarSection {...props} />
|
2024-01-15 12:33:15 +00:00
|
|
|
}
|
|
|
|
case 'article': {
|
2024-02-29 16:28:45 +00:00
|
|
|
return <SidebarArticle {...props} />
|
2024-01-15 12:33:15 +00:00
|
|
|
}
|
|
|
|
case 'category': {
|
2024-02-29 16:28:45 +00:00
|
|
|
return <SidebarCategory {...props} />
|
2024-01-15 12:33:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-29 16:28:45 +00:00
|
|
|
function SidebarSection({ title, children }: SidebarContentSectionLink) {
|
2024-01-15 12:33:15 +00:00
|
|
|
if (children.length === 0) return null
|
|
|
|
|
|
|
|
return (
|
|
|
|
<li className="sidebar__section">
|
2024-02-29 16:28:45 +00:00
|
|
|
{title && <span className="sidebar__section__title uppercase_title">{title}</span>}
|
2024-01-15 12:33:15 +00:00
|
|
|
<ul className="sidebar__list">
|
|
|
|
{children.map((link) => (
|
2024-02-29 16:28:45 +00:00
|
|
|
<SidebarLink key={link.url} {...link} />
|
2024-01-15 12:33:15 +00:00
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-02-29 16:28:45 +00:00
|
|
|
function SidebarCategory({ title, children }: SidebarContentCategoryLink) {
|
2024-01-30 14:19:25 +00:00
|
|
|
const linkCtx = useContext(linkContext)
|
2024-01-15 12:33:15 +00:00
|
|
|
if (children.length === 0) return null
|
2024-01-30 14:19:25 +00:00
|
|
|
const hasGroups = children.some((child) => !!(child as SidebarContentArticleLink).groupId)
|
|
|
|
const activeArticle = children.find(
|
|
|
|
(child) => (child as SidebarContentArticleLink).articleId === linkCtx?.articleId
|
|
|
|
)
|
|
|
|
const activeGroup = activeArticle && (activeArticle as SidebarContentArticleLink).groupId
|
2024-02-01 14:16:17 +00:00
|
|
|
const groups = Object.values(APIGroup)
|
2024-01-15 12:33:15 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<li className="sidebar__category">
|
2024-01-30 14:19:25 +00:00
|
|
|
{hasGroups ? (
|
|
|
|
<>
|
2024-02-05 14:42:55 +00:00
|
|
|
<span className="sidebar__link sidebar__category__title">{title}</span>
|
2024-01-30 14:19:25 +00:00
|
|
|
<Accordion.Root
|
|
|
|
type="multiple"
|
|
|
|
defaultValue={[`${linkCtx?.categoryId}-${activeGroup}-${linkCtx?.articleId}`]}
|
|
|
|
>
|
|
|
|
{groups.map((group) => {
|
|
|
|
const articles = children.filter(
|
|
|
|
(child) => (child as SidebarContentArticleLink).groupId === group
|
|
|
|
)
|
|
|
|
if (articles.length === 0) return null
|
|
|
|
|
2024-02-05 20:46:07 +00:00
|
|
|
const value = `${linkCtx?.categoryId}-${group}-${linkCtx?.articleId}`
|
2024-01-30 14:19:25 +00:00
|
|
|
return (
|
2024-02-05 20:46:07 +00:00
|
|
|
<Accordion.Item key={value} value={value}>
|
|
|
|
<Accordion.Trigger className="sidebar__section__group__title">
|
2024-01-30 14:19:25 +00:00
|
|
|
{group}
|
|
|
|
<Chevron />
|
|
|
|
</Accordion.Trigger>
|
|
|
|
<Accordion.Content>
|
2024-02-05 20:46:07 +00:00
|
|
|
<ul className="sidebar__list sidebar__group">
|
2024-01-30 14:19:25 +00:00
|
|
|
{articles.map((link) => (
|
2024-02-29 16:28:45 +00:00
|
|
|
<SidebarLink key={link.url} {...link} />
|
2024-01-30 14:19:25 +00:00
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</Accordion.Content>
|
|
|
|
</Accordion.Item>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</Accordion.Root>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
2024-02-05 14:42:55 +00:00
|
|
|
<Link
|
|
|
|
href={children[0].url}
|
|
|
|
title={title}
|
|
|
|
className="sidebar__link sidebar__category__title"
|
|
|
|
>
|
2024-01-30 14:19:25 +00:00
|
|
|
{title}
|
|
|
|
</Link>
|
|
|
|
<ul className="sidebar__list">
|
|
|
|
{children.map((link) => (
|
|
|
|
<SidebarLink key={link.url} {...link} />
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</>
|
|
|
|
)}
|
2024-01-15 12:33:15 +00:00
|
|
|
<hr />
|
|
|
|
</li>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-01-30 14:19:25 +00:00
|
|
|
function SidebarArticle({
|
|
|
|
title,
|
|
|
|
url,
|
|
|
|
articleId,
|
|
|
|
headings,
|
|
|
|
}: SidebarContentArticleLink & { headings?: ArticleHeadings }) {
|
2024-02-01 14:16:17 +00:00
|
|
|
const activeLink = useContext(linkContext)
|
|
|
|
const isActive = activeLink?.activeId === articleId
|
2024-01-15 12:33:15 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<li className="sidebar__article">
|
2024-02-01 18:16:30 +00:00
|
|
|
<Link href={url} title={title} className="sidebar__link" data-active={isActive}>
|
2024-01-30 14:19:25 +00:00
|
|
|
{title}
|
2024-01-15 12:33:15 +00:00
|
|
|
</Link>
|
2024-01-30 14:19:25 +00:00
|
|
|
|
|
|
|
{isActive && (
|
|
|
|
<ul className="sidebar__list">
|
|
|
|
{headings
|
2024-02-01 14:16:17 +00:00
|
|
|
?.filter((heading) => heading.level < 4)
|
2024-01-30 14:19:25 +00:00
|
|
|
.map((heading) => (
|
2024-02-01 14:16:17 +00:00
|
|
|
<li
|
2024-02-05 20:46:07 +00:00
|
|
|
key={`${heading.slug}`}
|
2024-02-01 14:16:17 +00:00
|
|
|
data-heading-level={heading.title === 'Constructor' ? 2 : heading.level}
|
|
|
|
>
|
2024-02-01 18:16:30 +00:00
|
|
|
<Link href={`#${heading.slug}`} title={heading.title} className="sidebar__link">
|
2024-01-30 14:19:25 +00:00
|
|
|
{heading.isCode ? (
|
|
|
|
<code>{heading.title.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')}</code>
|
|
|
|
) : (
|
|
|
|
heading.title.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1')
|
|
|
|
)}
|
|
|
|
</Link>
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
)}
|
2024-01-15 12:33:15 +00:00
|
|
|
</li>
|
|
|
|
)
|
|
|
|
}
|