'use client'; import { useEffect, useRef, type CSSProperties } from 'react'; import { FRAMES, MATTES, normalizeMatteId, isNoMat, type FrameId } from '../data/frames'; import { PRODUCTS, frameAspect, type ProductId, type Orientation } from '../lib/pricing'; // Reusable framed-image preview. Reuses the same `.slideshow__mount` // CSS the Builder uses for the live preview — frame moulding + mat // board + image viewport — so admin previews, customer approve view, // and the Builder all render the artwork with the customer's exact // chosen product / size / frame / mount config. type Props = { src: string; alt?: string; productId: ProductId; size: string; /** Portrait (default) or landscape — transposes the mount aspect so * the preview frame matches the orientation the customer ordered. * Legacy orders without it render portrait. */ orientation?: Orientation; frame: FrameId; matteId?: string | null; className?: string; onImageLoad?: () => void; onImageError?: () => void; }; export function FramedPreview({ src, alt, productId, size, orientation, frame, matteId, className, onImageLoad, onImageError }: Props) { const imageRef = useRef(null); useEffect(() => { const image = imageRef.current; if (!image?.complete) return; if (image.naturalWidth) onImageLoad?.(); else onImageError?.(); }, [src, onImageLoad, onImageError]); const product = PRODUCTS[productId]; const sizes = product.sizes as Record; const sizeMeta = sizes[size] ?? sizes[product.defaultSize]; const mountAspect = frameAspect(productId, size, orientation ?? 'portrait') ?? sizeMeta.aspect; // Products with a mount (box frame, classic frame) expose // `availableMattes` and render a real mat board; canvas does not. const hasMount = ('availableMattes' in product ? product.availableMattes : []).length > 0; const matKey = normalizeMatteId(matteId); // "No matte" drops the mat board entirely. const noMat = hasMount && isNoMat(matteId); const frameDef = FRAMES[frame] ?? FRAMES.black; // Canvas float frames don't carry a mat board — render the 5 mm // recess gap as a dark shadow tone instead. Mounted products read // their mount colour from MATTES. const matteBg = hasMount ? MATTES[matKey].hex : '#0d0a08'; const style: CSSProperties = { '--frame-w': `${product.frameW}px`, '--matte-w': `${noMat ? 0 : product.matteW}px`, '--frame-bg': frameDef.frame, '--matte-bg': matteBg, '--mount-aspect': mountAspect, } as CSSProperties; return (
{/* eslint-disable-next-line @next/next/no-img-element */} {alt
); }