206 lines
5.9 KiB
TypeScript
206 lines
5.9 KiB
TypeScript
import Link from "next/link";
|
|
import { notFound } from "next/navigation";
|
|
import { getProseChapter, getProseChapters } from "@/lib/api";
|
|
import { PageHeader } from "@/components/page-header";
|
|
import { Card, CardContent } from "@/components/ui/card";
|
|
import { buttonVariants } from "@/components/ui/button";
|
|
import { ChevronLeft, ChevronRight } from "lucide-react";
|
|
import { cn } from "@/lib/utils";
|
|
|
|
export default async function KbChapterPage({
|
|
params,
|
|
}: {
|
|
params: Promise<{ volume: string; slug: string }>;
|
|
}) {
|
|
const { volume, slug } = await params;
|
|
const vol = parseInt(volume);
|
|
if (isNaN(vol) || vol < 1 || vol > 2) notFound();
|
|
|
|
const chapter = await getProseChapter("kb", vol, slug);
|
|
if (!chapter) notFound();
|
|
|
|
// Fetch all chapters/segments in this volume for sidebar + prev/next
|
|
const allChapters = await getProseChapters("kb", vol);
|
|
const currentIdx = allChapters.findIndex((ch) => ch.slug === slug);
|
|
|
|
const prev = currentIdx > 0 ? allChapters[currentIdx - 1] : null;
|
|
const next =
|
|
currentIdx < allChapters.length - 1 ? allChapters[currentIdx + 1] : null;
|
|
|
|
const chapterLabel =
|
|
chapter.type === "chapter" && chapter.chapter != null
|
|
? `Chapter ${chapter.chapter}`
|
|
: null;
|
|
|
|
return (
|
|
<div className="min-h-screen bg-background">
|
|
<PageHeader sticky wide>
|
|
<Link
|
|
href={`/books/kb/${vol}`}
|
|
className="text-sm text-muted-foreground hover:text-foreground transition-colors"
|
|
>
|
|
← Volume {vol}
|
|
</Link>
|
|
<h1 className="mt-3 text-2xl font-bold text-foreground">
|
|
{chapterLabel && (
|
|
<span className="text-primary">{chapterLabel}: </span>
|
|
)}
|
|
{chapter.title}
|
|
</h1>
|
|
</PageHeader>
|
|
|
|
<div className="w-full md:max-w-6xl mx-auto flex">
|
|
{/* Left sidebar — chapter index */}
|
|
<ChapterSidebar
|
|
chapters={allChapters}
|
|
currentSlug={slug}
|
|
volume={vol}
|
|
/>
|
|
|
|
{/* Main content */}
|
|
<main className="flex-1 min-w-0 px-6 py-8 space-y-8">
|
|
{/* Top navigation */}
|
|
<ChapterNav prev={prev} next={next} volume={vol} />
|
|
|
|
{/* Chapter content */}
|
|
<Card>
|
|
<CardContent className="py-8 px-6 sm:px-8">
|
|
<div className="space-y-4">
|
|
{chapter.paragraphs.map((paragraph, i) => (
|
|
<div
|
|
key={i}
|
|
className="text-foreground leading-relaxed"
|
|
dangerouslySetInnerHTML={{ __html: paragraph }}
|
|
/>
|
|
))}
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
{/* Bottom navigation */}
|
|
<ChapterNav prev={prev} next={next} volume={vol} />
|
|
</main>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Sidebar — chapter index
|
|
// ---------------------------------------------------------------------------
|
|
|
|
type SidebarChapter = {
|
|
slug: string;
|
|
title: string;
|
|
type: string;
|
|
chapter: number | null;
|
|
};
|
|
|
|
function ChapterSidebar({
|
|
chapters,
|
|
currentSlug,
|
|
volume,
|
|
}: {
|
|
chapters: SidebarChapter[];
|
|
currentSlug: string;
|
|
volume: number;
|
|
}) {
|
|
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">
|
|
<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">
|
|
{chapters.map((ch) => {
|
|
const isActive = ch.slug === currentSlug;
|
|
const label =
|
|
ch.type === "chapter" && ch.chapter != null
|
|
? `${ch.chapter}. ${ch.title}`
|
|
: ch.title;
|
|
|
|
return (
|
|
<Link
|
|
key={ch.slug}
|
|
href={`/books/kb/${volume}/${ch.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={label}
|
|
>
|
|
{label}
|
|
</Link>
|
|
);
|
|
})}
|
|
</nav>
|
|
</aside>
|
|
);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Prev / Next navigation
|
|
// ---------------------------------------------------------------------------
|
|
|
|
type NavChapter = {
|
|
slug: string;
|
|
title: string;
|
|
type: string;
|
|
chapter: number | null;
|
|
};
|
|
|
|
function ChapterNav({
|
|
prev,
|
|
next,
|
|
volume,
|
|
}: {
|
|
prev: NavChapter | null;
|
|
next: NavChapter | null;
|
|
volume: number;
|
|
}) {
|
|
return (
|
|
<div className="flex items-center justify-between gap-3">
|
|
{prev ? (
|
|
<Link
|
|
href={`/books/kb/${volume}/${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.type === "chapter" && prev.chapter != null
|
|
? `Ch. ${prev.chapter}`
|
|
: prev.title}
|
|
</span>
|
|
<span className="sm:hidden">Previous</span>
|
|
</Link>
|
|
) : (
|
|
<div />
|
|
)}
|
|
|
|
{next ? (
|
|
<Link
|
|
href={`/books/kb/${volume}/${next.slug}`}
|
|
className={cn(
|
|
buttonVariants({ variant: "outline", size: "sm" }),
|
|
"gap-1.5",
|
|
)}
|
|
>
|
|
<span className="hidden sm:inline truncate max-w-[180px]">
|
|
{next.type === "chapter" && next.chapter != null
|
|
? `Ch. ${next.chapter}`
|
|
: next.title}
|
|
</span>
|
|
<span className="sm:hidden">Next</span>
|
|
<ChevronRight className="size-4" />
|
|
</Link>
|
|
) : (
|
|
<div />
|
|
)}
|
|
</div>
|
|
);
|
|
}
|