import NextLink from "next/link"; import React, { ReactNode, ComponentType } from "react"; import styles from "./OrganizedContent.module.css"; type Link = ComponentType; interface Section { id: string; title: string; } const READ_ALL_TITLE = "Read All"; export const READ_ALL_ID = "read-all"; interface Props { sections: Section[]; id: string; children: ReactNode; link: Link; } export function OrganizedContent({ sections, id, children, link: Link, }: Props) { const currentIndex = sections.findIndex( ({ id: sectionId }) => sectionId === id ); if (currentIndex < 0) { throw new Error(`Section with ID ${id} was not found`); } const section = sections[currentIndex]; const isReadAll = section.id === READ_ALL_ID; return (
); } interface NavProps { sections: Section[]; currentIndex: number; link: Link; } function Nav({ sections, currentIndex, link: Link }: NavProps) { return ( ); } interface FooterProps { sections: Section[]; currentIndex: number; link: Link; } function Footer({ sections, currentIndex, link: Link }: FooterProps) { const prevSection = currentIndex > 0 && sections[currentIndex - 1].id !== READ_ALL_ID ? sections[currentIndex - 1] : undefined; const nextSection = currentIndex < sections.length - 1 && sections[currentIndex + 1].id !== READ_ALL_ID ? sections[currentIndex + 1] : undefined; return (
{prevSection && (
Previous
{prevSection.title}
)} {nextSection && (
Next
{nextSection.title}
)}
); } export interface SectionWithContent { section: Section; Content: ComponentType; } export function createReadAllSection( sections: Section[], content: false ): Section; export function createReadAllSection( sections: SectionWithContent[], content: true ): SectionWithContent; export function createReadAllSection( sections: SectionWithContent[] | Section[], content = true ): SectionWithContent | Section { const readAllSection = { id: READ_ALL_ID, title: READ_ALL_TITLE, }; return content ? { section: readAllSection, Content: function ReadAllContent() { return ( <> {(sections as SectionWithContent[]).map( ({ section: { id, title }, Content }) => (

{title}

) )} ); }, } : readAllSection; } export interface LinkProps { className?: string; id: string; children: ReactNode; } export function createLink(page: string) { let base = page.startsWith("/") ? page : `/${page}`; base = base.endsWith("/") ? base : `${base}/`; return function Link({ className, id, children }: LinkProps) { const href = id === READ_ALL_ID ? base : base + id; return ( {children} ); }; } function Arrow({ direction }: { direction: "left" | "right" }) { return ( ); }