import { ChevronLeft, ChevronRight } from 'lucide-react'
import Link from 'next/link'
import { Doc } from '@/.velite'
import { docsConfig } from '@/config/docs'
import { cn } from '@/lib/utils'
import { NavItem, NavItemWithChildren } from '@/types/nav'
interface DocsPagerProps {
doc: Doc
}
export function DocsPager({ doc }: DocsPagerProps) {
const pager = getPagerForDoc(doc)
if (!pager) {
return null
}
const hasBoth = pager?.prev?.href && pager?.next?.href
return (
{pager?.prev?.href && (
Previous
{pager.prev.title}
)}
{pager?.next?.href && (
Next
{pager.next.title}
)}
)
}
export function getPagerForDoc(doc: Doc) {
const flattenedLinks = [null, ...flatten(docsConfig.sidebarNav), null]
const activeIndex = flattenedLinks.findIndex((link) => doc.slug === link?.href)
const prev = activeIndex !== 0 ? flattenedLinks[activeIndex - 1] : null
const next = activeIndex !== flattenedLinks.length - 1 ? flattenedLinks[activeIndex + 1] : null
return {
prev,
next,
}
}
export function flatten(links: NavItemWithChildren[]): NavItem[] {
return links
.reduce((flat, link) => {
return flat.concat(link.items?.length ? flatten(link.items) : link)
}, [])
.filter((link) => !link?.disabled)
}
export interface BreadcrumbSegment {
title: string
href?: string
}
export function getBreadcrumbSegments(doc: Doc): BreadcrumbSegment[] {
const segments: BreadcrumbSegment[] = [{ title: 'Docs', href: '/docs' }]
for (const section of docsConfig.sidebarNav) {
const flatItems = flatten(section.items ?? [])
const inSection = flatItems.some((item) => item.href === doc.slug)
if (!inSection || !section.title) continue
const sectionLanding = flatItems.find((item) => item.priority) ?? flatItems[0]
const isOnLanding = sectionLanding?.href === doc.slug
if (sectionLanding?.href && !isOnLanding) {
segments.push({ title: section.title, href: sectionLanding.href })
}
break
}
return segments
}