import React, { useState, useEffect } from 'react'; import { BookOpen, Star } from 'lucide-react'; interface BookSelectorProps { books: string[]; onBookSelect: (book: string) => void; formatBookName: (bookName: string) => string; user?: any; } const BookSelector: React.FC = ({ books, onBookSelect, formatBookName, user }) => { const [favorites, setFavorites] = useState>(new Set()); const [loading, setLoading] = useState(false); // Load favorites when user is available useEffect(() => { if (user) { loadFavorites(); } }, [user]); const loadFavorites = async () => { if (!user) return; setLoading(true); try { const response = await fetch('/api/favorites', { credentials: 'include' }); if (response.ok) { const data = await response.json(); const favoriteBooks: string[] = data.favorites .filter((fav: any) => !fav.chapter) // Only book-level favorites .map((fav: any) => fav.book); const bookFavorites = new Set(favoriteBooks); setFavorites(bookFavorites); } } catch (error) { console.error('Failed to load favorites:', error); } finally { setLoading(false); } }; const toggleFavorite = async (book: string, event: React.MouseEvent) => { event.stopPropagation(); // Prevent book selection when clicking star if (!user) return; const isFavorited = favorites.has(book); try { if (isFavorited) { // Remove favorite const response = await fetch('/api/favorites', { credentials: 'include' }); if (response.ok) { const data = await response.json(); const bookFavorite = data.favorites.find((fav: any) => fav.book === book && !fav.chapter); if (bookFavorite) { await fetch(`/api/favorites/${bookFavorite.id}`, { method: 'DELETE', credentials: 'include' }); setFavorites(prev => { const newFavorites = new Set(prev); newFavorites.delete(book); return newFavorites; }); } } } else { // Add favorite const response = await fetch('/api/favorites', { method: 'POST', headers: { 'Content-Type': 'application/json', }, credentials: 'include', body: JSON.stringify({ book: book }) }); if (response.ok) { setFavorites(prev => new Set(prev).add(book)); } } } catch (error) { console.error('Failed to toggle favorite:', error); } }; // Group books by testament const oldTestament = books.slice(0, 39); // First 39 books const newTestament = books.slice(39); // Remaining books const BookGroup: React.FC<{ title: string; books: string[] }> = ({ title, books }) => (

{title}

{books.map((book) => (
{/* Star button - only show for authenticated users */} {user && ( )}
))}
); return (

ESV Bible

Select a book to begin reading

{user && (

Click the ★ to add books to your favorites

)}
); }; export default BookSelector;