feat: add pagination

This commit is contained in:
gosangam
2026-04-14 23:15:26 +05:30
parent beffb9fae1
commit 213eea8e94
7 changed files with 428 additions and 406 deletions
+22 -14
View File
@@ -1,7 +1,7 @@
import Link from "next/link";
import { PageHeader } from "@/components/page-header";
import { notFound } from "next/navigation";
import { getBookMeta, getChapterVerses, getChapterVerseIndex } from "@/lib/api";
import { getBookMeta, getSingleVerse, getChapterVerseIndex } from "@/lib/api";
import { ChapterViewer } from "@/app/books/chapter-viewer";
type ChapterInfo = {
@@ -19,11 +19,13 @@ type CantoInfo = {
export default async function SbChapterPage({
params,
searchParams,
}: {
params: Promise<{ canto: string; chapter: string }>;
searchParams: Promise<{ verse?: string }>;
}) {
const { canto, chapter } = await params;
const PAGE_SIZE = 20;
const { verse: verseParam } = await searchParams;
const meta = await getBookMeta("sb");
if (!meta) notFound();
@@ -37,12 +39,20 @@ export default async function SbChapterPage({
);
if (!chapterInfo) notFound();
const [verseIndex, firstPage] = await Promise.all([
getChapterVerseIndex("sb", canto, parseInt(chapter)),
getChapterVerses("sb", canto, parseInt(chapter), 1, PAGE_SIZE),
]);
const verseIndex = await getChapterVerseIndex("sb", canto, parseInt(chapter));
if (verseIndex.length === 0) notFound();
const requestedVerse = verseParam
? parseInt(verseParam)
: verseIndex[0].verse;
const verse = await getSingleVerse(
"sb",
canto,
parseInt(chapter),
requestedVerse,
);
if (!verse) notFound();
return (
<div className="min-h-screen bg-background">
<PageHeader sticky wide>
@@ -59,19 +69,17 @@ export default async function SbChapterPage({
<div className="w-full md:max-w-5xl mx-auto flex">
<ChapterViewer
initialVerses={firstPage.verses}
hasMore={firstPage.hasMore}
totalPages={firstPage.totalPages}
book="sb"
division={canto}
chapter={parseInt(chapter)}
limit={PAGE_SIZE}
showBengali={false}
initialVerse={verse}
initialVerseNum={requestedVerse}
verseIndex={verseIndex.map((v) => ({
verse: v.verse,
verseEnd: v.verseEnd ?? undefined,
reference: v.reference,
}))}
book="sb"
division={canto}
chapter={parseInt(chapter)}
showBengali={false}
/>
</div>
</div>