'use client'; import { createPasskeyAccount } from '@/app/lib/auth-client'; import { useEffect, useRef, useState } from 'react'; import styles from './studio.module.css'; type Selection = { productId: string; size: string; orientation: 'portrait' | 'landscape'; frame: string; matteId?: string; wrap?: string }; type PrintQuote = { quoteToken: string; amountCents: number; currency: 'USD'; selection: Selection; width: number; height: number; artworkSha256: string; expiresAt: string | number; }; type Access = { enabled: boolean; signedIn: boolean; signupAvailable?: boolean }; const SELECTION: Selection = { productId: 'classic', size: '8x8', orientation: 'portrait', frame: 'black', matteId: 'nomat' }; export default function PrintPanel({ canvas, hasImage }: { canvas: HTMLCanvasElement | null; hasImage: boolean }) { const [access, setAccess] = useState(); const [image, setImage] = useState(); const [preview, setPreview] = useState(''); const [quote, setQuote] = useState(); const [approval, setApproval] = useState(false); const [terms, setTerms] = useState(false); const [busy, setBusy] = useState(false); const [error, setError] = useState(''); const [notice, setNotice] = useState(''); const processing = useRef(false); useEffect(() => { const c = new AbortController(); fetch('/api/studio/print/quote', { cache: 'no-store', signal: c.signal }) .then(r => r.ok ? r.json() as Promise : undefined) .then(info => { if (!c.signal.aborted) setAccess(info ?? { enabled: false, signedIn: false }); }) .catch(() => { if (!c.signal.aborted) setAccess({ enabled: false, signedIn: false }); }); return () => c.abort(); }, []); useEffect(() => { if (!image) { setPreview(''); return; } const url = URL.createObjectURL(image); setPreview(url); return () => URL.revokeObjectURL(url); }, [image]); function snapshot() { if (!canvas || !hasImage) throw new Error('Upload or create an image first.'); if (canvas.width !== canvas.height) throw new Error('Choose Square under Adjust, then return here to review the 8 × 8 print.'); // Freeze the pixels before any asynchronous work. A new proof always clears consent. const rendition = document.createElement('canvas'); rendition.width = canvas.width; rendition.height = canvas.height; const context = rendition.getContext('2d'); if (!context) throw new Error('Could not preview this print.'); context.fillStyle = '#ffffff'; context.fillRect(0, 0, rendition.width, rendition.height); context.drawImage(canvas, 0, 0); return new Promise((resolve, reject) => rendition.toBlob(blob => blob ? resolve(blob) : reject(new Error('Could not encode this print.')), 'image/jpeg', .95)); } async function prepare() { if (processing.current) return; processing.current = true; setBusy(true); setError(''); setQuote(undefined); setImage(undefined); setApproval(false); setTerms(false); try { const exact = await snapshot(); const form = new FormData(); form.set('image', exact, 'approved-artwork.jpg'); form.set('selection', JSON.stringify(SELECTION)); const reply = await fetch('/api/studio/print/quote', { method: 'POST', body: form }); const data = await reply.json() as PrintQuote & { error?: string }; if (!reply.ok) throw new Error(data.error || 'Could not prepare a print quote.'); setImage(exact); setQuote(data); } catch (cause) { setError((cause as Error).message); } finally { setBusy(false); processing.current = false; } } async function requestSignIn() { if (processing.current) return; processing.current = true; setBusy(true); setError(''); setNotice(''); try { const problem = await createPasskeyAccount(); if (problem) setNotice(problem); else window.location.reload(); } catch (cause) { setError((cause as Error).message); } finally { setBusy(false); processing.current = false; } } async function checkout() { if (!image || !quote || !approval || !terms || processing.current) return; const expiry = typeof quote.expiresAt === 'number' ? quote.expiresAt : Date.parse(quote.expiresAt); if (expiry <= Date.now()) { setError('The quote expired. Review the current canvas and request a new price.'); setQuote(undefined); setApproval(false); setTerms(false); return; } processing.current = true; setBusy(true); setError(''); try { const form = new FormData(); form.set('image', image, 'approved-artwork.jpg'); form.set('quoteToken', quote.quoteToken); form.set('approved', 'true'); form.set('termsAccepted', 'true'); const reply = await fetch('/api/studio/print/checkout', { method: 'POST', body: form }); const data = await reply.json() as { url?: string; error?: string }; if (!reply.ok || !data.url) throw new Error(data.error || 'Could not open checkout.'); const destination = new URL(data.url); if (destination.origin !== 'https://checkout.stripe.com') throw new Error('Checkout returned an unexpected address.'); window.location.assign(destination.href); } catch (cause) { setError((cause as Error).message); } finally { setBusy(false); processing.current = false; } } return

Approve your own print

Digital downloads are still free. Review the exact artwork and price. The printer receives the approved file only after successful payment.

{!access ?

Checking print availability…

: !access.enabled ?

Physical print checkout is not open yet.

: !access.signedIn ?

Sign in with your passkey for print checkout. Save your edits first.

{access.signupAvailable && }
: <>

Classic matte-black frame · 8 × 8 in · no mat

{preview && quote &&
{/* eslint-disable-next-line @next/next/no-img-element */} Exact image to be sent to the printer

{new Intl.NumberFormat('en-US', { style: 'currency', currency: quote.currency }).format(quote.amountCents / 100)} before tax · {quote.width} × {quote.height} px

} } {notice &&

{notice}

} {busy &&

Working…

} {error &&

{error}

}
; }