import React, { useEffect, useState, useCallback } from 'react'; import { useParams, useNavigate } from 'react-router-dom'; import { useReader } from '../context/ReaderContext'; import { Toolbar } from '../components/Toolbar/Toolbar.jsx'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faArrowLeft, faList, faExpand, faCompress, faBookOpen, faScroll } from '@fortawesome/free-solid-svg-icons'; export function ReaderPage() { const { chapterId } = useParams(); const navigate = useNavigate(); const { context, currentPage, pages, loading, error, mode, loadChapterContext, loadPage, loadPages, setCurrentPage, setMode } = useReader(); const [currentPageData, setCurrentPageData] = useState(null); const [isFullscreen, setIsFullscreen] = useState(false); useEffect(() => { const initializeChapter = async () => { const contextLoaded = await loadChapterContext(chapterId); if (contextLoaded) { await loadPages(chapterId); } }; initializeChapter(); }, [chapterId, loadChapterContext, loadPages]); useEffect(() => { if (mode === 'classic' && currentPage > 0 && pages.length > 0) { loadPage(chapterId, currentPage).then(setCurrentPageData); } }, [chapterId, currentPage, loadPage, mode, pages.length]); const handleKeyDown = useCallback((e) => { if (mode === 'classic') { if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') { if (currentPage > 1) { setCurrentPage(currentPage - 1); } else if (context?.navigation.previous) { navigate(`/reader/${context.navigation.previous.id}`); } } else if (e.key === 'ArrowRight' || e.key === 'ArrowDown') { if (currentPage < pages.length) { setCurrentPage(currentPage + 1); } else if (context?.navigation.next) { navigate(`/reader/${context.navigation.next.id}`); } } } }, [currentPage, context, mode, navigate, pages.length, setCurrentPage]); useEffect(() => { window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, [handleKeyDown]); const toggleFullscreen = () => { if (!document.fullscreenElement) { document.documentElement.requestFullscreen(); setIsFullscreen(true); } else { document.exitFullscreen(); setIsFullscreen(false); } }; const handleImageClick = (e) => { if (mode !== 'classic') return; const rect = e.target.getBoundingClientRect(); const x = e.clientX - rect.left; const width = rect.width; if (x < width / 2) { // Clic sur la partie gauche if (currentPage > 1) { setCurrentPage(currentPage - 1); } else if (context?.navigation.previous) { navigate(`/reader/${context.navigation.previous.id}`); } } else { // Clic sur la partie droite if (currentPage < pages.length) { setCurrentPage(currentPage + 1); } else if (context?.navigation.next) { navigate(`/reader/${context.navigation.next.id}`); } } }; const toolbarConfig = { leftSection: [ { icon: faArrowLeft, navigateBack: true } ], rightSection: [ { icon: mode === 'classic' ? faScroll : faBookOpen, onClick: () => setMode(mode === 'classic' ? 'scrolling' : 'classic'), label: `Mode ${mode === 'classic' ? 'défilement' : 'page par page'}` }, { icon: isFullscreen ? faCompress : faExpand, onClick: toggleFullscreen, label: isFullscreen ? 'Quitter le plein écran' : 'Plein écran' }, { icon: faList, onClick: () => {}, label: 'Chapitres' } ] }; if (loading || (!currentPageData && mode === 'classic' && pages.length === 0)) { return (