'use client'; import { useState, useEffect } from 'react'; import { useIntersectionObserver } from '@/hooks/useIntersectionObserver'; import { useImageDownloadTime } from '@/components/shared/ImageCard/useImageDownloadTime'; import { ImageMetadataBar } from '@/components/shared/ImageMetadataBar'; import { calculateAspectRatio } from '@/utils/imageUtils'; import { usePageContext } from '@/contexts/page-context'; import { ExpandedImageView } from '@/components/shared/ExpandedImageView'; type GalleryImageCardProps = { imageUrl: string; filename: string; size: number; contentType: string; lastModified: string; onDownloadMeasured: (imageId: string, downloadMs: number) => void; }; export const GalleryImageCard = ({ imageUrl, filename, size, contentType, lastModified, onDownloadMeasured, }: GalleryImageCardProps) => { const { openModal } = usePageContext(); const [isVisible, setIsVisible] = useState(false); const [imageDimensions, setImageDimensions] = useState<{ width: number; height: number; } | null>(null); const { ref } = useIntersectionObserver({ onIntersect: () => setIsVisible(true), threshold: 0.1, }); const { downloadTime } = useImageDownloadTime(isVisible ? imageUrl : null); useEffect(() => { if (downloadTime !== null) { onDownloadMeasured(imageUrl, downloadTime); } }, [downloadTime, imageUrl, onDownloadMeasured]); useEffect(() => { if (!isVisible) return; const img = new Image(); img.onload = () => { setImageDimensions({ width: img.width, height: img.height, }); }; img.src = imageUrl; }, [isVisible, imageUrl]); const handleImageClick = () => { openModal(); }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); handleImageClick(); } }; const formatTimestamp = (dateString: string): string => { const date = new Date(dateString); return date.toLocaleString('en-US', { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', }); }; return (
{isVisible ? ( <> {filename}
{formatTimestamp(lastModified)}
{filename}
) : (
)}
{imageDimensions && ( )}
); };