"use client" import Link from "next/link" import { motion } from "motion/react" import { NavItem, NavItemWithChildren } from "@/types/nav" import { Doc } from "@/types/types" import { docsConfig } from "@/config/docs" const MotionLink = motion.create(Link) interface DocsPagerProps { doc: Doc } export function DocsPager({ doc }: DocsPagerProps) { const pager = getPagerForDoc(doc) if (!pager) { return null } return (
{pager?.prev?.href && ( {pager.prev.title} )} {pager?.next?.href && ( {pager.next.title} )}
) } export function getPagerForDoc(doc: Doc) { const flattenedLinks = flatten(docsConfig) const activeIndex = flattenedLinks.findIndex( (link) => doc.slug === link.href?.replace(/^\/docs\//, "") ) 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) }