import { useState } from 'react'; import { useParams, useNavigate } from 'react-router-dom'; import { useFeedArticles } from '@/hooks/useFeedArticles'; import NewsCard from '@/components/NewsCard'; import ArticleModal from '@/components/ArticleModal'; import { NewsItem } from '@/types/news'; import { Button } from '@/components/ui/button'; import { ArrowLeft, Loader2 } from 'lucide-react'; import { Pagination, PaginationContent, PaginationEllipsis, PaginationItem, PaginationLink, PaginationNext, PaginationPrevious, } from '@/components/ui/pagination'; const FeedDetail = () => { const { feedId } = useParams<{ feedId: string }>(); const navigate = useNavigate(); const [currentPage, setCurrentPage] = useState(1); const [selectedArticle, setSelectedArticle] = useState(null); const { articles, feedInfo, loading, totalCount, totalPages, togglePin, markAsRead, deleteArticle } = useFeedArticles(feedId || '', currentPage); const handleOpenArticle = (article: NewsItem) => { setSelectedArticle(article); }; const handleCloseArticleModal = () => { setSelectedArticle(null); }; const handlePageChange = (page: number) => { setCurrentPage(page); window.scrollTo({ top: 0, behavior: 'smooth' }); }; const renderPaginationItems = () => { const items = []; const maxVisible = 5; if (totalPages <= maxVisible) { for (let i = 1; i <= totalPages; i++) { items.push( handlePageChange(i)} isActive={currentPage === i} > {i} ); } } else { // Always show first page items.push( handlePageChange(1)} isActive={currentPage === 1} > 1 ); // Show ellipsis or pages around current if (currentPage > 3) { items.push(); } const start = Math.max(2, currentPage - 1); const end = Math.min(totalPages - 1, currentPage + 1); for (let i = start; i <= end; i++) { items.push( handlePageChange(i)} isActive={currentPage === i} > {i} ); } if (currentPage < totalPages - 2) { items.push(); } // Always show last page items.push( handlePageChange(totalPages)} isActive={currentPage === totalPages} > {totalPages} ); } return items; }; if (loading) { return (
); } if (!feedInfo) { return (

Flux introuvable

); } return (
{/* Header with back button and feed info */}

{feedInfo.name}

{feedInfo.description && (

{feedInfo.description}

)}

{totalCount} article{totalCount > 1 ? 's' : ''} au total

{/* Articles grid */} {articles.length === 0 ? (

Aucun article disponible pour ce flux

) : ( <>
{articles.map((article) => ( ))}
{/* Pagination */} {totalPages > 1 && (
currentPage > 1 && handlePageChange(currentPage - 1)} className={currentPage === 1 ? 'pointer-events-none opacity-50' : 'cursor-pointer'} /> {renderPaginationItems()} currentPage < totalPages && handlePageChange(currentPage + 1)} className={currentPage === totalPages ? 'pointer-events-none opacity-50' : 'cursor-pointer'} />
)} )}
{/* Article Modal */}
); }; export default FeedDetail;