'use client'; import { useState, useEffect, useRef } from 'react'; import './HeroSection.css'; import apiClient from '@/api/apiClient'; import SkeletonLoader from '@/skeletons/HeroSection'; const HeroSection = () => { const [currentIndex, setCurrentIndex] = useState(0); const intervalRef = useRef(null); const [fadeOut, setFadeOut] = useState(false); const [items, setItems] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { const fetchRecentItems = async () => { try { const response = await apiClient.getRecent(); const movies = response.movies.map(film => ({ title: film[0], description: film[2], imageUrl: film[3], type: 'Film' })); const series = response.series.map(serie => ({ title: serie[0], description: serie[2], imageUrl: serie[3], type: 'Series' })); setItems([...movies, ...series]); } catch (error) { console.error('Error fetching recent items:', error); // Handle error (e.g., set a default list or show an error message) } finally { setLoading(false); } }; fetchRecentItems(); }, []); const startAutoSwitch = () => { if (intervalRef.current) clearInterval(intervalRef.current); intervalRef.current = setInterval(() => { setFadeOut(true); setTimeout(() => { setCurrentIndex(prevIndex => (prevIndex + 1) % items.length); setFadeOut(false); }, 200); }, 5000); }; useEffect(() => { if (items.length > 0) { startAutoSwitch(); } return () => { clearInterval(intervalRef.current); }; }, [items]); const handleIndicatorClick = (index) => { setFadeOut(true); setTimeout(() => { setCurrentIndex(index); setFadeOut(false); }, 100); startAutoSwitch(); }; if (loading) { return ; } const { title, description, imageUrl } = items[currentIndex]; return (
{items.map((item, index) => (
))}

{title}

{description}

{items.map((_, index) => (
handleIndicatorClick(index)} >
))}
); }; export default HeroSection;