mongo fix
This commit is contained in:
+39
-138
@@ -1,76 +1,41 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import Link from "next/link";
|
||||
import { PageHeader } from "@/components/page-header";
|
||||
import { notFound } from "next/navigation";
|
||||
import { Card, CardHeader, CardContent } from "@/components/ui/card";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { VerseIndex } from "./verse-index";
|
||||
import { getBookMeta, getChapterVerses, getChapterVerseIndex } from "@/lib/api";
|
||||
import { ChapterViewer } from "@/app/books/chapter-viewer";
|
||||
|
||||
type Synonym = {
|
||||
word: string;
|
||||
meanings: string[];
|
||||
type BgMeta = {
|
||||
book: string;
|
||||
title: string;
|
||||
author: string;
|
||||
totalChapters: number;
|
||||
chapters: { number: number; title: string; totalVerses: number }[];
|
||||
};
|
||||
|
||||
type Verse = {
|
||||
book_title: string;
|
||||
chapter: number;
|
||||
verse: number;
|
||||
verse_end?: number;
|
||||
reference: string;
|
||||
devanagari: string;
|
||||
transliteration: string;
|
||||
synonyms: Synonym[];
|
||||
translation: string;
|
||||
purport: string[];
|
||||
};
|
||||
|
||||
function getChapterVerses(chapter: string): Verse[] {
|
||||
const dir = path.join(
|
||||
process.cwd(),
|
||||
"app/lib/books/bg/chapters",
|
||||
chapter.padStart(2, "0"),
|
||||
"verses",
|
||||
);
|
||||
if (!fs.existsSync(dir)) return [];
|
||||
|
||||
return fs
|
||||
.readdirSync(dir)
|
||||
.filter((f) => f.endsWith(".json"))
|
||||
.sort((a, b) => {
|
||||
const na = parseInt(a);
|
||||
const nb = parseInt(b);
|
||||
return na - nb;
|
||||
})
|
||||
.map((f) => JSON.parse(fs.readFileSync(path.join(dir, f), "utf-8")));
|
||||
}
|
||||
|
||||
function getChapterTitle(chapter: string): string | null {
|
||||
const introPath = path.join(process.cwd(), "app/lib/books/bg/intro.json");
|
||||
const intro = JSON.parse(fs.readFileSync(introPath, "utf-8"));
|
||||
const ch = intro.chapters.find(
|
||||
(c: { number: number }) => c.number === parseInt(chapter),
|
||||
);
|
||||
return ch?.title || null;
|
||||
}
|
||||
|
||||
export default async function ChapterPage({
|
||||
params,
|
||||
}: {
|
||||
params: Promise<{ chapter: string }>;
|
||||
}) {
|
||||
const { chapter } = await params;
|
||||
const title = getChapterTitle(chapter);
|
||||
if (!title) notFound();
|
||||
const PAGE_SIZE = 20;
|
||||
|
||||
const verses = getChapterVerses(chapter);
|
||||
if (verses.length === 0) notFound();
|
||||
const meta = await getBookMeta("bg");
|
||||
if (!meta) notFound();
|
||||
|
||||
const verseList = verses.map((v) => ({
|
||||
verse: v.verse,
|
||||
verse_end: v.verse_end,
|
||||
reference: v.reference,
|
||||
}));
|
||||
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">
|
||||
@@ -87,85 +52,21 @@ export default async function ChapterPage({
|
||||
</PageHeader>
|
||||
|
||||
<div className="w-full md:max-w-5xl mx-auto flex">
|
||||
{/* Left sidebar — verse index */}
|
||||
<VerseIndex verses={verseList} chapter={chapter} />
|
||||
|
||||
{/* Main content */}
|
||||
<main className="flex-1 min-w-0 px-6 py-8 space-y-12">
|
||||
{verses.map((verse) => (
|
||||
<Card
|
||||
key={verse.reference}
|
||||
id={`verse-${verse.verse}`}
|
||||
className="scroll-mt-24"
|
||||
>
|
||||
{/* Header */}
|
||||
<CardHeader className="border-b border-border bg-muted/50 py-3">
|
||||
<Badge variant="secondary">{verse.reference}</Badge>
|
||||
</CardHeader>
|
||||
|
||||
<CardContent className="space-y-5 pt-5">
|
||||
{/* Devanagari */}
|
||||
<div className="text-center">
|
||||
<p className="text-xl leading-relaxed text-red-800 dark:text-red-400 whitespace-pre-line font-serif">
|
||||
{verse.devanagari}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Transliteration */}
|
||||
<div className="text-center">
|
||||
<p className="text-base text-muted-foreground italic whitespace-pre-line">
|
||||
{verse.transliteration}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Synonyms */}
|
||||
{verse.synonyms.length > 0 && (
|
||||
<div>
|
||||
<h3 className="text-xs font-bold uppercase tracking-wider text-muted-foreground mb-2">
|
||||
Synonyms
|
||||
</h3>
|
||||
<p className="text-sm text-muted-foreground leading-relaxed">
|
||||
{verse.synonyms.map((s, i) => (
|
||||
<span key={i}>
|
||||
<span className="text-foreground italic">
|
||||
{s.word}
|
||||
</span>
|
||||
—{s.meanings.join(", ")}
|
||||
{i < verse.synonyms.length - 1 ? "; " : "."}
|
||||
</span>
|
||||
))}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Translation */}
|
||||
<div>
|
||||
<h3 className="text-xs font-bold uppercase tracking-wider text-muted-foreground mb-2">
|
||||
Translation
|
||||
</h3>
|
||||
<p
|
||||
className="text-base text-foreground leading-relaxed font-medium"
|
||||
dangerouslySetInnerHTML={{ __html: verse.translation }}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Purport */}
|
||||
{verse.purport.length > 0 && (
|
||||
<div>
|
||||
<h3 className="text-xs font-bold uppercase tracking-wider text-muted-foreground mb-2">
|
||||
Purport
|
||||
</h3>
|
||||
<div className="text-sm text-muted-foreground leading-relaxed space-y-3">
|
||||
{verse.purport.map((p, i) => (
|
||||
<p key={i} dangerouslySetInnerHTML={{ __html: p }} />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
))}
|
||||
</main>
|
||||
<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>
|
||||
);
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
type VerseEntry = {
|
||||
verse: number;
|
||||
verse_end?: number;
|
||||
reference: string;
|
||||
};
|
||||
|
||||
export function VerseIndex({
|
||||
verses,
|
||||
chapter,
|
||||
}: {
|
||||
verses: VerseEntry[];
|
||||
chapter: string;
|
||||
}) {
|
||||
const [activeVerse, setActiveVerse] = useState<number | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const observer = new IntersectionObserver(
|
||||
(entries) => {
|
||||
for (const entry of entries) {
|
||||
if (entry.isIntersecting) {
|
||||
const id = entry.target.id;
|
||||
const num = parseInt(id.replace("verse-", ""));
|
||||
if (!isNaN(num)) setActiveVerse(num);
|
||||
}
|
||||
}
|
||||
},
|
||||
{ rootMargin: "-20% 0px -60% 0px" },
|
||||
);
|
||||
|
||||
const articles = document.querySelectorAll("article[id^='verse-']");
|
||||
articles.forEach((el) => observer.observe(el));
|
||||
|
||||
return () => observer.disconnect();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<aside className="hidden lg:block w-48 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">
|
||||
Verses
|
||||
</h2>
|
||||
<nav className="grid grid-cols-3 gap-0.5">
|
||||
{verses.map((v) => (
|
||||
<a
|
||||
key={v.verse}
|
||||
href={`#verse-${v.verse}`}
|
||||
className={`px-2 py-1 rounded-md text-sm text-center transition-colors ${
|
||||
activeVerse === v.verse
|
||||
? "bg-primary/10 text-primary font-medium"
|
||||
: "text-muted-foreground hover:bg-muted hover:text-foreground"
|
||||
}`}
|
||||
>
|
||||
{v.verse_end ? `${v.verse}–${v.verse_end}` : v.verse}
|
||||
</a>
|
||||
))}
|
||||
</nav>
|
||||
</aside>
|
||||
);
|
||||
}
|
||||
+12
-12
@@ -1,6 +1,6 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import Link from "next/link";
|
||||
import { notFound } from "next/navigation";
|
||||
import { getBookMeta } from "@/lib/api";
|
||||
import { PageHeader } from "@/components/page-header";
|
||||
import { Card, CardContent } from "@/components/ui/card";
|
||||
import { ExtractedVerseCount } from "../extracted-verse-count";
|
||||
@@ -11,17 +11,17 @@ type ChapterInfo = {
|
||||
totalVerses: number;
|
||||
};
|
||||
|
||||
type Intro = {
|
||||
book: string;
|
||||
title: string;
|
||||
author: string;
|
||||
totalChapters: number;
|
||||
chapters: ChapterInfo[];
|
||||
};
|
||||
export default async function BgPage() {
|
||||
const meta = await getBookMeta("bg");
|
||||
if (!meta) notFound();
|
||||
|
||||
export default function BgPage() {
|
||||
const introPath = path.join(process.cwd(), "app/lib/books/bg/intro.json");
|
||||
const intro: Intro = JSON.parse(fs.readFileSync(introPath, "utf-8"));
|
||||
const intro = meta as unknown as {
|
||||
book: string;
|
||||
title: string;
|
||||
author: string;
|
||||
totalChapters: number;
|
||||
chapters: ChapterInfo[];
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-background">
|
||||
|
||||
@@ -1,31 +1,8 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import Link from "next/link";
|
||||
import { PageHeader } from "@/components/page-header";
|
||||
import { notFound } from "next/navigation";
|
||||
import { Card, CardHeader, CardContent } from "@/components/ui/card";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { VerseIndex } from "./verse-index";
|
||||
|
||||
type Synonym = {
|
||||
word: string;
|
||||
meanings: string[];
|
||||
};
|
||||
|
||||
type Verse = {
|
||||
book_title: string;
|
||||
lila: string;
|
||||
chapter: number;
|
||||
verse: number;
|
||||
verse_end?: number | null;
|
||||
reference: string;
|
||||
devanagari: string;
|
||||
bengali: string;
|
||||
transliteration: string;
|
||||
synonyms: Synonym[];
|
||||
translation: string;
|
||||
purport: string[];
|
||||
};
|
||||
import { getBookMeta, getChapterVerses, getChapterVerseIndex } from "@/lib/api";
|
||||
import { ChapterViewer } from "@/app/books/chapter-viewer";
|
||||
|
||||
type ChapterInfo = {
|
||||
number: number;
|
||||
@@ -41,70 +18,31 @@ type LilaInfo = {
|
||||
chapters: ChapterInfo[];
|
||||
};
|
||||
|
||||
type Intro = {
|
||||
book: string;
|
||||
title: string;
|
||||
author: string;
|
||||
totalLilas: number;
|
||||
lilas: LilaInfo[];
|
||||
};
|
||||
|
||||
function getChapterVerses(lila: string, chapter: string): Verse[] {
|
||||
const dir = path.join(
|
||||
process.cwd(),
|
||||
"app/lib/books/cc/lilas",
|
||||
lila,
|
||||
"chapters",
|
||||
chapter.padStart(2, "0"),
|
||||
"verses",
|
||||
);
|
||||
if (!fs.existsSync(dir)) return [];
|
||||
|
||||
return fs
|
||||
.readdirSync(dir)
|
||||
.filter((f) => f.endsWith(".json"))
|
||||
.sort((a, b) => {
|
||||
const na = parseInt(a);
|
||||
const nb = parseInt(b);
|
||||
return na - nb;
|
||||
})
|
||||
.map((f) => JSON.parse(fs.readFileSync(path.join(dir, f), "utf-8")));
|
||||
}
|
||||
|
||||
function getLilaAndChapterInfo(
|
||||
lila: string,
|
||||
chapter: string,
|
||||
): { lilaInfo: LilaInfo; chapterInfo: ChapterInfo } | null {
|
||||
const introPath = path.join(process.cwd(), "app/lib/books/cc/intro.json");
|
||||
const intro: Intro = JSON.parse(fs.readFileSync(introPath, "utf-8"));
|
||||
const lilaInfo = intro.lilas.find((l) => l.key === lila);
|
||||
if (!lilaInfo) return null;
|
||||
const chapterInfo = lilaInfo.chapters.find(
|
||||
(ch) => ch.number === parseInt(chapter),
|
||||
);
|
||||
if (!chapterInfo) return null;
|
||||
return { lilaInfo, chapterInfo };
|
||||
}
|
||||
|
||||
export default async function CcChapterPage({
|
||||
params,
|
||||
}: {
|
||||
params: Promise<{ lila: string; chapter: string }>;
|
||||
}) {
|
||||
const { lila, chapter } = await params;
|
||||
const info = getLilaAndChapterInfo(lila, chapter);
|
||||
if (!info) notFound();
|
||||
const PAGE_SIZE = 20;
|
||||
|
||||
const { lilaInfo, chapterInfo } = info;
|
||||
const meta = await getBookMeta("cc");
|
||||
if (!meta) notFound();
|
||||
|
||||
const verses = getChapterVerses(lila, chapter);
|
||||
if (verses.length === 0) notFound();
|
||||
const lilas = meta.lilas as LilaInfo[] | undefined;
|
||||
const lilaInfo = lilas?.find((l) => l.key === lila);
|
||||
if (!lilaInfo) notFound();
|
||||
|
||||
const verseList = verses.map((v) => ({
|
||||
verse: v.verse,
|
||||
verse_end: v.verse_end ?? undefined,
|
||||
reference: v.reference,
|
||||
}));
|
||||
const chapterInfo = lilaInfo.chapters.find(
|
||||
(ch) => ch.number === parseInt(chapter),
|
||||
);
|
||||
if (!chapterInfo) notFound();
|
||||
|
||||
const [verseIndex, firstPage] = await Promise.all([
|
||||
getChapterVerseIndex("cc", lila, parseInt(chapter)),
|
||||
getChapterVerses("cc", lila, parseInt(chapter), 1, PAGE_SIZE),
|
||||
]);
|
||||
if (verseIndex.length === 0) notFound();
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-background">
|
||||
@@ -121,94 +59,21 @@ export default async function CcChapterPage({
|
||||
</PageHeader>
|
||||
|
||||
<div className="w-full md:max-w-5xl mx-auto flex">
|
||||
{/* Left sidebar — verse index */}
|
||||
<VerseIndex verses={verseList} chapter={chapter} />
|
||||
|
||||
{/* Main content */}
|
||||
<main className="flex-1 min-w-0 px-6 py-8 space-y-12">
|
||||
{verses.map((verse) => (
|
||||
<Card
|
||||
key={verse.reference}
|
||||
id={`verse-${verse.verse}`}
|
||||
className="scroll-mt-24"
|
||||
>
|
||||
{/* Header */}
|
||||
<CardHeader className="border-b border-border bg-muted/50 py-3">
|
||||
<Badge variant="secondary">{verse.reference}</Badge>
|
||||
</CardHeader>
|
||||
|
||||
<CardContent className="space-y-5 pt-5">
|
||||
{/* Bengali */}
|
||||
{verse.bengali && (
|
||||
<div className="text-center">
|
||||
<p className="text-xl leading-relaxed whitespace-pre-line font-serif text-green-800 dark:text-green-400">
|
||||
{verse.bengali}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Devanagari */}
|
||||
<div className="text-center">
|
||||
<p className="text-xl leading-relaxed text-red-800 dark:text-red-400 whitespace-pre-line font-serif">
|
||||
{verse.devanagari}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Transliteration */}
|
||||
<div className="text-center">
|
||||
<p className="text-base italic text-muted-foreground whitespace-pre-line">
|
||||
{verse.transliteration}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Synonyms */}
|
||||
{verse.synonyms.length > 0 && (
|
||||
<div>
|
||||
<h3 className="text-xs font-bold uppercase tracking-wider text-muted-foreground mb-2">
|
||||
Synonyms
|
||||
</h3>
|
||||
<p className="text-sm text-muted-foreground leading-relaxed">
|
||||
{verse.synonyms.map((s, i) => (
|
||||
<span key={i}>
|
||||
<span className="italic text-foreground">
|
||||
{s.word}
|
||||
</span>
|
||||
—{s.meanings.join(", ")}
|
||||
{i < verse.synonyms.length - 1 ? "; " : "."}
|
||||
</span>
|
||||
))}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Translation */}
|
||||
<div>
|
||||
<h3 className="text-xs font-bold uppercase tracking-wider text-muted-foreground mb-2">
|
||||
Translation
|
||||
</h3>
|
||||
<p
|
||||
className="text-base text-foreground leading-relaxed font-medium"
|
||||
dangerouslySetInnerHTML={{ __html: verse.translation }}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Purport */}
|
||||
{verse.purport.length > 0 && (
|
||||
<div>
|
||||
<h3 className="text-xs font-bold uppercase tracking-wider text-muted-foreground mb-2">
|
||||
Purport
|
||||
</h3>
|
||||
<div className="text-sm text-muted-foreground leading-relaxed space-y-3">
|
||||
{verse.purport.map((p, i) => (
|
||||
<p key={i} dangerouslySetInnerHTML={{ __html: p }} />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
))}
|
||||
</main>
|
||||
<ChapterViewer
|
||||
initialVerses={firstPage.verses}
|
||||
hasMore={firstPage.hasMore}
|
||||
totalPages={firstPage.totalPages}
|
||||
book="cc"
|
||||
division={lila}
|
||||
chapter={parseInt(chapter)}
|
||||
limit={PAGE_SIZE}
|
||||
showBengali={true}
|
||||
verseIndex={verseIndex.map((v) => ({
|
||||
verse: v.verse,
|
||||
verseEnd: v.verseEnd ?? undefined,
|
||||
reference: v.reference,
|
||||
}))}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
type VerseEntry = {
|
||||
verse: number;
|
||||
verse_end?: number;
|
||||
reference: string;
|
||||
};
|
||||
|
||||
export function VerseIndex({
|
||||
verses,
|
||||
chapter,
|
||||
}: {
|
||||
verses: VerseEntry[];
|
||||
chapter: string;
|
||||
}) {
|
||||
const [activeVerse, setActiveVerse] = useState<number | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const observer = new IntersectionObserver(
|
||||
(entries) => {
|
||||
for (const entry of entries) {
|
||||
if (entry.isIntersecting) {
|
||||
const id = entry.target.id;
|
||||
const num = parseInt(id.replace("verse-", ""));
|
||||
if (!isNaN(num)) setActiveVerse(num);
|
||||
}
|
||||
}
|
||||
},
|
||||
{ rootMargin: "-20% 0px -60% 0px" },
|
||||
);
|
||||
|
||||
const articles = document.querySelectorAll("article[id^='verse-']");
|
||||
articles.forEach((el) => observer.observe(el));
|
||||
|
||||
return () => observer.disconnect();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<aside className="hidden lg:block w-48 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">
|
||||
Verses
|
||||
</h2>
|
||||
<nav className="grid grid-cols-3 gap-0.5">
|
||||
{verses.map((v) => (
|
||||
<a
|
||||
key={v.verse}
|
||||
href={`#verse-${v.verse}`}
|
||||
className={`px-2 py-1 rounded-md text-sm text-center transition-colors ${
|
||||
activeVerse === v.verse
|
||||
? "bg-primary/10 text-primary font-medium"
|
||||
: "text-muted-foreground hover:bg-muted hover:text-foreground"
|
||||
}`}
|
||||
>
|
||||
{v.verse_end ? `${v.verse}–${v.verse_end}` : v.verse}
|
||||
</a>
|
||||
))}
|
||||
</nav>
|
||||
</aside>
|
||||
);
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import Link from "next/link";
|
||||
import { PageHeader } from "@/components/page-header";
|
||||
import { notFound } from "next/navigation";
|
||||
import { Card, CardContent } from "@/components/ui/card";
|
||||
import { ExtractedVerseCount } from "../../extracted-verse-count";
|
||||
import { getBookMeta } from "@/lib/api";
|
||||
|
||||
type ChapterInfo = {
|
||||
number: number;
|
||||
@@ -20,28 +19,17 @@ type LilaInfo = {
|
||||
chapters: ChapterInfo[];
|
||||
};
|
||||
|
||||
type Intro = {
|
||||
book: string;
|
||||
title: string;
|
||||
author: string;
|
||||
totalLilas: number;
|
||||
lilas: LilaInfo[];
|
||||
};
|
||||
|
||||
function getIntro(): Intro {
|
||||
const introPath = path.join(process.cwd(), "app/lib/books/cc/intro.json");
|
||||
return JSON.parse(fs.readFileSync(introPath, "utf-8"));
|
||||
}
|
||||
|
||||
export default async function LilaPage({
|
||||
params,
|
||||
}: {
|
||||
params: Promise<{ lila: string }>;
|
||||
}) {
|
||||
const { lila } = await params;
|
||||
const intro = getIntro();
|
||||
const meta = await getBookMeta("cc");
|
||||
if (!meta) notFound();
|
||||
|
||||
const lilaInfo = intro.lilas.find((l) => l.key === lila);
|
||||
const lilas = meta.lilas as LilaInfo[];
|
||||
const lilaInfo = lilas.find((l) => l.key === lila);
|
||||
if (!lilaInfo) notFound();
|
||||
|
||||
const totalVerses = lilaInfo.chapters.reduce(
|
||||
|
||||
+16
-17
@@ -1,6 +1,5 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import Link from "next/link";
|
||||
import { getBookMeta } from "@/lib/api";
|
||||
import { PageHeader } from "@/components/page-header";
|
||||
import { Card, CardContent } from "@/components/ui/card";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
@@ -20,17 +19,19 @@ type LilaInfo = {
|
||||
chapters: ChapterInfo[];
|
||||
};
|
||||
|
||||
type Intro = {
|
||||
book: string;
|
||||
title: string;
|
||||
author: string;
|
||||
totalLilas: number;
|
||||
lilas: LilaInfo[];
|
||||
};
|
||||
export default async function CcPage() {
|
||||
const meta = await getBookMeta("cc");
|
||||
if (!meta) {
|
||||
return (
|
||||
<div className="p-8 text-center text-muted-foreground">
|
||||
Book metadata not found.
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function CcPage() {
|
||||
const introPath = path.join(process.cwd(), "app/lib/books/cc/intro.json");
|
||||
const intro: Intro = JSON.parse(fs.readFileSync(introPath, "utf-8"));
|
||||
const lilas = meta.lilas as LilaInfo[];
|
||||
const title = meta.title as string;
|
||||
const author = meta.author as string;
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-background">
|
||||
@@ -41,16 +42,14 @@ export default function CcPage() {
|
||||
>
|
||||
← Books
|
||||
</Link>
|
||||
<h1 className="mt-3 text-3xl font-bold text-foreground">
|
||||
{intro.title}
|
||||
</h1>
|
||||
<p className="mt-1 text-muted-foreground">{intro.author}</p>
|
||||
<h1 className="mt-3 text-3xl font-bold text-foreground">{title}</h1>
|
||||
<p className="mt-1 text-muted-foreground">{author}</p>
|
||||
</PageHeader>
|
||||
<main className="max-w-3xl mx-auto px-6 py-8">
|
||||
<Card className="divide-y divide-border">
|
||||
<CardContent className="p-0">
|
||||
<ul className="divide-y divide-border">
|
||||
{intro.lilas.map((lila) => {
|
||||
{lilas.map((lila) => {
|
||||
const totalVerses = lila.chapters.reduce(
|
||||
(sum, ch) => sum + ch.totalVerses,
|
||||
0,
|
||||
|
||||
@@ -0,0 +1,476 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef, useState, useCallback } from "react";
|
||||
import { Card, CardHeader, CardContent } from "@/components/ui/card";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Loader2 } from "lucide-react";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
type Synonym = {
|
||||
word: string;
|
||||
meanings: string[];
|
||||
};
|
||||
|
||||
type Verse = {
|
||||
id: string;
|
||||
book: string;
|
||||
division: string | null;
|
||||
chapter: number;
|
||||
verse: number;
|
||||
verseEnd: number | null;
|
||||
reference: string;
|
||||
bookTitle: string;
|
||||
devanagari: string;
|
||||
bengali: string | null;
|
||||
transliteration: string;
|
||||
synonyms: Synonym[];
|
||||
translation: string;
|
||||
purport: string[];
|
||||
};
|
||||
|
||||
type PaginatedResponse = {
|
||||
verses: Verse[];
|
||||
total: number;
|
||||
page: number;
|
||||
limit: number;
|
||||
totalPages: number;
|
||||
hasMore: boolean;
|
||||
};
|
||||
|
||||
type VerseIndexEntry = {
|
||||
verse: number;
|
||||
verseEnd?: number;
|
||||
reference: string;
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Props
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
type ChapterViewerProps = {
|
||||
initialVerses: Verse[];
|
||||
hasMore: boolean;
|
||||
totalPages: number;
|
||||
book: string;
|
||||
division: string | null;
|
||||
chapter: number;
|
||||
limit: number;
|
||||
showBengali?: boolean;
|
||||
verseIndex: VerseIndexEntry[];
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ChapterViewer
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* ChapterViewer — a single "use client" component that owns both the sidebar
|
||||
* verse index and the main verse list with infinite scroll + jump-to-verse.
|
||||
*
|
||||
* This avoids the server→client boundary issue of passing functions as props
|
||||
* (render props / callbacks can't cross that boundary).
|
||||
*
|
||||
* The server component only passes serialisable data:
|
||||
* - initialVerses (page 1 of full verse data, SSR'd)
|
||||
* - verseIndex (lightweight list of ALL verse numbers/references)
|
||||
* - pagination metadata (hasMore, totalPages, book, division, chapter, limit)
|
||||
*/
|
||||
export function ChapterViewer({
|
||||
initialVerses,
|
||||
hasMore: initialHasMore,
|
||||
totalPages: initialTotalPages,
|
||||
book,
|
||||
division,
|
||||
chapter,
|
||||
limit,
|
||||
showBengali = false,
|
||||
verseIndex,
|
||||
}: ChapterViewerProps) {
|
||||
// ------ Verse data state ------
|
||||
const [verses, setVerses] = useState<Verse[]>(initialVerses);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [hasMorePages, setHasMorePages] = useState(initialHasMore);
|
||||
const [highestPageFetched, setHighestPageFetched] = useState(1);
|
||||
const [totalPagesKnown, setTotalPagesKnown] = useState(initialTotalPages);
|
||||
const fetchingRef = useRef(false);
|
||||
const sentinelRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
// ------ Sidebar state ------
|
||||
const [activeVerse, setActiveVerse] = useState<number | null>(null);
|
||||
const [loadedVerses, setLoadedVerses] = useState<Set<number>>(() => {
|
||||
const set = new Set<number>();
|
||||
for (const v of initialVerses) set.add(v.verse);
|
||||
return set;
|
||||
});
|
||||
const [pendingVerse, setPendingVerse] = useState<number | null>(null);
|
||||
|
||||
// Reset when route changes (different chapter)
|
||||
useEffect(() => {
|
||||
setVerses(initialVerses);
|
||||
setHasMorePages(initialHasMore);
|
||||
setHighestPageFetched(1);
|
||||
setTotalPagesKnown(initialTotalPages);
|
||||
const set = new Set<number>();
|
||||
for (const v of initialVerses) set.add(v.verse);
|
||||
setLoadedVerses(set);
|
||||
setPendingVerse(null);
|
||||
setActiveVerse(null);
|
||||
}, [initialVerses, initialHasMore, initialTotalPages]);
|
||||
|
||||
// ---- Update loadedVerses whenever verses array changes ----
|
||||
useEffect(() => {
|
||||
const set = new Set<number>();
|
||||
for (const v of verses) set.add(v.verse);
|
||||
setLoadedVerses(set);
|
||||
}, [verses]);
|
||||
|
||||
// =====================================================================
|
||||
// Fetching
|
||||
// =====================================================================
|
||||
|
||||
const fetchPage = useCallback(
|
||||
async (page: number): Promise<PaginatedResponse | null> => {
|
||||
const params = new URLSearchParams({
|
||||
chapter: String(chapter),
|
||||
page: String(page),
|
||||
limit: String(limit),
|
||||
});
|
||||
if (division !== null) {
|
||||
params.set("division", division);
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/books/${book}/verses?${params}`);
|
||||
if (!res.ok) throw new Error(`API error: ${res.status}`);
|
||||
return await res.json();
|
||||
} catch (err) {
|
||||
console.error(`Failed to fetch page ${page}:`, err);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
[book, division, chapter, limit],
|
||||
);
|
||||
|
||||
// Fetch the next single page (for infinite scroll)
|
||||
const fetchNextPage = useCallback(async () => {
|
||||
if (fetchingRef.current || !hasMorePages) return;
|
||||
fetchingRef.current = true;
|
||||
setLoading(true);
|
||||
|
||||
const nextPage = highestPageFetched + 1;
|
||||
const data = await fetchPage(nextPage);
|
||||
|
||||
if (data) {
|
||||
setVerses((prev) => [...prev, ...data.verses]);
|
||||
setHasMorePages(data.hasMore);
|
||||
setHighestPageFetched(nextPage);
|
||||
setTotalPagesKnown(data.totalPages);
|
||||
}
|
||||
|
||||
setLoading(false);
|
||||
fetchingRef.current = false;
|
||||
}, [hasMorePages, highestPageFetched, fetchPage]);
|
||||
|
||||
// Fetch all pages up to the one containing targetVerse
|
||||
const requestVerse = useCallback(
|
||||
async (targetVerse: number) => {
|
||||
// Already loaded?
|
||||
if (verses.some((v) => v.verse === targetVerse)) {
|
||||
// Just scroll to it
|
||||
const el = document.getElementById(`verse-${targetVerse}`);
|
||||
if (el) el.scrollIntoView({ behavior: "smooth", block: "start" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (fetchingRef.current) return;
|
||||
fetchingRef.current = true;
|
||||
setLoading(true);
|
||||
setPendingVerse(targetVerse);
|
||||
|
||||
let currentHighest = highestPageFetched;
|
||||
const allNewVerses: Verse[] = [];
|
||||
let found = false;
|
||||
let moreRemaining = hasMorePages;
|
||||
|
||||
while (!found && currentHighest < totalPagesKnown) {
|
||||
const nextPage = currentHighest + 1;
|
||||
const data = await fetchPage(nextPage);
|
||||
|
||||
if (!data || data.verses.length === 0) break;
|
||||
|
||||
allNewVerses.push(...data.verses);
|
||||
currentHighest = nextPage;
|
||||
moreRemaining = data.hasMore;
|
||||
setTotalPagesKnown(data.totalPages);
|
||||
|
||||
if (data.verses.some((v) => v.verse === targetVerse)) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (allNewVerses.length > 0) {
|
||||
setVerses((prev) => [...prev, ...allNewVerses]);
|
||||
setHighestPageFetched(currentHighest);
|
||||
setHasMorePages(moreRemaining);
|
||||
}
|
||||
|
||||
setLoading(false);
|
||||
fetchingRef.current = false;
|
||||
// pendingVerse will trigger scroll via the effect below
|
||||
},
|
||||
[verses, highestPageFetched, hasMorePages, totalPagesKnown, fetchPage],
|
||||
);
|
||||
|
||||
// =====================================================================
|
||||
// Scroll to pending verse once it appears in the DOM
|
||||
// =====================================================================
|
||||
|
||||
useEffect(() => {
|
||||
if (pendingVerse === null) return;
|
||||
if (!loadedVerses.has(pendingVerse)) return;
|
||||
|
||||
requestAnimationFrame(() => {
|
||||
setTimeout(() => {
|
||||
const el = document.getElementById(`verse-${pendingVerse}`);
|
||||
if (el) {
|
||||
el.scrollIntoView({ behavior: "smooth", block: "start" });
|
||||
}
|
||||
setPendingVerse(null);
|
||||
}, 50);
|
||||
});
|
||||
}, [loadedVerses, pendingVerse]);
|
||||
|
||||
// =====================================================================
|
||||
// Infinite scroll observer (bottom sentinel)
|
||||
// =====================================================================
|
||||
|
||||
useEffect(() => {
|
||||
const sentinel = sentinelRef.current;
|
||||
if (!sentinel) return;
|
||||
|
||||
const observer = new IntersectionObserver(
|
||||
(entries) => {
|
||||
if (entries[0]?.isIntersecting && hasMorePages && !fetchingRef.current) {
|
||||
fetchNextPage();
|
||||
}
|
||||
},
|
||||
{ rootMargin: "600px" },
|
||||
);
|
||||
|
||||
observer.observe(sentinel);
|
||||
return () => observer.disconnect();
|
||||
}, [fetchNextPage, hasMorePages]);
|
||||
|
||||
// =====================================================================
|
||||
// Active verse scroll tracking (IntersectionObserver + MutationObserver)
|
||||
// =====================================================================
|
||||
|
||||
useEffect(() => {
|
||||
const intersectionObserver = new IntersectionObserver(
|
||||
(entries) => {
|
||||
for (const entry of entries) {
|
||||
if (entry.isIntersecting) {
|
||||
const num = parseInt(entry.target.id.replace("verse-", ""));
|
||||
if (!isNaN(num)) setActiveVerse(num);
|
||||
}
|
||||
}
|
||||
},
|
||||
{ rootMargin: "-20% 0px -60% 0px" },
|
||||
);
|
||||
|
||||
const observeAll = () => {
|
||||
const els = document.querySelectorAll("[id^='verse-']");
|
||||
els.forEach((el) => intersectionObserver.observe(el));
|
||||
};
|
||||
|
||||
observeAll();
|
||||
|
||||
// Watch for new verse cards from infinite scroll
|
||||
const mutationObserver = new MutationObserver((mutations) => {
|
||||
let hasNew = false;
|
||||
for (const mutation of mutations) {
|
||||
for (const node of mutation.addedNodes) {
|
||||
if (
|
||||
node instanceof HTMLElement &&
|
||||
(node.id?.startsWith("verse-") ||
|
||||
node.querySelector?.("[id^='verse-']"))
|
||||
) {
|
||||
hasNew = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (hasNew) break;
|
||||
}
|
||||
if (hasNew) observeAll();
|
||||
});
|
||||
|
||||
const main = document.querySelector("main");
|
||||
if (main) {
|
||||
mutationObserver.observe(main, { childList: true, subtree: true });
|
||||
}
|
||||
|
||||
return () => {
|
||||
intersectionObserver.disconnect();
|
||||
mutationObserver.disconnect();
|
||||
};
|
||||
}, []);
|
||||
|
||||
// =====================================================================
|
||||
// Sidebar click handler
|
||||
// =====================================================================
|
||||
|
||||
const handleIndexClick = useCallback(
|
||||
(e: React.MouseEvent, verseNum: number) => {
|
||||
e.preventDefault();
|
||||
|
||||
const el = document.getElementById(`verse-${verseNum}`);
|
||||
if (el) {
|
||||
el.scrollIntoView({ behavior: "smooth", block: "start" });
|
||||
return;
|
||||
}
|
||||
|
||||
// Not loaded — request it
|
||||
setPendingVerse(verseNum);
|
||||
requestVerse(verseNum);
|
||||
},
|
||||
[requestVerse],
|
||||
);
|
||||
|
||||
// =====================================================================
|
||||
// Render
|
||||
// =====================================================================
|
||||
|
||||
return (
|
||||
<>
|
||||
{/* ---- Sidebar: Verse Index ---- */}
|
||||
<aside className="hidden lg:block w-48 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">
|
||||
Verses
|
||||
</h2>
|
||||
<nav className="grid grid-cols-3 gap-0.5">
|
||||
{verseIndex.map((v) => {
|
||||
const isLoaded = loadedVerses.has(v.verse);
|
||||
const isActive = activeVerse === v.verse;
|
||||
const isPending = pendingVerse === v.verse;
|
||||
|
||||
return (
|
||||
<a
|
||||
key={v.verse}
|
||||
href={`#verse-${v.verse}`}
|
||||
onClick={(e) => handleIndexClick(e, v.verse)}
|
||||
className={`px-2 py-1 rounded-md text-sm text-center transition-colors ${
|
||||
isPending
|
||||
? "bg-primary/20 text-primary animate-pulse font-medium"
|
||||
: isActive
|
||||
? "bg-primary/10 text-primary font-medium"
|
||||
: isLoaded
|
||||
? "text-muted-foreground hover:bg-muted hover:text-foreground"
|
||||
: "text-muted-foreground hover:bg-muted hover:text-foreground"
|
||||
}`}
|
||||
>
|
||||
{v.verseEnd ? `${v.verse}–${v.verseEnd}` : v.verse}
|
||||
</a>
|
||||
);
|
||||
})}
|
||||
</nav>
|
||||
</aside>
|
||||
|
||||
{/* ---- Main: Verse Cards ---- */}
|
||||
<main className="flex-1 min-w-0 px-6 py-8 space-y-12">
|
||||
{verses.map((verse) => (
|
||||
<Card
|
||||
key={`${verse.book}-${verse.division}-${verse.chapter}-${verse.verse}`}
|
||||
id={`verse-${verse.verse}`}
|
||||
className="scroll-mt-24"
|
||||
>
|
||||
<CardHeader className="border-b border-border bg-muted/50 py-3">
|
||||
<Badge variant="secondary">{verse.reference}</Badge>
|
||||
</CardHeader>
|
||||
|
||||
<CardContent className="space-y-5 pt-5">
|
||||
{/* Bengali (CC only) */}
|
||||
{showBengali && verse.bengali && (
|
||||
<div className="text-center">
|
||||
<p className="text-xl leading-relaxed whitespace-pre-line font-serif text-green-800 dark:text-green-400">
|
||||
{verse.bengali}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Devanagari */}
|
||||
<div className="text-center">
|
||||
<p className="text-xl leading-relaxed text-red-800 dark:text-red-400 whitespace-pre-line font-serif">
|
||||
{verse.devanagari}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Transliteration */}
|
||||
<div className="text-center">
|
||||
<p className="text-base italic text-muted-foreground whitespace-pre-line">
|
||||
{verse.transliteration}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Synonyms */}
|
||||
{verse.synonyms.length > 0 && (
|
||||
<div>
|
||||
<h3 className="text-xs font-bold uppercase tracking-wider text-muted-foreground mb-2">
|
||||
Synonyms
|
||||
</h3>
|
||||
<p className="text-sm text-muted-foreground leading-relaxed">
|
||||
{verse.synonyms.map((s, i) => (
|
||||
<span key={i}>
|
||||
<span className="italic text-foreground">{s.word}</span>
|
||||
—{s.meanings.join(", ")}
|
||||
{i < verse.synonyms.length - 1 ? "; " : "."}
|
||||
</span>
|
||||
))}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Translation */}
|
||||
<div>
|
||||
<h3 className="text-xs font-bold uppercase tracking-wider text-muted-foreground mb-2">
|
||||
Translation
|
||||
</h3>
|
||||
<p
|
||||
className="text-base text-foreground leading-relaxed font-medium"
|
||||
dangerouslySetInnerHTML={{ __html: verse.translation }}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Purport */}
|
||||
{verse.purport.length > 0 && (
|
||||
<div>
|
||||
<h3 className="text-xs font-bold uppercase tracking-wider text-muted-foreground mb-2">
|
||||
Purport
|
||||
</h3>
|
||||
<div className="text-sm text-muted-foreground leading-relaxed space-y-3">
|
||||
{verse.purport.map((p, i) => (
|
||||
<p key={i} dangerouslySetInnerHTML={{ __html: p }} />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
))}
|
||||
|
||||
{/* Sentinel for infinite scroll */}
|
||||
<div ref={sentinelRef} className="h-1" />
|
||||
|
||||
{/* Loading indicator */}
|
||||
{loading && (
|
||||
<div className="flex items-center justify-center py-8 text-muted-foreground gap-2">
|
||||
<Loader2 className="size-4 animate-spin" />
|
||||
<span className="text-sm">Loading more verses…</span>
|
||||
</div>
|
||||
)}
|
||||
</main>
|
||||
</>
|
||||
);
|
||||
}
|
||||
+79
-75
@@ -1,5 +1,3 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import Link from "next/link";
|
||||
import { PageHeader } from "@/components/page-header";
|
||||
import {
|
||||
@@ -12,6 +10,26 @@ import {
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { ExtractedVerseCount } from "./extracted-verse-count";
|
||||
import { getAllBooks, type BookMeta } from "@/lib/api";
|
||||
|
||||
type ChapterEntry = { totalVerses: number };
|
||||
type CantoEntry = { totalChapters: number; chapters: ChapterEntry[] };
|
||||
type LilaEntry = { totalChapters: number; chapters: ChapterEntry[] };
|
||||
|
||||
interface BgMeta extends BookMeta {
|
||||
totalChapters: number;
|
||||
chapters: ChapterEntry[];
|
||||
}
|
||||
|
||||
interface SbMeta extends BookMeta {
|
||||
totalCantos: number;
|
||||
cantos: CantoEntry[];
|
||||
}
|
||||
|
||||
interface CcMeta extends BookMeta {
|
||||
totalLilas: number;
|
||||
lilas: LilaEntry[];
|
||||
}
|
||||
|
||||
type BookInfo = {
|
||||
slug: string;
|
||||
@@ -22,87 +40,73 @@ type BookInfo = {
|
||||
href: string;
|
||||
};
|
||||
|
||||
function loadBooks(): BookInfo[] {
|
||||
const booksDir = path.join(process.cwd(), "app/lib/books");
|
||||
function buildBookList(allBooks: BookMeta[]): BookInfo[] {
|
||||
const books: BookInfo[] = [];
|
||||
|
||||
// Bhagavad-gītā
|
||||
const bgIntroPath = path.join(booksDir, "bg", "intro.json");
|
||||
if (fs.existsSync(bgIntroPath)) {
|
||||
const bg = JSON.parse(fs.readFileSync(bgIntroPath, "utf-8"));
|
||||
const totalVerses = bg.chapters.reduce(
|
||||
(sum: number, ch: { totalVerses: number }) => sum + ch.totalVerses,
|
||||
0,
|
||||
);
|
||||
books.push({
|
||||
slug: "bg",
|
||||
title: bg.title,
|
||||
author: bg.author,
|
||||
description: `${bg.totalChapters} chapters`,
|
||||
totalVerses,
|
||||
href: "/books/bg",
|
||||
});
|
||||
}
|
||||
for (const data of allBooks) {
|
||||
const slug = data.book;
|
||||
const title = data.title as string;
|
||||
const author = data.author as string;
|
||||
|
||||
// Śrīmad-Bhāgavatam
|
||||
const sbIntroPath = path.join(booksDir, "sb", "intro.json");
|
||||
if (fs.existsSync(sbIntroPath)) {
|
||||
const sb = JSON.parse(fs.readFileSync(sbIntroPath, "utf-8"));
|
||||
const totalChapters = sb.cantos.reduce(
|
||||
(sum: number, c: { totalChapters: number }) => sum + c.totalChapters,
|
||||
0,
|
||||
);
|
||||
const totalVerses = sb.cantos.reduce(
|
||||
(sum: number, c: { chapters: { totalVerses: number }[] }) =>
|
||||
sum +
|
||||
c.chapters.reduce(
|
||||
(s: number, ch: { totalVerses: number }) => s + ch.totalVerses,
|
||||
0,
|
||||
),
|
||||
0,
|
||||
);
|
||||
books.push({
|
||||
slug: "sb",
|
||||
title: sb.title,
|
||||
author: sb.author,
|
||||
description: `${sb.totalCantos} cantos, ${totalChapters} chapters`,
|
||||
totalVerses,
|
||||
href: "/books/sb",
|
||||
});
|
||||
}
|
||||
|
||||
// Śrī Caitanya-caritāmṛta
|
||||
const ccIntroPath = path.join(booksDir, "cc", "intro.json");
|
||||
if (fs.existsSync(ccIntroPath)) {
|
||||
const cc = JSON.parse(fs.readFileSync(ccIntroPath, "utf-8"));
|
||||
const totalChapters = cc.lilas.reduce(
|
||||
(sum: number, l: { totalChapters: number }) => sum + l.totalChapters,
|
||||
0,
|
||||
);
|
||||
const totalVerses = cc.lilas.reduce(
|
||||
(sum: number, l: { chapters: { totalVerses: number }[] }) =>
|
||||
sum +
|
||||
l.chapters.reduce(
|
||||
(s: number, ch: { totalVerses: number }) => s + ch.totalVerses,
|
||||
0,
|
||||
),
|
||||
0,
|
||||
);
|
||||
books.push({
|
||||
slug: "cc",
|
||||
title: cc.title,
|
||||
author: cc.author,
|
||||
description: `${cc.totalLilas} līlās, ${totalChapters} chapters`,
|
||||
totalVerses,
|
||||
href: "/books/cc",
|
||||
});
|
||||
if (slug === "bg") {
|
||||
const bg = data as BgMeta;
|
||||
const totalVerses = bg.chapters.reduce(
|
||||
(sum, ch) => sum + ch.totalVerses,
|
||||
0,
|
||||
);
|
||||
books.push({
|
||||
slug,
|
||||
title,
|
||||
author,
|
||||
description: `${bg.totalChapters} chapters`,
|
||||
totalVerses,
|
||||
href: "/books/bg",
|
||||
});
|
||||
} else if (slug === "sb") {
|
||||
const sb = data as SbMeta;
|
||||
const totalChapters = sb.cantos.reduce(
|
||||
(sum, c) => sum + c.totalChapters,
|
||||
0,
|
||||
);
|
||||
const totalVerses = sb.cantos.reduce(
|
||||
(sum, c) => sum + c.chapters.reduce((s, ch) => s + ch.totalVerses, 0),
|
||||
0,
|
||||
);
|
||||
books.push({
|
||||
slug,
|
||||
title,
|
||||
author,
|
||||
description: `${sb.totalCantos} cantos, ${totalChapters} chapters`,
|
||||
totalVerses,
|
||||
href: "/books/sb",
|
||||
});
|
||||
} else if (slug === "cc") {
|
||||
const cc = data as CcMeta;
|
||||
const totalChapters = cc.lilas.reduce(
|
||||
(sum, l) => sum + l.totalChapters,
|
||||
0,
|
||||
);
|
||||
const totalVerses = cc.lilas.reduce(
|
||||
(sum, l) => sum + l.chapters.reduce((s, ch) => s + ch.totalVerses, 0),
|
||||
0,
|
||||
);
|
||||
books.push({
|
||||
slug,
|
||||
title,
|
||||
author,
|
||||
description: `${cc.totalLilas} līlās, ${totalChapters} chapters`,
|
||||
totalVerses,
|
||||
href: "/books/cc",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return books;
|
||||
}
|
||||
|
||||
export default function BooksPage() {
|
||||
const books = loadBooks();
|
||||
export default async function BooksPage() {
|
||||
const allBooks = await getAllBooks();
|
||||
const books = buildBookList(allBooks);
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-background">
|
||||
|
||||
@@ -1,30 +1,8 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import Link from "next/link";
|
||||
import { PageHeader } from "@/components/page-header";
|
||||
import { notFound } from "next/navigation";
|
||||
import { Card, CardHeader, CardContent } from "@/components/ui/card";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { VerseIndex } from "./verse-index";
|
||||
|
||||
type Synonym = {
|
||||
word: string;
|
||||
meanings: string[];
|
||||
};
|
||||
|
||||
type Verse = {
|
||||
book_title: string;
|
||||
canto: number;
|
||||
chapter: number;
|
||||
verse: number;
|
||||
verse_end?: number | null;
|
||||
reference: string;
|
||||
devanagari: string;
|
||||
transliteration: string;
|
||||
synonyms: Synonym[];
|
||||
translation: string;
|
||||
purport: string[];
|
||||
};
|
||||
import { getBookMeta, getChapterVerses, getChapterVerseIndex } from "@/lib/api";
|
||||
import { ChapterViewer } from "@/app/books/chapter-viewer";
|
||||
|
||||
type ChapterInfo = {
|
||||
number: number;
|
||||
@@ -39,70 +17,31 @@ type CantoInfo = {
|
||||
chapters: ChapterInfo[];
|
||||
};
|
||||
|
||||
type Intro = {
|
||||
book: string;
|
||||
title: string;
|
||||
author: string;
|
||||
totalCantos: number;
|
||||
cantos: CantoInfo[];
|
||||
};
|
||||
|
||||
function getChapterVerses(canto: string, chapter: string): Verse[] {
|
||||
const dir = path.join(
|
||||
process.cwd(),
|
||||
"app/lib/books/sb/cantos",
|
||||
canto.padStart(2, "0"),
|
||||
"chapters",
|
||||
chapter.padStart(2, "0"),
|
||||
"verses",
|
||||
);
|
||||
if (!fs.existsSync(dir)) return [];
|
||||
|
||||
return fs
|
||||
.readdirSync(dir)
|
||||
.filter((f) => f.endsWith(".json"))
|
||||
.sort((a, b) => {
|
||||
const na = parseInt(a);
|
||||
const nb = parseInt(b);
|
||||
return na - nb;
|
||||
})
|
||||
.map((f) => JSON.parse(fs.readFileSync(path.join(dir, f), "utf-8")));
|
||||
}
|
||||
|
||||
function getCantoAndChapterInfo(
|
||||
canto: string,
|
||||
chapter: string,
|
||||
): { cantoInfo: CantoInfo; chapterInfo: ChapterInfo } | null {
|
||||
const introPath = path.join(process.cwd(), "app/lib/books/sb/intro.json");
|
||||
const intro: Intro = JSON.parse(fs.readFileSync(introPath, "utf-8"));
|
||||
const cantoInfo = intro.cantos.find((c) => c.number === parseInt(canto));
|
||||
if (!cantoInfo) return null;
|
||||
const chapterInfo = cantoInfo.chapters.find(
|
||||
(ch) => ch.number === parseInt(chapter),
|
||||
);
|
||||
if (!chapterInfo) return null;
|
||||
return { cantoInfo, chapterInfo };
|
||||
}
|
||||
|
||||
export default async function SbChapterPage({
|
||||
params,
|
||||
}: {
|
||||
params: Promise<{ canto: string; chapter: string }>;
|
||||
}) {
|
||||
const { canto, chapter } = await params;
|
||||
const info = getCantoAndChapterInfo(canto, chapter);
|
||||
if (!info) notFound();
|
||||
const PAGE_SIZE = 20;
|
||||
|
||||
const { cantoInfo, chapterInfo } = info;
|
||||
const meta = await getBookMeta("sb");
|
||||
if (!meta) notFound();
|
||||
|
||||
const verses = getChapterVerses(canto, chapter);
|
||||
if (verses.length === 0) notFound();
|
||||
const cantos = (meta as unknown as { cantos: CantoInfo[] }).cantos;
|
||||
const cantoInfo = cantos.find((c) => c.number === parseInt(canto));
|
||||
if (!cantoInfo) notFound();
|
||||
|
||||
const verseList = verses.map((v) => ({
|
||||
verse: v.verse,
|
||||
verse_end: v.verse_end ?? undefined,
|
||||
reference: v.reference,
|
||||
}));
|
||||
const chapterInfo = cantoInfo.chapters.find(
|
||||
(ch) => ch.number === parseInt(chapter),
|
||||
);
|
||||
if (!chapterInfo) notFound();
|
||||
|
||||
const [verseIndex, firstPage] = await Promise.all([
|
||||
getChapterVerseIndex("sb", canto, parseInt(chapter)),
|
||||
getChapterVerses("sb", canto, parseInt(chapter), 1, PAGE_SIZE),
|
||||
]);
|
||||
if (verseIndex.length === 0) notFound();
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-background">
|
||||
@@ -119,85 +58,21 @@ export default async function SbChapterPage({
|
||||
</PageHeader>
|
||||
|
||||
<div className="w-full md:max-w-5xl mx-auto flex">
|
||||
{/* Left sidebar — verse index */}
|
||||
<VerseIndex verses={verseList} chapter={chapter} />
|
||||
|
||||
{/* Main content */}
|
||||
<main className="flex-1 min-w-0 px-6 py-8 space-y-12">
|
||||
{verses.map((verse) => (
|
||||
<Card
|
||||
key={verse.reference}
|
||||
id={`verse-${verse.verse}`}
|
||||
className="scroll-mt-24"
|
||||
>
|
||||
{/* Header */}
|
||||
<CardHeader className="border-b border-border bg-muted/50 py-3">
|
||||
<Badge variant="secondary">{verse.reference}</Badge>
|
||||
</CardHeader>
|
||||
|
||||
<CardContent className="space-y-5 pt-5">
|
||||
{/* Devanagari */}
|
||||
<div className="text-center">
|
||||
<p className="text-xl leading-relaxed text-red-800 dark:text-red-400 whitespace-pre-line font-serif">
|
||||
{verse.devanagari}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Transliteration */}
|
||||
<div className="text-center">
|
||||
<p className="text-base italic text-muted-foreground whitespace-pre-line">
|
||||
{verse.transliteration}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Synonyms */}
|
||||
{verse.synonyms.length > 0 && (
|
||||
<div>
|
||||
<h3 className="text-xs font-bold uppercase tracking-wider text-muted-foreground mb-2">
|
||||
Synonyms
|
||||
</h3>
|
||||
<p className="text-sm text-muted-foreground leading-relaxed">
|
||||
{verse.synonyms.map((s, i) => (
|
||||
<span key={i}>
|
||||
<span className="italic text-foreground">
|
||||
{s.word}
|
||||
</span>
|
||||
—{s.meanings.join(", ")}
|
||||
{i < verse.synonyms.length - 1 ? "; " : "."}
|
||||
</span>
|
||||
))}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Translation */}
|
||||
<div>
|
||||
<h3 className="text-xs font-bold uppercase tracking-wider text-muted-foreground mb-2">
|
||||
Translation
|
||||
</h3>
|
||||
<p
|
||||
className="text-base text-foreground leading-relaxed font-medium"
|
||||
dangerouslySetInnerHTML={{ __html: verse.translation }}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Purport */}
|
||||
{verse.purport.length > 0 && (
|
||||
<div>
|
||||
<h3 className="text-xs font-bold uppercase tracking-wider text-muted-foreground mb-2">
|
||||
Purport
|
||||
</h3>
|
||||
<div className="text-sm text-muted-foreground leading-relaxed space-y-3">
|
||||
{verse.purport.map((p, i) => (
|
||||
<p key={i} dangerouslySetInnerHTML={{ __html: p }} />
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
))}
|
||||
</main>
|
||||
<ChapterViewer
|
||||
initialVerses={firstPage.verses}
|
||||
hasMore={firstPage.hasMore}
|
||||
totalPages={firstPage.totalPages}
|
||||
book="sb"
|
||||
division={canto}
|
||||
chapter={parseInt(chapter)}
|
||||
limit={PAGE_SIZE}
|
||||
showBengali={false}
|
||||
verseIndex={verseIndex.map((v) => ({
|
||||
verse: v.verse,
|
||||
verseEnd: v.verseEnd ?? undefined,
|
||||
reference: v.reference,
|
||||
}))}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
type VerseEntry = {
|
||||
verse: number;
|
||||
verse_end?: number;
|
||||
reference: string;
|
||||
};
|
||||
|
||||
export function VerseIndex({
|
||||
verses,
|
||||
chapter,
|
||||
}: {
|
||||
verses: VerseEntry[];
|
||||
chapter: string;
|
||||
}) {
|
||||
const [activeVerse, setActiveVerse] = useState<number | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const observer = new IntersectionObserver(
|
||||
(entries) => {
|
||||
for (const entry of entries) {
|
||||
if (entry.isIntersecting) {
|
||||
const id = entry.target.id;
|
||||
const num = parseInt(id.replace("verse-", ""));
|
||||
if (!isNaN(num)) setActiveVerse(num);
|
||||
}
|
||||
}
|
||||
},
|
||||
{ rootMargin: "-20% 0px -60% 0px" },
|
||||
);
|
||||
|
||||
const articles = document.querySelectorAll("article[id^='verse-']");
|
||||
articles.forEach((el) => observer.observe(el));
|
||||
|
||||
return () => observer.disconnect();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<aside className="hidden lg:block w-48 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">
|
||||
Verses
|
||||
</h2>
|
||||
<nav className="grid grid-cols-3 gap-0.5">
|
||||
{verses.map((v) => (
|
||||
<a
|
||||
key={v.verse}
|
||||
href={`#verse-${v.verse}`}
|
||||
className={`px-2 py-1 rounded-md text-sm text-center transition-colors ${
|
||||
activeVerse === v.verse
|
||||
? "bg-primary/10 text-primary font-medium"
|
||||
: "text-muted-foreground hover:bg-muted hover:text-foreground"
|
||||
}`}
|
||||
>
|
||||
{v.verse_end ? `${v.verse}–${v.verse_end}` : v.verse}
|
||||
</a>
|
||||
))}
|
||||
</nav>
|
||||
</aside>
|
||||
);
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import Link from "next/link";
|
||||
import { PageHeader } from "@/components/page-header";
|
||||
import { notFound } from "next/navigation";
|
||||
import { Card, CardContent } from "@/components/ui/card";
|
||||
import { ExtractedVerseCount } from "../../extracted-verse-count";
|
||||
import { getBookMeta } from "@/lib/api";
|
||||
|
||||
type ChapterInfo = {
|
||||
number: number;
|
||||
@@ -19,19 +18,6 @@ type CantoInfo = {
|
||||
chapters: ChapterInfo[];
|
||||
};
|
||||
|
||||
type Intro = {
|
||||
book: string;
|
||||
title: string;
|
||||
author: string;
|
||||
totalCantos: number;
|
||||
cantos: CantoInfo[];
|
||||
};
|
||||
|
||||
function getIntro(): Intro {
|
||||
const introPath = path.join(process.cwd(), "app/lib/books/sb/intro.json");
|
||||
return JSON.parse(fs.readFileSync(introPath, "utf-8"));
|
||||
}
|
||||
|
||||
export default async function CantoPage({
|
||||
params,
|
||||
}: {
|
||||
@@ -39,9 +25,11 @@ export default async function CantoPage({
|
||||
}) {
|
||||
const { canto } = await params;
|
||||
const cantoNum = parseInt(canto);
|
||||
const intro = getIntro();
|
||||
const meta = await getBookMeta("sb");
|
||||
if (!meta) notFound();
|
||||
|
||||
const cantoInfo = intro.cantos.find((c) => c.number === cantoNum);
|
||||
const cantos = (meta as unknown as { cantos: CantoInfo[] }).cantos;
|
||||
const cantoInfo = cantos.find((c) => c.number === cantoNum);
|
||||
if (!cantoInfo) notFound();
|
||||
|
||||
const totalVerses = cantoInfo.chapters.reduce(
|
||||
|
||||
+12
-12
@@ -1,10 +1,10 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import Link from "next/link";
|
||||
import { PageHeader } from "@/components/page-header";
|
||||
import { Card, CardContent } from "@/components/ui/card";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { ExtractedVerseCount } from "../extracted-verse-count";
|
||||
import { getBookMeta } from "@/lib/api";
|
||||
import { notFound } from "next/navigation";
|
||||
|
||||
type ChapterInfo = {
|
||||
number: number;
|
||||
@@ -19,17 +19,17 @@ type CantoInfo = {
|
||||
chapters: ChapterInfo[];
|
||||
};
|
||||
|
||||
type Intro = {
|
||||
book: string;
|
||||
title: string;
|
||||
author: string;
|
||||
totalCantos: number;
|
||||
cantos: CantoInfo[];
|
||||
};
|
||||
export default async function SbPage() {
|
||||
const meta = await getBookMeta("sb");
|
||||
if (!meta) notFound();
|
||||
|
||||
export default function SbPage() {
|
||||
const introPath = path.join(process.cwd(), "app/lib/books/sb/intro.json");
|
||||
const intro: Intro = JSON.parse(fs.readFileSync(introPath, "utf-8"));
|
||||
const intro = meta as {
|
||||
book: string;
|
||||
title: string;
|
||||
author: string;
|
||||
totalCantos: number;
|
||||
cantos: CantoInfo[];
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-background">
|
||||
|
||||
Reference in New Issue
Block a user