import React, { useState, useEffect } from 'react'; import { ArrowLeft, FileText, Star, ChevronRight, Search } from 'lucide-react'; import { getBook } from '../services/api'; interface ChapterSelectorProps { book: string; onChapterSelect: (chapter: string) => void; onBack: () => void; formatBookName: (bookName: string) => string; user?: any; onFavoriteChange?: () => void; version?: string; onSearchClick?: () => void; } const ChapterSelector: React.FC = ({ book, onChapterSelect, onBack, formatBookName, user, onFavoriteChange, version = 'esv', onSearchClick }) => { const [chapters, setChapters] = useState([]); const [loading, setLoading] = useState(true); const [favorites, setFavorites] = useState>(new Set()); useEffect(() => { loadChapters(); }, [book]); // Load favorites when user is available useEffect(() => { if (user) { loadFavorites(); } }, [user, book, version]); const loadFavorites = async () => { if (!user) return; try { const response = await fetch('/api/favorites', { credentials: 'include' }); if (response.ok) { const data = await response.json(); const favoriteChapters: string[] = data.favorites .filter((fav: any) => fav.book === book && fav.chapter && fav.version === version && !fav.verse_start) // Only chapter-level favorites for this book and version .map((fav: any) => fav.chapter); const chapterFavorites = new Set(favoriteChapters); setFavorites(chapterFavorites); console.log('Loaded chapter favorites for version:', version, favoriteChapters); } } catch (error) { console.error('Failed to load favorites:', error); } }; const toggleFavorite = async (chapter: string, event: React.MouseEvent) => { event.stopPropagation(); // Prevent chapter selection when clicking star if (!user) return; const isFavorited = favorites.has(chapter); try { if (isFavorited) { // Remove favorite const response = await fetch('/api/favorites', { credentials: 'include' }); if (response.ok) { const data = await response.json(); const chapterFavorite = data.favorites.find((fav: any) => fav.book === book && fav.chapter === chapter && !fav.verse_start ); if (chapterFavorite) { await fetch(`/api/favorites/${chapterFavorite.id}`, { method: 'DELETE', credentials: 'include' }); setFavorites(prev => { const newFavorites = new Set(prev); newFavorites.delete(chapter); return newFavorites; }); onFavoriteChange?.(); // Notify parent to refresh favorites menu } } } else { // Add favorite const response = await fetch('/api/favorites', { method: 'POST', headers: { 'Content-Type': 'application/json', }, credentials: 'include', body: JSON.stringify({ book: book, chapter: chapter, version: version }) }); if (response.ok) { setFavorites(prev => new Set(prev).add(chapter)); onFavoriteChange?.(); // Notify parent to refresh favorites menu } } } catch (error) { console.error('Failed to toggle favorite:', error); } }; const loadChapters = async () => { try { setLoading(true); const response = await getBook(book, version); // The API now returns { chapters: ["1", "2", "3", ...] } if (response.chapters) { // Sort chapters numerically to ensure proper order const sortedChapters = response.chapters.sort((a, b) => parseInt(a, 10) - parseInt(b, 10)); setChapters(sortedChapters); } else { console.error('API returned no chapters data'); setChapters([]); } } catch (error) { console.error('Failed to load chapters:', error); // Don't show fallback chapters - just show an empty list setChapters([]); } finally { setLoading(false); } }; if (loading) { return (

Loading chapters...

); } return (
{/* Search Bar */}
{/* Breadcrumb Navigation */}
{formatBookName(book)}
{/* Book Title */}

{formatBookName(book)}

Select a chapter to read

{user && (

Click the ★ to add chapters to your favorites

)}
{/* Chapters Grid */}
{chapters.map((chapter) => (
{/* Star button - only show for authenticated users */} {user && ( )}
))}
{/* Chapter Count */}

{chapters.length} chapters available

); }; export default ChapterSelector;