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[]; }; type ChapterInfo = { number: number; title: string; totalVerses: number; }; type LilaInfo = { key: string; name: string; totalChapters: number; totalVerses: number; 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 { lilaInfo, chapterInfo } = info; const verses = getChapterVerses(lila, chapter); if (verses.length === 0) notFound(); const verseList = verses.map((v) => ({ verse: v.verse, verse_end: v.verse_end ?? undefined, reference: v.reference, })); return (
← {lilaInfo.name}

Chapter {chapter}: {chapterInfo.title}

{/* Left sidebar — verse index */} {/* Main content */}
{verses.map((verse) => ( {/* Header */} {verse.reference} {/* Bengali */} {verse.bengali && (

{verse.bengali}

)} {/* Devanagari */}

{verse.devanagari}

{/* Transliteration */}

{verse.transliteration}

{/* Synonyms */} {verse.synonyms.length > 0 && (

Synonyms

{verse.synonyms.map((s, i) => ( {s.word} —{s.meanings.join(", ")} {i < verse.synonyms.length - 1 ? "; " : "."} ))}

)} {/* Translation */}

Translation

{/* Purport */} {verse.purport.length > 0 && (

Purport

{verse.purport.map((p, i) => (

))}

)}
))}
); }