'use client'; import { useEffect, useRef, useState, type ChangeEvent, type DragEvent } from 'react'; import { CropModal } from './CropModal'; import { ACCEPTED_IMAGE_ACCEPT, validateUpload } from '../lib/upload-limits'; type ImageDropProps = { /** Prompt heading shown in the empty state. */ title: string; /** Prompt sub-line shown in the empty state. */ hint: string; /** CSS aspect-ratio for the zone (default square). */ ratio?: string; /** When true, a picked image opens the square crop tool before it's accepted. */ crop?: boolean; /** Extra class names appended to the zone (layout/variant hooks). */ className?: string; /** * Optional pre-existing File to seed the preview from. The parent * (e.g. the wizard Builder) holds the picked File in its own state, * but this component unmounts when the customer navigates between * steps. Passing the persisted File back in as `value` lets the * preview survive that round-trip. New picks via the file input or * drag-drop still go through `onChange`. */ value?: File | null; /** Receives the accepted image File. Local only — no network upload. */ onChange?: (file: File) => void; /** * When provided, a remove (×) button appears over the preview so the * customer can clear the image entirely (back to the empty prompt), * not just replace it. Opt-in — pet-photo slots manage their own * removal at the slot level, so they leave this unset. */ onClear?: () => void; }; // A drag-and-drop / click-to-browse image upload zone. Accepts a single // image and previews it locally via an object URL. Deliberately does NOT // upload anywhere — the File is handed to onChange for the caller to // hold; wiring it to a backend is a later step. When `crop` is set, the // picked image is routed through the square CropModal first. export function ImageDrop({ title, hint, ratio = '1', crop = false, className, value, onChange, onClear }: ImageDropProps) { const inputRef = useRef(null); const [preview, setPreview] = useState(null); const [dragging, setDragging] = useState(false); const [pendingCrop, setPendingCrop] = useState(null); const [error, setError] = useState(null); // Single source of truth for the preview object URL: the parent-held // `value` File. We create the URL here, hand it to the , and // revoke it on cleanup. New picks (file input, drag-drop, crop output) // are routed up via onChange so the parent updates state; this effect // re-runs with the new File and regenerates the URL. // // Owning the URL lifecycle in one effect (rather than also creating // URLs in finalize) is what makes the preview survive step-switch // unmount/remount cycles AND React's strict-mode double-invoke // (where unmount-cleanup-then-remount would otherwise leave the // `` pointing at a revoked URL). useEffect(() => { if (!value) { setPreview(null); return; } const url = URL.createObjectURL(value); setPreview(url); return () => URL.revokeObjectURL(url); }, [value]); // Commit an accepted (already-cropped, if applicable) file. Hand it // up to the parent; the value-sync effect above takes care of the // preview on the next render. const finalize = (file: File) => { onChange?.(file); }; const handleFile = (file: File | undefined | null) => { if (!file) return; // Enforce type (JPG/PNG only) + size (≤10MB). Mirror of the // server-side check in /api/orders so the customer gets instant // feedback instead of a failed submit. Rejects HEIC, WebP, GIF, // oversized files, etc. const validationError = validateUpload(file); if (validationError) { setError(validationError); return; } setError(null); if (crop) setPendingCrop(file); else finalize(file); }; const onInputChange = (e: ChangeEvent) => { handleFile(e.target.files?.[0]); e.target.value = ''; // allow re-picking the same file }; const onDrop = (e: DragEvent) => { e.preventDefault(); setDragging(false); handleFile(e.dataTransfer.files?.[0]); }; const openPicker = () => inputRef.current?.click(); return ( <>
{ if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); openPicker(); } }} onDragOver={(e) => { e.preventDefault(); setDragging(true); }} onDragLeave={() => setDragging(false)} onDrop={onDrop} > {preview ? ( <> {/* eslint-disable-next-line @next/next/no-img-element */}
Replace image
{onClear && ( )} ) : (
{title}
{hint}
)}
{error &&

{error}

} {pendingCrop && ( setPendingCrop(null)} onCrop={(cropped) => { setPendingCrop(null); finalize(cropped); }} /> )} ); }