import React, { useState, useEffect } from 'react'; import { ArrowLeft, FileText } from 'lucide-react'; import { getBook } from '../services/api'; interface ChapterSelectorProps { book: string; onChapterSelect: (chapter: string) => void; onBack: () => void; formatBookName: (bookName: string) => string; } const ChapterSelector: React.FC = ({ book, onChapterSelect, onBack, formatBookName }) => { const [chapters, setChapters] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { loadChapters(); }, [book]); const loadChapters = async () => { try { setLoading(true); const bookContent = await getBook(book); // Parse markdown to extract chapter numbers const lines = bookContent.split('\n'); const chapterNumbers: string[] = []; for (const line of lines) { // Look for chapter headers like "# 1" or "# 1\n" const chapterMatch = line.match(/^#\s+(\d+)/); if (chapterMatch) { chapterNumbers.push(chapterMatch[1]); } } setChapters(chapterNumbers); } catch (error) { console.error('Failed to load chapters:', error); // Fallback: generate chapter numbers 1-50 (most books have fewer than 50 chapters) const fallbackChapters = Array.from({ length: 50 }, (_, i) => (i + 1).toString()); setChapters(fallbackChapters); } finally { setLoading(false); } }; if (loading) { return (

Loading chapters...

); } return (
{/* Header */}
{/* Book Title */}

{formatBookName(book)}

Select a chapter to read

{/* Chapters Grid */}
{chapters.map((chapter) => ( ))}
{/* Chapter Count */}

{chapters.length} chapters available

); }; export default ChapterSelector;