'use client' import 'cropperjs/dist/cropper.css' import { useRef, useState, type ComponentType } from 'react' import { Cropper as RawCropper, type ReactCropperElement, type ReactCropperProps } from 'react-cropper' // react-cropper ships types compiled against a different @types/react major // than this app, which makes TS reject it as a JSX component. Re-type it // against this app's React types (behaviour is identical at runtime). const Cropper = RawCropper as unknown as ComponentType< ReactCropperProps & { ref?: React.Ref } > // --------------------------------------------------------------------------- // Types // --------------------------------------------------------------------------- interface CropImageDialogProps { /** Object URL of the source image to crop. */ src: string /** Aspect ratio for the crop box (width / height). e.g. 1 for square avatar. */ cropAspectRatio: number /** Receives the cropped image as a webp Blob, or null if the canvas failed. */ onCropped: (blob: Blob | null) => void /** Called when the dialog should close (cancel or after crop). */ onClose: () => void /** Optional title shown in the header. */ title?: string } // --------------------------------------------------------------------------- // Component // --------------------------------------------------------------------------- /** * Modal crop + resize dialog. Produces an aspect-correct webp Blob via the * cropper canvas. Adapted from the nextjs-15 social app's CropImageDialog to * the product's plain-overlay modal convention (matching ContentComposer's * checkpoint picker) and CSS-variable dark theme. */ export default function CropImageDialog({ src, cropAspectRatio, onCropped, onClose, title = 'Crop image', }: CropImageDialogProps) { const cropperRef = useRef(null) const [processing, setProcessing] = useState(false) const handleCrop = () => { const cropper = cropperRef.current?.cropper if (!cropper) return setProcessing(true) // getCroppedCanvas is bounded to avoid oversized uploads. const canvas = cropper.getCroppedCanvas({ maxWidth: 1024, maxHeight: 1024, imageSmoothingQuality: 'high', }) if (!canvas) { setProcessing(false) onCropped(null) onClose() return } canvas.toBlob( (blob) => { setProcessing(false) onCropped(blob) onClose() }, 'image/webp', 0.9, ) } return (

{title}

) }