function FancyLightbox({ images, activeIndex, onClose, ratingInfo, refreshRatings }) { const [index, setIndex] = React.useState(activeIndex); const [isClosing, setIsClosing] = React.useState(false); const [touchStartX, setTouchStartX] = React.useState(null); const [isPortrait, setIsPortrait] = React.useState(false); // Sync internal index when activeIndex changes React.useEffect(() => { setIndex(activeIndex); }, [activeIndex]); const image = images[index]; const handleClose = () => { setIsClosing(true); setTimeout(() => { onClose(); setIsClosing(false); }, 300); }; const handlePrev = () => setIndex(i => (i - 1 + images.length) % images.length); const handleNext = () => setIndex(i => (i + 1) % images.length); const handleTouchStart = e => setTouchStartX(e.changedTouches[0].clientX); const handleTouchEnd = e => { if (touchStartX === null) return; const endX = e.changedTouches[0].clientX; const diffX = touchStartX - endX; if (Math.abs(diffX) > 50) { diffX > 0 ? handleNext() : handlePrev(); } setTouchStartX(null); }; React.useEffect(() => { const handleKey = e => { if (e.key === 'Escape') handleClose(); if (e.key === 'ArrowRight') handleNext(); if (e.key === 'ArrowLeft') handlePrev(); }; document.addEventListener('keydown', handleKey); return () => document.removeEventListener('keydown', handleKey); }, [images.length]); React.useEffect(() => { const img = new Image(); img.src = image.src; img.onload = () => setIsPortrait(img.height > img.width); }, [image]); return (
{image.description}
Uploaded by: {image.uploader}