289 lines
8.0 KiB
TypeScript
289 lines
8.0 KiB
TypeScript
export const dynamic = "force-dynamic";
|
|
|
|
import type { Metadata } from "next";
|
|
import Link from "next/link";
|
|
import { notFound } from "next/navigation";
|
|
import { ogForEssay, extractQuoteFromHl, buildOgMeta } from "@/lib/og";
|
|
import { PageHeader } from "@/components/page-header";
|
|
import { Card, CardContent } from "@/components/ui/card";
|
|
import { buttonVariants } from "@/components/ui/button";
|
|
import { ChevronLeft, ChevronRight } from "lucide-react";
|
|
import { cn } from "@/lib/utils";
|
|
import { MobileNav, type MobileNavGroup } from "@/components/mobile-nav";
|
|
import { bff, bffList } from "@/lib/bff";
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// HTML sanitisation (same pattern used across all readers)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function sanitizeHtml(html: string): string {
|
|
let clean = html.replace(/<script[\s>][\s\S]*?<\/script>/gi, "");
|
|
clean = clean.replace(/<style[\s>][\s\S]*?<\/style>/gi, "");
|
|
clean = clean.replace(/<iframe[\s>][\s\S]*?<\/iframe>/gi, "");
|
|
clean = clean.replace(
|
|
/<\/?(?:script|style|iframe|object|embed|form|input|textarea|select|button)[^>]*>/gi,
|
|
"",
|
|
);
|
|
clean = clean.replace(/\s+on\w+\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]+)/gi, "");
|
|
clean = clean.replace(/href\s*=\s*"javascript:[^"]*"/gi, 'href=""');
|
|
clean = clean.replace(/href\s*=\s*'javascript:[^']*'/gi, "href=''");
|
|
return clean;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Data fetching
|
|
// ---------------------------------------------------------------------------
|
|
|
|
type EssayData = {
|
|
slug: string;
|
|
title: string;
|
|
author: string;
|
|
year: number | null;
|
|
group: string | null;
|
|
paragraphs: string[];
|
|
};
|
|
|
|
type SidebarItem = {
|
|
slug: string;
|
|
title: string;
|
|
};
|
|
|
|
async function getEssay(slug: string): Promise<EssayData | null> {
|
|
const doc = await bff<{
|
|
slug: string;
|
|
title: string;
|
|
author: string;
|
|
year: number | null;
|
|
group: string | null;
|
|
paragraphs: string[];
|
|
}>(`/essays/${slug}`);
|
|
if (!doc) return null;
|
|
return {
|
|
slug: doc.slug,
|
|
title: doc.title,
|
|
author: doc.author,
|
|
year: doc.year,
|
|
group: doc.group,
|
|
paragraphs: (doc.paragraphs ?? []).map(sanitizeHtml),
|
|
};
|
|
}
|
|
|
|
async function getEssaySidebar(): Promise<SidebarItem[]> {
|
|
return bffList<SidebarItem>("/essays");
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Page
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export async function generateMetadata({
|
|
params,
|
|
searchParams,
|
|
}: {
|
|
params: Promise<{ slug: string }>;
|
|
searchParams: Promise<Record<string, string | string[] | undefined>>;
|
|
}): Promise<Metadata> {
|
|
const { slug } = await params;
|
|
const sp = await searchParams;
|
|
const essay = await getEssay(slug);
|
|
if (!essay) return {};
|
|
|
|
const hlQuote = extractQuoteFromHl(sp, essay.paragraphs);
|
|
if (hlQuote) {
|
|
return buildOgMeta({
|
|
title: essay.title,
|
|
detail: "Essay by Śrīla Prabhupāda",
|
|
quote: hlQuote,
|
|
});
|
|
}
|
|
|
|
return ogForEssay(essay.title);
|
|
}
|
|
|
|
export default async function EssayPage({
|
|
params,
|
|
}: {
|
|
params: Promise<{ slug: string }>;
|
|
}) {
|
|
const { slug } = await params;
|
|
|
|
const essay = await getEssay(slug);
|
|
if (!essay) notFound();
|
|
|
|
const allEssays = await getEssaySidebar();
|
|
const currentIdx = allEssays.findIndex((item) => item.slug === slug);
|
|
|
|
const prev = currentIdx > 0 ? allEssays[currentIdx - 1] : null;
|
|
const next =
|
|
currentIdx < allEssays.length - 1 ? allEssays[currentIdx + 1] : null;
|
|
|
|
return (
|
|
<div className="min-h-screen bg-background">
|
|
<PageHeader sticky>
|
|
<Link
|
|
href="/essays"
|
|
className="text-sm text-muted-foreground hover:text-foreground transition-colors"
|
|
>
|
|
← Essays
|
|
</Link>
|
|
<h1 className="mt-3 text-2xl font-bold text-foreground">
|
|
{essay.title}
|
|
</h1>
|
|
</PageHeader>
|
|
|
|
<div className="max-w-5xl mx-auto flex">
|
|
{/* Left sidebar */}
|
|
<EssaySidebar items={allEssays} currentSlug={slug} />
|
|
|
|
{/* Main content */}
|
|
<main className="flex-1 min-w-0 px-6 py-8 space-y-8">
|
|
{/* Top navigation */}
|
|
<EssayNav
|
|
prev={prev}
|
|
next={next}
|
|
allEssays={allEssays}
|
|
currentSlug={slug}
|
|
/>
|
|
|
|
{/* Content */}
|
|
<Card>
|
|
<CardContent className="py-8 px-6 sm:px-8">
|
|
<div className="space-y-4">
|
|
{essay.paragraphs.map((paragraph, i) => (
|
|
<div
|
|
key={i}
|
|
className="text-base text-foreground leading-relaxed"
|
|
dangerouslySetInnerHTML={{ __html: paragraph }}
|
|
/>
|
|
))}
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
|
|
{/* Bottom navigation */}
|
|
<EssayNav
|
|
prev={prev}
|
|
next={next}
|
|
allEssays={allEssays}
|
|
currentSlug={slug}
|
|
/>
|
|
</main>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Sidebar
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function EssaySidebar({
|
|
items,
|
|
currentSlug,
|
|
}: {
|
|
items: SidebarItem[];
|
|
currentSlug: string;
|
|
}) {
|
|
return (
|
|
<aside className="hidden lg:block w-56 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">
|
|
Essays
|
|
</h2>
|
|
<nav className="flex flex-col gap-0.5">
|
|
{items.map((item) => (
|
|
<Link
|
|
key={item.slug}
|
|
href={`/essays/${item.slug}`}
|
|
className={cn(
|
|
"px-2 py-1.5 rounded-md text-sm transition-colors truncate",
|
|
item.slug === currentSlug
|
|
? "bg-primary/10 text-primary font-medium"
|
|
: "text-muted-foreground hover:bg-muted hover:text-foreground",
|
|
)}
|
|
title={item.title}
|
|
>
|
|
{item.title}
|
|
</Link>
|
|
))}
|
|
</nav>
|
|
</aside>
|
|
);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Prev / Next navigation
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function EssayNav({
|
|
prev,
|
|
next,
|
|
allEssays,
|
|
currentSlug,
|
|
}: {
|
|
prev: SidebarItem | null;
|
|
next: SidebarItem | null;
|
|
allEssays: SidebarItem[];
|
|
currentSlug: string;
|
|
}) {
|
|
const mobileNavGroups: MobileNavGroup[] = [];
|
|
|
|
if (allEssays.length > 0) {
|
|
mobileNavGroups.push({
|
|
title: "Essays",
|
|
items: allEssays.map((i) => ({
|
|
slug: i.slug,
|
|
label: i.title,
|
|
href: `/essays/${i.slug}`,
|
|
type: "chapter",
|
|
})),
|
|
});
|
|
}
|
|
|
|
return (
|
|
<div className="flex items-center justify-between gap-3">
|
|
<div className="flex items-center gap-2">
|
|
<MobileNav
|
|
groups={mobileNavGroups}
|
|
currentSlug={currentSlug}
|
|
bookTitle="Essays"
|
|
/>
|
|
{prev ? (
|
|
<Link
|
|
href={`/essays/${prev.slug}`}
|
|
className={cn(
|
|
buttonVariants({ variant: "outline", size: "sm" }),
|
|
"gap-1.5",
|
|
)}
|
|
>
|
|
<ChevronLeft className="size-4" />
|
|
<span className="hidden sm:inline truncate max-w-[180px]">
|
|
{prev.title}
|
|
</span>
|
|
<span className="sm:hidden">Previous</span>
|
|
</Link>
|
|
) : (
|
|
<div className="hidden lg:block" />
|
|
)}
|
|
</div>
|
|
|
|
{next ? (
|
|
<Link
|
|
href={`/essays/${next.slug}`}
|
|
className={cn(
|
|
buttonVariants({ variant: "outline", size: "sm" }),
|
|
"gap-1.5",
|
|
)}
|
|
>
|
|
<span className="hidden sm:inline truncate max-w-[180px]">
|
|
{next.title}
|
|
</span>
|
|
<span className="sm:hidden">Next</span>
|
|
<ChevronRight className="size-4" />
|
|
</Link>
|
|
) : (
|
|
<div />
|
|
)}
|
|
</div>
|
|
);
|
|
}
|