Branchwell

Lumen & Gesso / website · lumengesso.com / app / components / FramedPreview.tsx

1'use client';
2
3import { useEffect, useRef, type CSSProperties } from 'react';
4import { FRAMES, MATTES, normalizeMatteId, isNoMat, type FrameId } from '../data/frames';
5import { PRODUCTS, frameAspect, type ProductId, type Orientation } from '../lib/pricing';
6
7// Reusable framed-image preview. Reuses the same `.slideshow__mount`
8// CSS the Builder uses for the live preview — frame moulding + mat
9// board + image viewport — so admin previews, customer approve view,
10// and the Builder all render the artwork with the customer's exact
11// chosen product / size / frame / mount config.
12type Props = {
13 src: string;
14 alt?: string;
15 productId: ProductId;
16 size: string;
17 /** Portrait (default) or landscape — transposes the mount aspect so
18 * the preview frame matches the orientation the customer ordered.
19 * Legacy orders without it render portrait. */
20 orientation?: Orientation;
21 frame: FrameId;
22 matteId?: string | null;
23 className?: string;
24 onImageLoad?: () => void;
25 onImageError?: () => void;
26};
27
28export function FramedPreview({ src, alt, productId, size, orientation, frame, matteId, className, onImageLoad, onImageError }: Props) {
29 const imageRef = useRef<HTMLImageElement>(null);
30 useEffect(() => {
31 const image = imageRef.current;
32 if (!image?.complete) return;
33 if (image.naturalWidth) onImageLoad?.();
34 else onImageError?.();
35 }, [src, onImageLoad, onImageError]);
36 const product = PRODUCTS[productId];
37 const sizes = product.sizes as Record<string, { aspect: string }>;
38 const sizeMeta = sizes[size] ?? sizes[product.defaultSize];
39 const mountAspect = frameAspect(productId, size, orientation ?? 'portrait') ?? sizeMeta.aspect;
40 // Products with a mount (box frame, classic frame) expose
41 // `availableMattes` and render a real mat board; canvas does not.
42 const hasMount = ('availableMattes' in product ? product.availableMattes : []).length > 0;
43 const matKey = normalizeMatteId(matteId);
44 // "No matte" drops the mat board entirely.
45 const noMat = hasMount && isNoMat(matteId);
46 const frameDef = FRAMES[frame] ?? FRAMES.black;
47
48 // Canvas float frames don't carry a mat board — render the 5 mm
49 // recess gap as a dark shadow tone instead. Mounted products read
50 // their mount colour from MATTES.
51 const matteBg = hasMount ? MATTES[matKey].hex : '#0d0a08';
52
53 const style: CSSProperties = {
54 '--frame-w': `${product.frameW}px`,
55 '--matte-w': `${noMat ? 0 : product.matteW}px`,
56 '--frame-bg': frameDef.frame,
57 '--matte-bg': matteBg,
58 '--mount-aspect': mountAspect,
59 } as CSSProperties;
60
61 return (
62 <div className={`slideshow${className ? ' ' + className : ''}`}>
63 <div className="slideshow__mount" style={style}>
64 <div className={`slideshow__matte${noMat ? ' is-no-matte' : ''}`}>
65 <div className="slideshow__viewport">
66 <div className="slideshow__track">
67 <div className="slideshow__slide is-active">
68 {/* eslint-disable-next-line @next/next/no-img-element */}
69 <img ref={imageRef} src={src} alt={alt ?? ''} onLoad={onImageLoad} onError={onImageError} />
70 </div>
71 </div>
72 </div>
73 </div>
74 </div>
75 </div>
76 );
77}