'use client'; import Link from 'next/link'; import { useEffect, useState } from 'react'; import { createPortal } from 'react-dom'; import type { Book, LoadedChapter } from '@/lib/book-types'; import { calcBookProgress, getReadingProgress } from '@/lib/reading-progress'; import { BOOKMARKS_EVENT, getBookmarks, type Bookmark } from '@/lib/bookmarks'; import { getHighlights, HIGHLIGHTS_EVENT, removeHighlight, type Highlight, } from '@/lib/highlights'; import { extractHeadings } from '@/lib/headings'; import { useT } from '@/components/LocaleProvider'; function scrollToId(id: string) { document.getElementById(id)?.scrollIntoView({ behavior: 'smooth', block: 'start' }); } function scrollToChapterSlug(slug: string) { scrollToId(slug); history.replaceState(null, '', `/read#${slug}`); } function ChapterNavList({ book, chapters, activeSlug, filter, onNavigate, }: { book: Book; chapters: LoadedChapter[]; activeSlug: string; filter: string; onNavigate?: () => void; }) { const t = useT(); const [openSlugs, setOpenSlugs] = useState>(() => new Set()); function toggle(slug: string) { setOpenSlugs((prev) => { const next = new Set(prev); if (next.has(slug)) next.delete(slug); else next.add(slug); return next; }); } return ( <> {book.chapters .map((chapter, i) => ({ chapter, i })) .filter( ({ chapter }) => !filter.trim() || chapter.title.toLowerCase().includes(filter.trim().toLowerCase()) ) .map(({ chapter, i }) => { const active = activeSlug === chapter.slug; const loaded = chapters.find((c) => c.slug === chapter.slug); const headings = loaded ? extractHeadings(loaded.content).filter((h) => h.level === 2) : []; const expanded = openSlugs.has(chapter.slug); return (
{headings.length > 0 ? ( ) : ( )} { e.preventDefault(); scrollToChapterSlug(chapter.slug); onNavigate?.(); }} className={`chapter-sidebar-item ${active ? 'is-active' : ''}`} style={{ flex: 1, minWidth: 0, marginBottom: 0, padding: '8px 8px 8px 6px' }} > {String(i + 1).padStart(2, '0')} {chapter.emoji} {chapter.title}
{expanded && headings.length > 0 && ( )}
); })} ); } function SidebarContent({ book, chapters, activeSlug, readChapters, remainingMins, bookmarks, highlights, filter, setFilter, onNavigate, }: { book: Book; chapters: LoadedChapter[]; activeSlug: string; readChapters: string[]; remainingMins: number; bookmarks: Bookmark[]; highlights: Highlight[]; filter: string; setFilter: (v: string) => void; onNavigate?: () => void; }) { const t = useT(); const percent = calcBookProgress(readChapters.length, book.chapters.length); return ( <>

《{book.title}》

{t('reader.toc')}

{t('reader.readProgress', { read: readChapters.length, total: book.chapters.length, mins: remainingMins, })}

setFilter(e.target.value)} placeholder={t('reader.filterChapters')} className="chapter-sidebar-search" />
{bookmarks.length > 0 && (

{t('reader.bookmarks')}

{bookmarks.map((bm) => ( ))}
)} {highlights.length > 0 && (

{t('reader.highlights')}

{highlights.map((hl) => (
))}
)}
{t('reader.backCover')}
); } export function ChapterSidebarToggle({ onOpen }: { onOpen: () => void }) { const t = useT(); return ( ); } export function ChapterSidebar({ book, chapters, activeSlug, remainingMins, mobileOpen, onMobileClose, }: { book: Book; chapters: LoadedChapter[]; activeSlug: string; remainingMins: number; mobileOpen: boolean; onMobileClose: () => void; }) { const t = useT(); const [readChapters, setReadChapters] = useState([]); const [bookmarks, setBookmarks] = useState([]); const [highlights, setHighlights] = useState([]); const [filter, setFilter] = useState(''); const [mounted, setMounted] = useState(false); useEffect(() => setMounted(true), []); useEffect(() => { const saved = getReadingProgress(); setReadChapters(saved?.readChapters ?? []); }, [activeSlug]); useEffect(() => { const refresh = () => setBookmarks(getBookmarks()); refresh(); window.addEventListener(BOOKMARKS_EVENT, refresh); return () => window.removeEventListener(BOOKMARKS_EVENT, refresh); }, []); useEffect(() => { const refresh = () => setHighlights(getHighlights()); refresh(); window.addEventListener(HIGHLIGHTS_EVENT, refresh); return () => window.removeEventListener(HIGHLIGHTS_EVENT, refresh); }, []); useEffect(() => { if (!mobileOpen) return; document.documentElement.classList.add('drawer-lock'); document.body.classList.add('drawer-lock'); return () => { document.documentElement.classList.remove('drawer-lock'); document.body.classList.remove('drawer-lock'); }; }, [mobileOpen]); useEffect(() => { if (!mobileOpen) return; const onKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') onMobileClose(); }; window.addEventListener('keydown', onKeyDown); return () => window.removeEventListener('keydown', onKeyDown); }, [mobileOpen, onMobileClose]); const contentProps = { book, chapters, activeSlug, readChapters, remainingMins, bookmarks, highlights, filter, setFilter, }; const mobileDrawer = mounted && createPortal(
, document.body ); return ( <> {mobileDrawer} ); }