377 lines
12 KiB
TypeScript
377 lines
12 KiB
TypeScript
export const dynamic = "force-dynamic";
|
|
|
|
import type { Metadata } from "next";
|
|
import Link from "next/link";
|
|
import { notFound } from "next/navigation";
|
|
import {
|
|
getSsrContent,
|
|
getSsrSidebarItems,
|
|
type SsrChapter,
|
|
type SsrSidebarItem,
|
|
} from "@/lib/ssr";
|
|
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<Record<string, string | string[] | undefined>>;
|
|
}): Promise<Metadata> {
|
|
const { chapter: slug } = await params;
|
|
const sp = await searchParams;
|
|
const chapter = await getProseChapter("ssr", 0, slug);
|
|
const hlQuote = extractQuoteFromHl(sp, chapter?.paragraphs);
|
|
if (hlQuote) {
|
|
return buildOgMeta({ title: BOOK_NAMES["ssr"], quote: hlQuote });
|
|
}
|
|
return ogForChapter("ssr", chapter?.title || "");
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Page
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export default async function SsrChapterPage({
|
|
params,
|
|
}: {
|
|
params: Promise<{ chapter: string }>;
|
|
}) {
|
|
const { chapter: slug } = await params;
|
|
|
|
const content = await getSsrContent(slug);
|
|
if (!content) notFound();
|
|
|
|
const sidebarItems = await getSsrSidebarItems();
|
|
|
|
// Build a flat list of navigable items (skip "parent" type — they're labels, not pages)
|
|
const navigableItems = sidebarItems.filter((item) => item.type !== "parent");
|
|
const currentIdx = navigableItems.findIndex((item) => item.slug === slug);
|
|
|
|
const prev = currentIdx > 0 ? navigableItems[currentIdx - 1] : null;
|
|
const next =
|
|
currentIdx < navigableItems.length - 1
|
|
? navigableItems[currentIdx + 1]
|
|
: null;
|
|
|
|
// Determine if this is a sub-chapter (has parentChapter and is type "chapter"/"sub-chapter")
|
|
const isSubChapter =
|
|
"parentChapter" in content &&
|
|
typeof (content as SsrChapter).parentChapter === "number";
|
|
const parentChapterNum = isSubChapter
|
|
? (content as SsrChapter).parentChapter
|
|
: null;
|
|
|
|
return (
|
|
<div className="min-h-screen bg-background">
|
|
<JsonLd data={articleJsonLd({ path: `/books/ssr/${slug}`, headline: content.title, description: content.paragraphs?.[0], isPartOf: "The Science of Self-Realization" })} />
|
|
<PageHeader sticky>
|
|
<Link
|
|
href="/books/ssr"
|
|
className="text-sm text-muted-foreground hover:text-foreground transition-colors"
|
|
>
|
|
← The Science of Self-Realization
|
|
</Link>
|
|
<div className="mt-3 flex items-start justify-between gap-3">
|
|
<h1 className="text-2xl font-bold text-foreground">
|
|
{parentChapterNum != null && (
|
|
<span className="text-primary">Chapter {parentChapterNum}: </span>
|
|
)}
|
|
{content.title}
|
|
</h1>
|
|
<FindInPage targetId="book-content" className="shrink-0 mt-0.5" />
|
|
</div>
|
|
</PageHeader>
|
|
|
|
<div className="max-w-5xl mx-auto flex">
|
|
{/* Left sidebar */}
|
|
<SsrSidebar items={sidebarItems} currentSlug={slug} />
|
|
|
|
{/* Main content */}
|
|
<main className="flex-1 min-w-0 px-6 py-8 space-y-8">
|
|
{/* Top navigation */}
|
|
<SsrNav
|
|
prev={prev}
|
|
next={next}
|
|
sidebarItems={sidebarItems}
|
|
currentSlug={slug}
|
|
/>
|
|
|
|
{/* Content */}
|
|
<Card id="book-content">
|
|
<CardContent className="py-8 px-6 sm:px-8">
|
|
<div className="space-y-4">
|
|
{content.paragraphs.map((paragraph, i) => (
|
|
<div
|
|
key={i}
|
|
className="text-base text-foreground leading-relaxed"
|
|
dangerouslySetInnerHTML={{ __html: paragraph }}
|
|
/>
|
|
))}
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
{/* Bottom navigation */}
|
|
<SsrNav
|
|
prev={prev}
|
|
next={next}
|
|
sidebarItems={sidebarItems}
|
|
currentSlug={slug}
|
|
/>
|
|
</main>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Sidebar
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function SsrSidebar({
|
|
items,
|
|
currentSlug,
|
|
}: {
|
|
items: SsrSidebarItem[];
|
|
currentSlug: string;
|
|
}) {
|
|
// Split items by section
|
|
const frontMatter = items.filter(
|
|
(i) => i.type === "intro-segment" && i.slug !== "intro-about-the-author",
|
|
);
|
|
const parents = items.filter((i) => i.type === "parent");
|
|
const subChapters = items.filter((i) => i.type === "sub-chapter");
|
|
const backMatter = items.filter((i) => i.slug === "intro-about-the-author");
|
|
|
|
return (
|
|
<aside className="hidden lg:block w-56 shrink-0 sticky top-[89px] self-start max-h-[calc(100vh-89px)] overflow-y-auto py-8 pr-4">
|
|
{/* Front Matter */}
|
|
{frontMatter.length > 0 && (
|
|
<>
|
|
<h2 className="text-xs font-bold uppercase tracking-wider text-muted-foreground mb-3 px-2">
|
|
Front Matter
|
|
</h2>
|
|
<nav className="flex flex-col gap-0.5 mb-4">
|
|
{frontMatter.map((item) => (
|
|
<SidebarLink
|
|
key={item.slug}
|
|
item={item}
|
|
isActive={item.slug === currentSlug}
|
|
/>
|
|
))}
|
|
</nav>
|
|
</>
|
|
)}
|
|
|
|
{/* Chapters — grouped by parent */}
|
|
{parents.length > 0 && (
|
|
<>
|
|
<h2 className="text-xs font-bold uppercase tracking-wider text-muted-foreground mb-3 px-2">
|
|
Chapters
|
|
</h2>
|
|
<nav className="flex flex-col gap-0.5 mb-4">
|
|
{parents.map((parent) => (
|
|
<div key={parent.slug}>
|
|
{/* Parent heading — not a link, just a bold label */}
|
|
<div className="px-2 py-1.5 text-xs font-bold text-muted-foreground mt-3 first:mt-0">
|
|
{parent.label}
|
|
</div>
|
|
{/* Sub-chapters indented */}
|
|
{subChapters
|
|
.filter((sub) => sub.parentChapter === parent.parentChapter)
|
|
.map((sub) => {
|
|
const isActive = sub.slug === currentSlug;
|
|
return (
|
|
<Link
|
|
key={sub.slug}
|
|
href={`/books/ssr/${sub.slug}`}
|
|
className={cn(
|
|
"pl-4 px-2 py-1.5 rounded-md text-sm transition-colors truncate block",
|
|
isActive
|
|
? "bg-primary/10 text-primary font-medium"
|
|
: "text-muted-foreground hover:bg-muted hover:text-foreground",
|
|
)}
|
|
title={sub.label}
|
|
>
|
|
{sub.label}
|
|
</Link>
|
|
);
|
|
})}
|
|
</div>
|
|
))}
|
|
</nav>
|
|
</>
|
|
)}
|
|
|
|
{/* Back Matter */}
|
|
{backMatter.length > 0 && (
|
|
<>
|
|
<h2 className="text-xs font-bold uppercase tracking-wider text-muted-foreground mb-3 px-2">
|
|
Back Matter
|
|
</h2>
|
|
<nav className="flex flex-col gap-0.5">
|
|
{backMatter.map((item) => (
|
|
<SidebarLink
|
|
key={item.slug}
|
|
item={item}
|
|
isActive={item.slug === currentSlug}
|
|
/>
|
|
))}
|
|
</nav>
|
|
</>
|
|
)}
|
|
</aside>
|
|
);
|
|
}
|
|
|
|
function SidebarLink({
|
|
item,
|
|
isActive,
|
|
}: {
|
|
item: SsrSidebarItem;
|
|
isActive: boolean;
|
|
}) {
|
|
return (
|
|
<Link
|
|
href={`/books/ssr/${item.slug}`}
|
|
className={cn(
|
|
"px-2 py-1.5 rounded-md text-sm transition-colors truncate",
|
|
isActive
|
|
? "bg-primary/10 text-primary font-medium"
|
|
: "text-muted-foreground hover:bg-muted hover:text-foreground",
|
|
)}
|
|
title={item.label}
|
|
>
|
|
{item.label}
|
|
</Link>
|
|
);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Prev / Next navigation
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function SsrNav({
|
|
prev,
|
|
next,
|
|
sidebarItems,
|
|
currentSlug,
|
|
}: {
|
|
prev: SsrSidebarItem | null;
|
|
next: SsrSidebarItem | null;
|
|
sidebarItems: SsrSidebarItem[];
|
|
currentSlug: string;
|
|
}) {
|
|
const mobileNavGroups: MobileNavGroup[] = [];
|
|
|
|
const frontMatter = sidebarItems.filter(
|
|
(i) => i.type === "intro-segment" && i.slug !== "intro-about-the-author",
|
|
);
|
|
if (frontMatter.length > 0) {
|
|
mobileNavGroups.push({
|
|
title: "Front Matter",
|
|
items: frontMatter.map((i) => ({
|
|
slug: i.slug,
|
|
label: i.label,
|
|
href: `/books/ssr/${i.slug}`,
|
|
type: i.type,
|
|
})),
|
|
});
|
|
}
|
|
|
|
const chapterItems = sidebarItems.filter(
|
|
(i) => i.type === "parent" || i.type === "sub-chapter",
|
|
);
|
|
if (chapterItems.length > 0) {
|
|
mobileNavGroups.push({
|
|
title: "Chapters",
|
|
items: chapterItems.map((i) => ({
|
|
slug: i.slug,
|
|
label: i.label,
|
|
href: `/books/ssr/${i.slug}`,
|
|
type: i.type,
|
|
parentChapter: i.parentChapter,
|
|
isParent: i.type === "parent",
|
|
})),
|
|
});
|
|
}
|
|
|
|
const backMatter = sidebarItems.filter(
|
|
(i) => i.slug === "intro-about-the-author",
|
|
);
|
|
if (backMatter.length > 0) {
|
|
mobileNavGroups.push({
|
|
title: "Back Matter",
|
|
items: backMatter.map((i) => ({
|
|
slug: i.slug,
|
|
label: i.label,
|
|
href: `/books/ssr/${i.slug}`,
|
|
type: i.type,
|
|
})),
|
|
});
|
|
}
|
|
|
|
return (
|
|
<div className="flex items-center justify-between gap-3">
|
|
<div className="flex items-center gap-2">
|
|
<MobileNav
|
|
groups={mobileNavGroups}
|
|
currentSlug={currentSlug}
|
|
bookTitle="The Science of Self-Realization"
|
|
/>
|
|
{prev ? (
|
|
<Link
|
|
href={`/books/ssr/${prev.slug}`}
|
|
className={cn(
|
|
buttonVariants({ variant: "outline", size: "sm" }),
|
|
"gap-1.5",
|
|
)}
|
|
>
|
|
<ChevronLeft className="size-4" />
|
|
<span className="hidden sm:inline truncate max-w-[180px]">
|
|
{prev.label}
|
|
</span>
|
|
<span className="sm:hidden">Previous</span>
|
|
</Link>
|
|
) : (
|
|
<div className="hidden lg:block" />
|
|
)}
|
|
</div>
|
|
|
|
{next ? (
|
|
<Link
|
|
href={`/books/ssr/${next.slug}`}
|
|
className={cn(
|
|
buttonVariants({ variant: "outline", size: "sm" }),
|
|
"gap-1.5",
|
|
)}
|
|
>
|
|
<span className="hidden sm:inline truncate max-w-[180px]">
|
|
{next.label}
|
|
</span>
|
|
<span className="sm:hidden">Next</span>
|
|
<ChevronRight className="size-4" />
|
|
</Link>
|
|
) : (
|
|
<div />
|
|
)}
|
|
</div>
|
|
);
|
|
}
|