export const dynamic = "force-dynamic"; import type { Metadata } from "next"; import Link from "next/link"; import { notFound } from "next/navigation"; import { getPqpaContent, getPqpaSidebarItems, type PqpaChapter, type PqpaSidebarItem, } from "@/lib/pqpa"; import { getProseChapter } from "@/lib/api"; import { ogForChapter, extractQuoteFromHl, buildOgMeta } from "@/lib/og"; import { BOOK_NAMES } from "@/lib/constants"; import { PageHeader } from "@/components/layout/page-header"; import { JsonLd } from "@/components/seo/json-ld"; import { articleJsonLd } from "@/lib/jsonld"; import { Card, CardContent } from "@/components/ui/card"; import { buttonVariants } from "@/components/ui/button"; import { ChevronLeft, ChevronRight } from "lucide-react"; import { cn } from "@/lib/utils"; import { MobileNav, type MobileNavGroup } from "@/components/layout/mobile-nav"; import { FindInPage } from "@/components/layout/find-in-page"; // --------------------------------------------------------------------------- // Metadata // --------------------------------------------------------------------------- export async function generateMetadata({ params, searchParams, }: { params: Promise<{ chapter: string }>; searchParams: Promise>; }): Promise { const { chapter: slug } = await params; const sp = await searchParams; const chapter = await getProseChapter("pqpa", 0, slug); const hlQuote = extractQuoteFromHl(sp, chapter?.paragraphs); if (hlQuote) { return buildOgMeta({ title: BOOK_NAMES["pqpa"], quote: hlQuote }); } return ogForChapter("pqpa", chapter?.title || ""); } // --------------------------------------------------------------------------- // Page // --------------------------------------------------------------------------- export default async function PqpaChapterPage({ params, }: { params: Promise<{ chapter: string }>; }) { const { chapter: slug } = await params; const content = await getPqpaContent(slug); if (!content) notFound(); const sidebarItems = await getPqpaSidebarItems(); const currentIdx = sidebarItems.findIndex((item) => item.slug === slug); const prev = currentIdx > 0 ? sidebarItems[currentIdx - 1] : null; const next = currentIdx < sidebarItems.length - 1 ? sidebarItems[currentIdx + 1] : null; // Build the page title const isChapter = "chapter" in content && typeof (content as PqpaChapter).chapter === "number" && (content as PqpaChapter).chapter !== null; const chapterNum = isChapter ? (content as PqpaChapter).chapter : null; return (
← Perfect Questions Perfect Answers

{chapterNum != null && ( Chapter {chapterNum}: )} {content.title}

{/* Left sidebar */} {/* Main content */}
{/* Top navigation */} {/* Content */}
{content.paragraphs.map((paragraph, i) => (
))}
{/* Bottom navigation */}
); } // --------------------------------------------------------------------------- // Sidebar // --------------------------------------------------------------------------- function PqpaSidebar({ items, currentSlug, }: { items: PqpaSidebarItem[]; currentSlug: string; }) { // Split items into groups for visual sections // Front matter: intro-segment items before chapters // Chapters: chapter items // Conclusion: conclusion items // Back matter: intro-segment items after conclusion (about-the-author) const frontMatterItems: PqpaSidebarItem[] = []; const chapterItems: PqpaSidebarItem[] = []; const conclusionItems: PqpaSidebarItem[] = []; const backMatterItems: PqpaSidebarItem[] = []; let pastChapters = false; for (const item of items) { if (item.type === "chapter") { pastChapters = true; chapterItems.push(item); } else if (item.type === "conclusion") { pastChapters = true; conclusionItems.push(item); } else if (!pastChapters) { frontMatterItems.push(item); } else { backMatterItems.push(item); } } return ( ); } function SidebarLink({ item, isActive, }: { item: PqpaSidebarItem; isActive: boolean; }) { const label = item.label; return ( {label} ); } // --------------------------------------------------------------------------- // Prev / Next navigation // --------------------------------------------------------------------------- function PqpaNav({ prev, next, sidebarItems, currentSlug, }: { prev: PqpaSidebarItem | null; next: PqpaSidebarItem | null; sidebarItems: PqpaSidebarItem[]; currentSlug: string; }) { const mobileNavGroups: MobileNavGroup[] = []; const frontMatterItems: PqpaSidebarItem[] = []; const chapterItems: PqpaSidebarItem[] = []; const conclusionItems: PqpaSidebarItem[] = []; const backMatterItems: PqpaSidebarItem[] = []; let pastChapters = false; for (const item of sidebarItems) { if (item.type === "chapter") { pastChapters = true; chapterItems.push(item); } else if (item.type === "conclusion") { pastChapters = true; conclusionItems.push(item); } else if (!pastChapters) { frontMatterItems.push(item); } else { backMatterItems.push(item); } } if (frontMatterItems.length > 0) { mobileNavGroups.push({ title: "Front Matter", items: frontMatterItems.map((i) => ({ slug: i.slug, label: i.label, href: `/books/pqpa/${i.slug}`, type: i.type, })), }); } if (chapterItems.length > 0) { mobileNavGroups.push({ title: "Chapters", items: [ ...chapterItems.map((i) => ({ slug: i.slug, label: i.label, href: `/books/pqpa/${i.slug}`, type: i.type, })), ...conclusionItems.map((i) => ({ slug: i.slug, label: i.label, href: `/books/pqpa/${i.slug}`, type: i.type, })), ], }); } if (backMatterItems.length > 0) { mobileNavGroups.push({ title: "Back Matter", items: backMatterItems.map((i) => ({ slug: i.slug, label: i.label, href: `/books/pqpa/${i.slug}`, type: i.type, })), }); } return (
{prev ? ( {prev.type === "chapter" && prev.chapter != null ? `Ch. ${prev.chapter}` : prev.label} Previous ) : (
)}
{next ? ( {next.type === "chapter" && next.chapter != null ? `Ch. ${next.chapter}` : next.label} Next ) : (
)}
); }