import React, { ReactNode, ComponentType } from "react"; import styles from "./OrganizedContent.module.css"; export interface LinkProps { className?: string; id: string; children: ReactNode; } type Link = ComponentType; interface Section { id: string; title: string; Content: ComponentType; } const READ_ALL_TITLE = "Read All"; export const READ_ALL_ID = "read-all"; interface Props { sections: Section[]; currentId: string; link: Link; } export function OrganizedContent(props: Props) { const sections = createSections(props.sections); const currentIndex = sections.findIndex(({ id }) => id === props.currentId); if (currentIndex < 0) { throw new Error(`Section with ID ${props.currentId} 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 (
{sections.map((section, index) => { const classNames = [styles.navItem]; if (index === currentIndex) { classNames.push(styles.selected); } if (section.id === READ_ALL_ID) { classNames.push(styles.readAll); } return ( {section.title} ); })}
); } 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}
)}
); } function createSections(sections: Section[]) { return [ { id: READ_ALL_ID, title: READ_ALL_TITLE, Content() { return ( <> {sections.map(({ id, title, Content: SectionContent }) => (

{title}

))} ); }, }, ...sections, ]; } function Arrow({ direction }: { direction: "left" | "right" }) { return ( ); }