import { memo, useEffect, useRef, useState } from 'react'; import { thumbTransform } from '../lib/edits'; import { useEditedThumb } from '../lib/editThumbs'; import type { PhotoRecord } from '../types'; interface Props { photo: PhotoRecord; /** Aspect of the tile the picture fills; the rotation transform needs it. */ boxAspect?: number; alt?: string; loading?: 'lazy' | 'eager'; draggable?: boolean; } /** * A thumbnail that follows the photo's edits. * * Edited photos get a freshly rendered small JPEG; everything else keeps the * server/import thumbnail and the CSS orientation transform. Rendering starts * only when the tile is close to the viewport. */ function EditedImage({ photo, boxAspect = 4 / 3, alt, loading = 'lazy', draggable = false, }: Props) { const ref = useRef(null); const [nearView, setNearView] = useState(false); useEffect(() => { const el = ref.current; if (!el) return; if (typeof IntersectionObserver === 'undefined') { setNearView(true); return; } const observer = new IntersectionObserver( (entries) => { if (!entries.some((entry) => entry.isIntersecting)) return; setNearView(true); observer.disconnect(); }, { rootMargin: '400px' }, ); observer.observe(el); return () => observer.disconnect(); }, []); const edited = useEditedThumb(photo, nearView); return ( {alt ); } /** * Memoised: a selection change on the library re-renders the whole grid, and a * picture whose photo, size and props did not change has no work to redo. */ export default memo(EditedImage);