'use client'; /** * ImageInfo - Displays image dimensions badge */ import { useEffect, useState } from 'react'; import { useImageCache } from '../../../../stores/mediaCache'; import type { ImageInfoProps } from '../types'; // ============================================================================= // COMPONENT // ============================================================================= export function ImageInfo({ src }: ImageInfoProps) { const { getDimensions, cacheDimensions } = useImageCache(); const [dimensions, setDimensions] = useState<{ w: number; h: number } | null>(null); useEffect(() => { // Check cache first const cached = getDimensions(src); if (cached) { setDimensions({ w: cached.width, h: cached.height }); return; } // Clear stale dimensions while the new source resolves setDimensions(null); // Load and cache dimensions let cancelled = false; const img = new Image(); img.onload = () => { if (cancelled) return; const dims = { w: img.naturalWidth, h: img.naturalHeight }; setDimensions(dims); cacheDimensions(src, { width: dims.w, height: dims.h }); }; img.src = src; return () => { cancelled = true; img.onload = null; img.src = ''; }; }, [src, getDimensions, cacheDimensions]); if (!dimensions) return null; return (
{dimensions.w} × {dimensions.h}
); }