git-subtree-dir: manga-site git-subtree-split: f2ef775f7095dc2b107b576cd4053593e89dd887
70 lines
1.9 KiB
TypeScript
70 lines
1.9 KiB
TypeScript
import { notFound } from "next/navigation";
|
|
import { prisma } from "@/lib/db";
|
|
import { PageReader } from "@/components/PageReader";
|
|
import type { Metadata } from "next";
|
|
|
|
type Props = {
|
|
params: Promise<{ slug: string; chapter: string }>;
|
|
};
|
|
|
|
export async function generateMetadata({ params }: Props): Promise<Metadata> {
|
|
const { slug, chapter } = await params;
|
|
const chapterNum = parseInt(chapter, 10);
|
|
if (isNaN(chapterNum)) return { title: "Not Found" };
|
|
|
|
const manga = await prisma.manga.findUnique({ where: { slug } });
|
|
if (!manga) return { title: "Not Found" };
|
|
|
|
return {
|
|
title: `${manga.title} — Ch. ${chapterNum}`,
|
|
description: `Read chapter ${chapterNum} of ${manga.title}`,
|
|
};
|
|
}
|
|
|
|
export default async function ChapterReaderPage({ params }: Props) {
|
|
const { slug, chapter } = await params;
|
|
const chapterNum = parseInt(chapter, 10);
|
|
if (isNaN(chapterNum)) notFound();
|
|
|
|
const [manga, initialChapterMeta] = await Promise.all([
|
|
prisma.manga.findUnique({
|
|
where: { slug },
|
|
include: {
|
|
chapters: {
|
|
orderBy: { number: "asc" },
|
|
include: {
|
|
_count: { select: { pages: true } },
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
prisma.page.findMany({
|
|
where: { chapter: { number: chapterNum, manga: { slug } } },
|
|
orderBy: { number: "asc" },
|
|
select: { number: true, width: true, height: true },
|
|
}),
|
|
]);
|
|
|
|
if (!manga) notFound();
|
|
|
|
const currentChapter = manga.chapters.find((c) => c.number === chapterNum);
|
|
if (!currentChapter) notFound();
|
|
|
|
const allChapters = manga.chapters.map((c) => ({
|
|
id: c.id,
|
|
number: c.number,
|
|
title: c.title,
|
|
totalPages: c._count.pages,
|
|
}));
|
|
|
|
return (
|
|
<PageReader
|
|
mangaSlug={manga.slug}
|
|
mangaTitle={manga.title}
|
|
startChapterNumber={currentChapter.number}
|
|
chapters={allChapters}
|
|
initialChapterMeta={initialChapterMeta}
|
|
/>
|
|
);
|
|
}
|