Files
spsays-training/app/(app)/(reader)/books/cc/[lila]/page.tsx
T

148 lines
4.8 KiB
TypeScript

import type { Metadata } from "next";
import Link from "next/link";
import { PageHeader } from "@/components/layout/page-header";
import { notFound } from "next/navigation";
import { Card, CardContent } from "@/components/ui/card";
import { FindInPage } from "@/components/layout/find-in-page";
import { ExtractedVerseCount } from "../../extracted-verse-count";
import { getBookMeta, getProseChapter } from "@/lib/api";
import { buildOgMeta } from "@/lib/og";
import { BOOK_NAMES, CC_LILAS } from "@/lib/constants";
export async function generateMetadata({
params,
}: {
params: Promise<{ lila: string }>;
}): Promise<Metadata> {
const { lila } = await params;
const bookTitle = BOOK_NAMES["cc"] || "Śrī Caitanya-caritāmṛta";
// A non-līlā segment (front-flap, preface, introduction) is front matter.
if (!CC_LILAS[lila]) {
const segment = await getProseChapter("cc", 0, lila);
if (segment) {
return buildOgMeta({ title: bookTitle, detail: segment.title });
}
}
return buildOgMeta({ title: bookTitle, detail: CC_LILAS[lila] || lila });
}
type ChapterInfo = {
number: number;
title: string;
totalVerses: number;
};
type LilaInfo = {
key: string;
name: string;
totalChapters: number;
totalVerses: number;
chapters: ChapterInfo[];
};
export default async function LilaPage({
params,
}: {
params: Promise<{ lila: string }>;
}) {
const { lila } = await params;
const meta = await getBookMeta("cc");
if (!meta) notFound();
const lilas = meta.lilas as LilaInfo[];
const lilaInfo = lilas.find((l) => l.key === lila);
// Front matter — the param is not a līlā key, so resolve it as a prose
// intro-segment (front-flap, preface, introduction).
if (!lilaInfo) {
const segment = await getProseChapter("cc", 0, lila);
if (!segment) notFound();
return (
<div className="min-h-screen bg-background">
<PageHeader sticky>
<Link
href="/books/cc"
className="text-sm text-muted-foreground hover:text-foreground transition-colors"
>
&larr; Śrī Caitanya-caritāmṛta
</Link>
<div className="mt-3 flex items-start justify-between gap-3">
<h1 className="text-2xl font-bold text-foreground">
{segment.title}
</h1>
<FindInPage targetId="book-content" className="shrink-0 mt-0.5" />
</div>
</PageHeader>
<main className="max-w-5xl mx-auto px-6 py-8">
<Card id="book-content">
<CardContent className="py-8 px-6 sm:px-8">
<div className="space-y-4">
{segment.paragraphs.map((paragraph, i) => (
<div
key={i}
className="text-base text-foreground leading-relaxed"
dangerouslySetInnerHTML={{ __html: paragraph }}
/>
))}
</div>
</CardContent>
</Card>
</main>
</div>
);
}
const totalVerses = lilaInfo.chapters.reduce(
(sum, ch) => sum + ch.totalVerses,
0,
);
return (
<div className="min-h-screen bg-background">
<PageHeader>
<Link
href="/books/cc"
className="text-sm text-muted-foreground hover:text-foreground"
>
&larr; Śrī Caitanya-caritāmṛta
</Link>
<h1 className="mt-3 text-3xl font-bold text-foreground">
{lilaInfo.name}
</h1>
<p className="mt-1 text-muted-foreground">
{lilaInfo.totalChapters} chapters ·{" "}
<ExtractedVerseCount count={totalVerses} label="extracted verses" />
</p>
</PageHeader>
<main className="max-w-5xl mx-auto px-6 py-8">
<Card className="divide-y divide-border">
<CardContent className="p-0">
<ul className="divide-y divide-border">
{lilaInfo.chapters.map((ch) => (
<li key={ch.number}>
<Link
href={`/books/cc/${lila}/${ch.number}`}
className="flex flex-col sm:flex-row sm:items-center sm:justify-between px-5 py-4 gap-1 sm:gap-0 hover:bg-muted transition-colors"
>
<div>
<span className="text-sm text-primary font-medium">
Chapter {ch.number}
</span>
<p className="font-medium text-foreground">{ch.title}</p>
</div>
<span className="text-sm text-muted-foreground sm:shrink-0 sm:ml-4">
<ExtractedVerseCount count={ch.totalVerses} />
</span>
</Link>
</li>
))}
</ul>
</CardContent>
</Card>
</main>
</div>
);
}