import React, { useEffect, useState } from 'react' import { useParams, useNavigate } from 'react-router-dom' import { ArrowLeft, ChevronLeft, ChevronRight } from 'lucide-react' import axios from 'axios' import { useStoryStore } from '../../store/storyStore' import { Chapter } from '../../types' import { Button } from '../../components/ui/Button' const API = 'https://localhost:7125/api' function mapChapter(c: any): Chapter { return { chapter_id: c.id ?? c.chapter_id, story_id: c.storyId ?? c.story_id, title: c.title ?? c.name ?? '', content: c.content ?? '', chapter_number: c.number ?? c.chapter_number ?? 0, word_count: c.wordCount ?? c.word_count ?? 0, view_count: c.viewCount ?? c.view_count ?? 0, is_published: true, created_at: c.createdAt ?? c.created_at ?? '', updated_at: c.updatedAt ?? c.updated_at ?? '', } } export const ChapterReadPage: React.FC = () => { const { storyId, chapterId } = useParams<{ storyId: string; chapterId: string }>() const navigate = useNavigate() const { stories, chapters, incrementViewCount } = useStoryStore() const [scrollProgress, setScrollProgress] = useState(0) const [viewed, setViewed] = useState(false) const [chapter, setChapter] = useState(null) const [loading, setLoading] = useState(true) useEffect(() => { setLoading(true) setViewed(false) setScrollProgress(0) axios.get(`${API}/chapters/${chapterId}`) .then(res => { const data = res.data?.chapter ?? res.data setChapter(mapChapter(data)) }) .catch(() => { const fallback = chapters.find(c => c.chapter_id === Number(chapterId)) if (fallback) setChapter(fallback) }) .finally(() => setLoading(false)) }, [chapterId]) const story = stories.find(s => s.story_id === Number(storyId)) const storyChapters = chapters .filter(c => c.story_id === Number(storyId) && c.is_published) .sort((a, b) => a.chapter_number - b.chapter_number) const currentIndex = storyChapters.findIndex(c => c.chapter_id === Number(chapterId)) const prevChapter = currentIndex > 0 ? storyChapters[currentIndex - 1] : null const nextChapter = currentIndex < storyChapters.length - 1 ? storyChapters[currentIndex + 1] : null // Track scroll progress useEffect(() => { const handler = () => { const el = document.documentElement const progress = (el.scrollTop / (el.scrollHeight - el.clientHeight)) * 100 setScrollProgress(Math.min(100, progress)) // Increment view count when 30% read if (progress > 30 && !viewed) { setViewed(true) if (chapter) incrementViewCount(chapter.chapter_id) } } window.addEventListener('scroll', handler) return () => window.removeEventListener('scroll', handler) }, [chapter, viewed, incrementViewCount]) if (loading) { return (

Loading chapter...

) } if (!story || !chapter) { return (

Chapter not found

) } return (
{/* Progress bar */}
{/* Top nav */}

{chapter.title}

Chapter {chapter.chapter_number}

{/* Content */}
{/* Chapter header */}

Chapter {chapter.chapter_number}

{chapter.title}

{/* Chapter content */}
{chapter.content.split('\n\n').map((para, i) => (

{para}

))}
{/* Chapter navigation */}
{prevChapter ? ( ) : (
)} {nextChapter ? ( ) : (

End of story

)}
) }