neroshitron/components/ui/gallery.tsx

228 lines
9.5 KiB
TypeScript
Raw Permalink Normal View History

2024-05-26 21:57:10 -04:00
import { use, useState, useRef } from 'react';
2024-05-26 02:14:59 -04:00
import { useEffect } from 'react';
2024-05-26 14:09:30 -04:00
import { render } from 'react-dom';
2024-05-26 02:14:59 -04:00
import Masonry from 'react-masonry-css';
2024-05-26 21:57:10 -04:00
import PanZoom, { PanZoomRef } from 'react-easy-panzoom';
2024-05-26 02:14:59 -04:00
interface GalleryProps {
2024-05-26 13:08:59 -04:00
id: string;
2024-05-26 16:58:48 -04:00
columns: number;
2024-05-26 02:14:59 -04:00
closeMenu: () => void;
}
2024-05-26 16:58:48 -04:00
const Gallery = ({ id, columns, closeMenu }: GalleryProps) => {
2024-05-26 02:14:59 -04:00
const [isSingle, setIsSingle] = useState<boolean>(false);
const [loaded, setLoaded] = useState({})
const [selectedImage, setSelectedImage] = useState<string | null>(null);
const [images, setImages] = useState<string[]>([]);
2024-05-26 13:08:59 -04:00
const [galleryId, setGalleryId] = useState(id as string);
2024-05-26 16:36:45 -04:00
const [currentIndex, setCurrentIndex] = useState(0);
2024-05-26 02:14:59 -04:00
const getData = async () => {
2024-05-26 16:45:17 -04:00
const thumbnailResponse = await fetch('/api/galleries/' + String(galleryId) + '/images');
const thumbnailUrl = await thumbnailResponse.json() as string[];
setImages(thumbnailUrl);
2024-05-26 02:14:59 -04:00
}
2024-05-26 16:45:17 -04:00
const next = () => {
if (currentIndex < images.length - 1) {
setCurrentIndex(currentIndex + 1);
} else {
setCurrentIndex(0);
}
2024-05-26 16:36:45 -04:00
}
2024-05-26 16:45:17 -04:00
const previous = () => {
if (currentIndex > 0) {
setCurrentIndex(currentIndex - 1);
} else {
setCurrentIndex(images.length - 1);
}
2024-05-26 16:36:45 -04:00
}
2024-05-26 16:45:17 -04:00
const renderButtons = () => {
return (
2024-05-27 17:02:25 -04:00
<div className="z-20 bottom-10 fixed pt-4 bg-purple-900 bg-opacity-40 animate-in rounded-2xl" style={{ backdropFilter: 'blur(10px)' }}>
<div className='grid grid-cols-4 pl-4 gap-4 pr-4'>
<button
className={`justify-center text-center w-full animate-in animate-once animate-duration-1000 animate-ease-out animate-reverse mb-4 py-2 px-4 rounded-lg no-underline flex items-center z-50 bg-neroshi-blue-900 hover:bg-neroshi-blue-800`}
onClick={() => close()}
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="size-6">
<path strokeLinecap="round" strokeLinejoin="round" d="M6 18 18 6M6 6l12 12" />
</svg>
</button>
<button
className={`justify-center text-center w-full animate-in animate-once animate-duration-1000 animate-ease-out animate-reverse mb-4 py-2 px-4 rounded-lg no-underline flex items-center z-50 ${!selectedImage ? 'opacity-50 cursor-not-allowed bg-gray-800' : 'bg-pink-700 hover:bg-pink-600'}`}
onClick={() => previous()}
disabled={!selectedImage}
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="size-6">
<path strokeLinecap="round" strokeLinejoin="round" d="m18.75 4.5-7.5 7.5 7.5 7.5m-6-15L5.25 12l7.5 7.5" />
</svg>
</button>
<button
className={`justify-center text-center w-full animate-in animate-once animate-duration-1000 animate-ease-out animate-reverse mb-4 py-2 px-4 rounded-lg no-underline flex items-center z-50 ${!selectedImage ? 'opacity-50 cursor-not-allowed bg-gray-800' : 'bg-pink-700 hover:bg-pink-600'}`}
onClick={() => next()}
disabled={!selectedImage}
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="size-6">
<path strokeLinecap="round" strokeLinejoin="round" d="m5.25 4.5 7.5 7.5-7.5 7.5m6-15 7.5 7.5-7.5 7.5" />
</svg>
</button>
<button
className={`justify-center text-center w-full animate-in animate-once animate-duration-1000 animate-ease-out animate-reverse mb-4 py-2 px-4 rounded-lg no-underline flex items-center z-50 ${!selectedImage ? 'opacity-50 cursor-not-allowed bg-gray-800' : 'bg-purple-700 hover:bg-purple-600'}`}
onClick={() => selectedImage && handleDownload(selectedImage)}
disabled={!selectedImage}
>
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="size-6">
<path strokeLinecap="round" strokeLinejoin="round" d="m9 13.5 3 3m0 0 3-3m-3 3v-6m1.06-4.19-2.12-2.12a1.5 1.5 0 0 0-1.061-.44H4.5A2.25 2.25 0 0 0 2.25 6v12a2.25 2.25 0 0 0 2.25 2.25h15A2.25 2.25 0 0 0 21.75 18V9a2.25 2.25 0 0 0-2.25-2.25h-5.379a1.5 1.5 0 0 1-1.06-.44Z" />
</svg>
</button>
2024-05-26 16:45:17 -04:00
</div>
2024-05-27 17:02:25 -04:00
</div>
2024-05-26 16:45:17 -04:00
);
};
2024-05-26 02:14:59 -04:00
const handleDownload = (image: string) => {
const link = document.createElement('a');
link.href = image;
link.download = 'image.jpg'; // or any other filename
link.style.display = 'none';
2024-05-26 16:45:17 -04:00
2024-05-26 02:14:59 -04:00
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
useEffect(() => {
2024-05-26 16:45:17 -04:00
getData();
const handleKeyDown = (event: KeyboardEvent) => {
switch (event.key) {
case 'ArrowLeft':
case 'a':
case 'A':
previous();
break;
case 'ArrowRight':
case 'd':
case 'D':
next();
break;
case 'Escape':
close();
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);
};
}, [selectedImage,currentIndex]);
2024-05-26 02:14:59 -04:00
const handleClick = (image: string) => {
2024-05-26 16:45:17 -04:00
setSelectedImage(image);
setCurrentIndex(images.indexOf(image));
2024-05-26 02:14:59 -04:00
};
2024-05-26 03:46:00 -04:00
const open = () => {
2024-05-26 16:45:17 -04:00
if (selectedImage === null) return;
//console.log(selectedImage)
2024-05-26 03:46:00 -04:00
let base64Image = selectedImage.split(';base64,').pop();
2024-05-26 16:45:17 -04:00
if (!base64Image) return;
let blob = new Blob([Uint8Array.from(atob(base64Image), c => c.charCodeAt(0))], { type: 'image/jpeg' }); // adjust the type as needed
2024-05-26 03:46:00 -04:00
let url = URL.createObjectURL(blob);
window.open(url, '_blank');
}
2024-05-26 16:45:17 -04:00
2024-05-26 21:57:10 -04:00
const panZoomRef = useRef<any>(null);
const resetPanZoom = (event: any) => {
if (panZoomRef.current && event.target.id != "image-container") {
panZoomRef.current.autoCenter();
}
};
2024-05-26 14:36:20 -04:00
const close = () => {
2024-05-26 16:45:17 -04:00
if (selectedImage != null) {
2024-05-26 14:36:20 -04:00
setSelectedImage(null);
2024-05-26 16:45:17 -04:00
setImages([]);
2024-05-26 14:36:20 -04:00
}
2024-05-26 16:45:17 -04:00
else {
2024-05-26 14:36:20 -04:00
closeMenu();
}
}
2024-05-26 02:14:59 -04:00
const breakpointColumnsObj = {
2024-05-26 16:45:17 -04:00
default: 3
2024-05-26 02:14:59 -04:00
};
return (
2024-05-26 21:57:10 -04:00
<div >
<div className="z-20"
onClick={resetPanZoom} style={{ width: selectedImage ? "100%" : "auto", height: selectedImage ? "100%" : "auto" }}>
2024-05-27 17:02:25 -04:00
<div className='flex justify-center items-center pt-2 '>
2024-05-26 21:16:47 -04:00
{renderButtons()}
2024-05-27 17:02:25 -04:00
</div>
2024-05-26 21:16:47 -04:00
{selectedImage ? (
2024-05-26 21:57:10 -04:00
<>
2024-05-26 21:16:47 -04:00
<PanZoom
2024-05-26 21:27:26 -04:00
key={selectedImage}
2024-05-26 21:16:47 -04:00
autoCenter={true}
2024-05-26 21:57:10 -04:00
ref={panZoomRef}
2024-05-26 21:16:47 -04:00
>
2024-05-26 21:57:10 -04:00
{/*
<div
onClick={() => resetPanZoom()} className='w-full h-full z-10'>
</div> */}
<div id="image-container" >
2024-05-26 21:16:47 -04:00
<img
src={images[currentIndex]}
style={{ objectFit: "contain", maxWidth: "100%", maxHeight: "calc(100vh - 20px)", pointerEvents:"none" }}
className="cursor-pointer animate-in w-full h-auto"
2024-05-26 21:57:10 -04:00
>
</img>
</div>
2024-05-26 21:16:47 -04:00
</PanZoom>
2024-05-26 21:57:10 -04:00
</>
2024-05-26 21:16:47 -04:00
) : (
<div
className="z-30 pb-10"
style={{
display: selectedImage ? "flex" : "block",
alignItems: "flex-start",
}}
2024-05-27 17:02:25 -04:00
> <div className='flex justify-center items-center pt-2 '>
2024-05-26 21:16:47 -04:00
<Masonry
breakpointCols={columns}
2024-05-27 17:02:25 -04:00
className="my-masonry-grid pl-6"
2024-05-26 21:16:47 -04:00
style={{ width: selectedImage ? "50%" : "100%" }}
>
{images
.filter((img) => img !== selectedImage)
.map((image, index) => (
<img
src={image}
onClick={() => 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`}
/>
))}
</Masonry>
2024-05-27 17:02:25 -04:00
</div>
2024-05-26 21:16:47 -04:00
<>
2024-05-26 19:49:20 -04:00
</>
</div>
2024-05-26 21:16:47 -04:00
)}
2024-05-26 02:14:59 -04:00
</div>
2024-05-26 21:57:10 -04:00
</div>
2024-05-26 02:14:59 -04:00
);
}
export default Gallery;