"use client"; import { useEffect, useState } from "react"; import Link from "next/link"; type ChapterLite = { number: number; title: string; }; type Props = { mangaSlug: string; chapters: ChapterLite[]; }; function storageKey(slug: string) { return `sunnymh:last-read:${slug}`; } export function readLastReadChapter(slug: string): number | null { if (typeof window === "undefined") return null; const raw = window.localStorage.getItem(storageKey(slug)); if (!raw) return null; const n = Number(raw); return Number.isFinite(n) && n > 0 ? n : null; } export function writeLastReadChapter(slug: string, chapter: number) { if (typeof window === "undefined") return; window.localStorage.setItem(storageKey(slug), String(chapter)); } export function ReadingProgressButton({ mangaSlug, chapters }: Props) { const [lastRead, setLastRead] = useState(null); useEffect(() => { setLastRead(readLastReadChapter(mangaSlug)); }, [mangaSlug]); if (chapters.length === 0) return null; const first = chapters[0]; const resumeChapter = lastRead !== null ? chapters.find((c) => c.number === lastRead) : null; const target = resumeChapter ?? first; return ( {resumeChapter ? ( <> 继续阅读 · #{resumeChapter.number} {resumeChapter.title} ) : ( "开始阅读" )} ); }