Files
spsays-training/app/books/bg/[chapter]/page.tsx
T
2026-04-14 19:12:11 +05:30

74 lines
2.0 KiB
TypeScript

import Link from "next/link";
import { PageHeader } from "@/components/page-header";
import { notFound } from "next/navigation";
import { getBookMeta, getChapterVerses, getChapterVerseIndex } from "@/lib/api";
import { ChapterViewer } from "@/app/books/chapter-viewer";
type BgMeta = {
book: string;
title: string;
author: string;
totalChapters: number;
chapters: { number: number; title: string; totalVerses: number }[];
};
export default async function ChapterPage({
params,
}: {
params: Promise<{ chapter: string }>;
}) {
const { chapter } = await params;
const PAGE_SIZE = 20;
const meta = await getBookMeta("bg");
if (!meta) notFound();
const bgMeta = meta as unknown as BgMeta;
const chapterInfo = bgMeta.chapters?.find(
(c) => c.number === parseInt(chapter),
);
if (!chapterInfo?.title) notFound();
const title: string = chapterInfo.title;
const [verseIndex, firstPage] = await Promise.all([
getChapterVerseIndex("bg", null, parseInt(chapter)),
getChapterVerses("bg", null, parseInt(chapter), 1, PAGE_SIZE),
]);
if (verseIndex.length === 0) notFound();
return (
<div className="min-h-screen bg-background">
<PageHeader sticky wide>
<Link
href="/books/bg"
className="text-sm text-muted-foreground hover:text-foreground"
>
&larr; Bhagavad-gītā
</Link>
<h1 className="mt-2 text-2xl font-bold text-foreground">
Chapter {chapter}: {title}
</h1>
</PageHeader>
<div className="w-full md:max-w-5xl mx-auto flex">
<ChapterViewer
initialVerses={firstPage.verses}
hasMore={firstPage.hasMore}
totalPages={firstPage.totalPages}
book="bg"
division={null}
chapter={parseInt(chapter)}
limit={PAGE_SIZE}
showBengali={false}
verseIndex={verseIndex.map((v) => ({
verse: v.verse,
verseEnd: v.verseEnd ?? undefined,
reference: v.reference,
}))}
/>
</div>
</div>
);
}