import { use, useState } from 'react'; import { useEffect } from 'react'; import { render } from 'react-dom'; import Masonry from 'react-masonry-css'; interface GalleryProps { id: string; closeMenu: () => void; } const Gallery = ({ id, closeMenu }: GalleryProps) => { const [isSingle, setIsSingle] = useState(false); const [loaded, setLoaded] = useState({}) const [selectedImage, setSelectedImage] = useState(null); const [images, setImages] = useState([]); const [galleryId, setGalleryId] = useState(id as string); const [currentIndex, setCurrentIndex] = useState(0); const goToNext = () => { setCurrentIndex(prevIndex => (prevIndex + 1) % images.length); } const goToPrevious = () => { setCurrentIndex(prevIndex => (prevIndex - 1 + images.length) % images.length); } console.log(id) const getData = async () => { const thumbnailResponse = await fetch('/api/galleries/'+String(galleryId)+'/images'); const thumbnailUrl = await thumbnailResponse.json() as string[]; setImages(thumbnailUrl); } const generateRandomString = function (length:number) { let result = ''; let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; let charactersLength = characters.length; for ( let i = 0; i < length; i++ ) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; } const next = () => { if (currentIndex < images.length - 1) { setCurrentIndex(currentIndex + 1); } else { setCurrentIndex(0); } } const previous = () => { if (currentIndex > 0) { setCurrentIndex(currentIndex - 1); } else { setCurrentIndex(images.length - 1); } } const renderButtons = () => { return ( <>
); }; const handleDownload = (image: string) => { const link = document.createElement('a'); link.href = image; link.download = 'image.jpg'; // or any other filename link.style.display = 'none'; document.body.appendChild(link); link.click(); document.body.removeChild(link); }; useEffect(() => { getData(); const handleKeyDown = (event: KeyboardEvent) => { switch (event.key) { case 'ArrowLeft': case 'a': case 'A': previous(); break; case 'ArrowRight': case 'd': case 'D': next(); break; default: break; } }; setSelectedImage(images[currentIndex]); window.addEventListener('keydown', handleKeyDown); // Clean up the event listener when the component is unmounted return () => { window.removeEventListener('keydown', handleKeyDown); }; }, [currentIndex]); const handleClick = (image: string) => { setSelectedImage(image); setCurrentIndex(images.indexOf(image)); }; const open = () => { if(selectedImage === null) return; console.log(selectedImage) let base64Image = selectedImage.split(';base64,').pop(); if(!base64Image) return; let blob = new Blob([Uint8Array.from(atob(base64Image), c => c.charCodeAt(0))], {type: 'image/jpeg'}); // adjust the type as needed let url = URL.createObjectURL(blob); window.open(url, '_blank'); } const close = () => { if(selectedImage != null){ setSelectedImage(null); } else{ closeMenu(); } } const breakpointColumnsObj = { default: 3 }; return (
<> {renderButtons()} {selectedImage ? ( setSelectedImage(null)} /> ) : ( {images .filter((img) => img !== selectedImage) .map((image, index) => ( handleClick(image)} className={`animate-in animate-once animate-duration-1000 animate-ease-out animate-reverse hover:scale-105 p-2 cursor-pointer my-2 transition-all opacity-100 duration-500 ease-in-out transform`} /> ))} )}
); } export default Gallery;