neroshitron/components/ui/gallery_thumbnail.tsx

45 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-05-26 03:46:00 -04:00
import { useState, useEffect } from 'react';
2024-05-26 02:14:59 -04:00
interface GalleryThumbnailProps {
2024-05-26 13:08:59 -04:00
id: string;
2024-05-26 16:58:48 -04:00
columns: number;
onSelect: (id:string, columns:number) => void;
2024-05-26 02:14:59 -04:00
}
2024-05-26 16:58:48 -04:00
const GalleryThumbnail = ({ id, columns, onSelect }: GalleryThumbnailProps) => {
2024-05-26 13:08:59 -04:00
const [galleryId, setGalleryId] = useState<string>(id);
const [thumbnailUrl, setThumbnailUrl] = useState<string>('');
const [isLoading, setIsLoading] = useState<boolean>(true);
2024-05-26 16:58:48 -04:00
const [galleryCollumns, setColumns] = useState<number>(columns);
2024-05-26 13:08:59 -04:00
const openGallery = () => {
2024-05-26 16:58:48 -04:00
onSelect(galleryId, galleryCollumns);
2024-05-26 02:14:59 -04:00
};
const getData = async () => {
2024-05-26 03:46:00 -04:00
setIsLoading(true);
2024-05-26 02:14:59 -04:00
const thumbnailResponse = await fetch('/api/galleries/'+galleryId+'/thumbnail');
const thumbnailUrl = await thumbnailResponse.text();
setThumbnailUrl(thumbnailUrl);
2024-05-26 03:46:00 -04:00
setIsLoading(false);
2024-05-26 02:14:59 -04:00
}
2024-05-26 03:46:00 -04:00
2024-05-26 02:14:59 -04:00
useEffect(() => {
getData();
}, []);
return (
2024-05-26 03:46:00 -04:00
<div className="py-3 sm:max-w-xl sm:mx-auto flex-3">
<div className="h-48 overflow-visible w-full relative hover:scale-105 shadow-lg bg-gray-400 rounded-3xl">
{!isLoading && <img
2024-05-26 02:14:59 -04:00
className={`aspect-content rounded-3xl`}
src={thumbnailUrl}
alt=""
2024-05-26 13:08:59 -04:00
onClick={openGallery}
2024-05-26 02:14:59 -04:00
style={{ width: '20rem', height: '20rem', objectFit: 'cover' }}
2024-05-26 03:46:00 -04:00
/>}
2024-05-26 02:14:59 -04:00
</div>
</div>
);
}
export default GalleryThumbnail;