104 lines
2.9 KiB
TypeScript
104 lines
2.9 KiB
TypeScript
import { redirect } from "next/navigation";
|
|
import { notFound } from "next/navigation";
|
|
import { getChapterVerseIndex } from "@/lib/api";
|
|
import { Card, CardContent } from "@/components/ui/card";
|
|
import { PageHeader } from "@/components/page-header";
|
|
import Link from "next/link";
|
|
import fs from "fs";
|
|
import path from "path";
|
|
|
|
const MM_JSON_DIR = path.join(process.cwd(), "lib", "jsons", "books", "mm");
|
|
|
|
type IntroSegment = {
|
|
slug: string;
|
|
title: string;
|
|
paragraphs: string[];
|
|
};
|
|
|
|
export default async function MmChapterPage({
|
|
params,
|
|
}: {
|
|
params: Promise<{ chapter: string }>;
|
|
}) {
|
|
const { chapter } = await params;
|
|
|
|
// Handle intro segments (intro-introduction)
|
|
if (chapter.startsWith("intro-")) {
|
|
const rawSlug = chapter.replace(/^intro-/, "");
|
|
const filePath = path.join(MM_JSON_DIR, "intro", `${rawSlug}.json`);
|
|
|
|
if (!fs.existsSync(filePath)) notFound();
|
|
|
|
let segment: IntroSegment;
|
|
try {
|
|
segment = JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
|
} catch {
|
|
notFound();
|
|
}
|
|
|
|
return (
|
|
<div className="min-h-screen bg-background">
|
|
<PageHeader sticky wide>
|
|
<Link
|
|
href="/books/mm"
|
|
className="text-sm text-muted-foreground hover:text-foreground transition-colors"
|
|
>
|
|
← Mukunda-mālā-stotra
|
|
</Link>
|
|
<h1 className="mt-3 text-2xl font-bold text-foreground">
|
|
{segment.title}
|
|
</h1>
|
|
</PageHeader>
|
|
|
|
<main className="max-w-3xl mx-auto px-6 py-8">
|
|
<Card>
|
|
<CardContent className="py-8 px-6 sm:px-8">
|
|
<div className="space-y-4">
|
|
{segment.paragraphs.map((paragraph: string, i: number) => (
|
|
<div
|
|
key={i}
|
|
className="text-base text-foreground leading-relaxed"
|
|
dangerouslySetInnerHTML={{ __html: paragraph }}
|
|
/>
|
|
))}
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
</main>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// Handle numeric chapters — redirect to first verse
|
|
const chapterNum = parseInt(chapter);
|
|
if (isNaN(chapterNum)) notFound();
|
|
|
|
// Try DB first
|
|
const verseIndex = await getChapterVerseIndex("mm", null, chapterNum);
|
|
|
|
// Fallback to local JSON
|
|
if (verseIndex.length === 0) {
|
|
const versesDir = path.join(
|
|
MM_JSON_DIR,
|
|
"chapters",
|
|
String(chapterNum).padStart(2, "0"),
|
|
"verses",
|
|
);
|
|
if (fs.existsSync(versesDir)) {
|
|
const files = fs
|
|
.readdirSync(versesDir)
|
|
.filter((f: string) => f.endsWith(".json"))
|
|
.sort();
|
|
if (files.length > 0) {
|
|
const first = JSON.parse(
|
|
fs.readFileSync(path.join(versesDir, files[0]), "utf-8"),
|
|
);
|
|
redirect(`/books/mm/${chapter}/${first.verse}`);
|
|
}
|
|
}
|
|
notFound();
|
|
}
|
|
|
|
redirect(`/books/mm/${chapter}/${verseIndex[0].verse}`);
|
|
}
|