mongo fix
This commit is contained in:
@@ -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>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user