import { Spinner } from '@wordpress/components'; import { useEffect, useState, useInsertionEffect } from '@wordpress/element'; import classNames from 'classnames'; import { AnimatePresence, motion } from 'framer-motion'; import { loadImage } from '@draft/lib/image'; export const UnsplashImage = ({ image, skeletonHeight, isInsertingImage, onClick, }) => { const [authorUrl, setAuthorUrl] = useState(''); const [loaded, setLoaded] = useState(false); const aspectRatio = image?.width ? Number(image?.width) / Number(image?.height) : 122 / skeletonHeight; useEffect(() => { if (!image?.user?.links?.html) { setAuthorUrl(''); return; } const authorUrl = new URL(image.user.links.html); authorUrl.searchParams.set('utm_source', 'extendify'); authorUrl.searchParams.set('utm_medium', 'referral'); setAuthorUrl(authorUrl.toString()); }, [image]); useInsertionEffect(() => { if (!image?.urls || loaded) return; const img = new Image(); img.src = image.urls.thumb || image.urls.small; loadImage(img).then(() => setLoaded(true)); }, [image, loaded]); return ( {loaded ? null : (
)}
{image?.user?.name && authorUrl ? ( {`${image.user?.name}`} ) : null}
); };