'use client'; import Image from 'next/image'; import { useEffect, useRef } from 'react'; import type { TransformPair } from '../data/transformPairs'; import { catalogStartingSalePrice } from '../lib/pricing'; import ButtonIcon from './ButtonIcon'; // "Atelier Proof" — each pair is a single composition where the customer's // raw phone snap sits as a tilted polaroid leaning against the corner of // the finished framed portrait. Real-world artist gesture (proof shot of // reference + final work), executed with washi-tape, paper grain, and a // staggered scroll-in animation. No before/after slider — proximity does // the work. // // Presentational only: the pair data is passed in, so the same section // drives both the home page (HOME_PAIRS) and the /gallery page // (GALLERY_PAIRS). type TransformProps = { pairs: TransformPair[]; eyebrow: string; heading: string; lead: string; /** Heading element — h2 inside the home page, h1 on the gallery page. */ headingTag?: 'h1' | 'h2'; /** Show a "Shop Gallery" button after the pairs (home page only). */ galleryLink?: boolean; /** Show each commission's price with the site-wide sale applied (gallery). */ showPrices?: boolean; /** * How many leading pairs to load eagerly with `priority` (Next/Image * emits a `` for these so they're ready before * first paint). Defaults to 3 — fine for the home page where only * 3 pairs render. The /gallery page passes 6 so the first ~2-3 * viewports of pairs are already warm before any scroll happens. */ priorityCount?: number; }; export function Transform({ pairs, eyebrow, heading, lead, headingTag = 'h2', galleryLink = false, showPrices = false, priorityCount = 3, }: TransformProps) { const rootRef = useRef(null); const Heading = headingTag; // Three layers of fade/preload, tuned to keep the gallery from showing // empty slots on fast scroll: // // 1. reveal IO — adds .is-visible so the pair fades in. We trigger // it well *before* the pair enters the viewport (rootMargin // 400px below + threshold 0) so a fast scroll never outruns it. // 2. preload IO — kicks `new Image().src` for each pair when it // crosses ~3000px from the viewport edges. This warms the browser // HTTP cache so the eventual fetch hits cache instantly. // 3. idle preload — after first paint, we sequentially fetch every // non-priority pair's after-image in the background using // requestIdleCallback. This means the whole gallery is // effectively cached within seconds of landing, and even the // fastest scroll has warm images waiting. // // The reveal animation stays for visual polish but never gates // content — by the time the fade triggers, the image bytes are // already there. useEffect(() => { const root = rootRef.current; if (!root) return; const pairEls = root.querySelectorAll('.transform__pair'); const reveal = new IntersectionObserver( (entries) => { for (const e of entries) { if (e.isIntersecting) { e.target.classList.add('is-visible'); reveal.unobserve(e.target); } } }, // Start fading the pair in when its top edge is still 400px below // the viewport bottom — by the time the user can see it, it's // already mid-fade. Threshold 0 so even 1px of intersection counts. { threshold: 0, rootMargin: '0px 0px 400px 0px' }, ); const preload = new IntersectionObserver( (entries) => { for (const e of entries) { if (!e.isIntersecting) continue; const el = e.target as HTMLElement; el.querySelectorAll('img').forEach((img) => { const src = img.currentSrc || img.src; if (!src) return; const warm = new window.Image(); warm.src = src; }); preload.unobserve(el); } }, // 3000px headroom on both sides — at typical scroll speeds, this // gives the network ~5-8 seconds to warm an image before the // user reaches it. Bumped from 1500px because the previous // value let fast scrolls outrun the cache. { rootMargin: '3000px 0px 3000px 0px' }, ); pairEls.forEach((p) => { reveal.observe(p); preload.observe(p); }); // Idle-time bulk preload — fire fetches for every after-image // sequentially, one at a time, throttled to avoid choking the // network. Browser handles cache dedup, so if a pair was already // warmed by the IO above, this is a no-op for that URL. // Skipped on the home page (3 pairs all priority-loaded) since // there's nothing to warm beyond what's already eager. const afterSrcs = pairs.slice(priorityCount).map((p) => p.after.src); let cancelled = false; const warmNext = (i: number) => { if (cancelled || i >= afterSrcs.length) return; const img = new window.Image(); img.onload = img.onerror = () => warmNext(i + 1); img.src = afterSrcs[i]; }; // requestIdleCallback isn't in older Safari — fall back to setTimeout. const ric = (cb: () => void) => typeof window.requestIdleCallback === 'function' ? window.requestIdleCallback(cb, { timeout: 2000 }) : window.setTimeout(cb, 250); ric(() => warmNext(0)); return () => { cancelled = true; reveal.disconnect(); preload.disconnect(); }; }, [pairs, priorityCount]); return (
{eyebrow}
{heading}

{lead}

{pairs.map((p, i) => { // Leading pairs are above-the-fold or close — load eagerly // with `priority` so they appear instantly without any lazy // delay. The rest are lazy + warmed by the preload IO above // and the idle-time bulk warmer in useEffect. const isPriority = i < priorityCount; return (
{/* AFTER — the finished framed portrait, in-home */}
{p.after.alt}
{/* BEFORE — the customer's phone snap. Desktop: a tilted polaroid leaning on the after. Mobile: a flat bordered print stacked above the after (see globals.css). */}
What we worked from
№ {p.n}
{p.name}
{p.medium}
{showPrices && ( // "From $X" pricing — the gallery photos are AI-generated // room scenes where Gemini drew the frame at whatever // scale looked right, so we can't honestly tag each one // with a specific dimension OR product. Advertise the // catalogue-wide entry price (cheapest way into the whole // range); customer picks the actual product + size in the // builder.
From ${catalogStartingSalePrice()} at cost
)}

{p.blurb}

{ // Forward style + frame + product but NOT size — the // lifestyle photo doesn't represent any specific // dimension, so locking the builder to one would be // making the same lie this fix was meant to remove. // Customer picks size in the builder. const q = new URLSearchParams(); if (p.styleId) q.set('style', p.styleId); if (p.frameId) q.set('frame', p.frameId); if (p.matteId) q.set('matte', p.matteId); q.set('product', p.productId); return `/builder?${q.toString()}`; })()} > Start one like this
); })}
{galleryLink && (
Shop Gallery
)}
); }