import React, { useState, useRef, useEffect } from 'react' import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from '@/ui/components/ui/dialog' import { Button } from '@/ui/components/ui/button' import { Move, ZoomIn, ZoomOut, Loader2 } from 'lucide-react' import { m } from '@/ui/paraglide/messages.js' interface AvatarCropDialogProps { isOpen: boolean onClose: () => void imageSrc: string | null onConfirm: (croppedBlob: Blob) => Promise } export function AvatarCropDialog({ isOpen, onClose, imageSrc, onConfirm }: AvatarCropDialogProps) { const [zoom, setZoom] = useState(1.0) const [offset, setOffset] = useState({ x: 0, y: 0 }) const [isDragging, setIsDragging] = useState(false) const [isSaving, setIsSaving] = useState(false) const dragStart = useRef({ x: 0, y: 0 }) const imageRef = useRef(null) const containerRef = useRef(null) // Layout parameters const VIEWPORT_SIZE = 250 // Diameter of the circle crop in pixels // Reset parameters when a new image is loaded useEffect(() => { if (isOpen) { setZoom(1.0) setOffset({ x: 0, y: 0 }) setIsSaving(false) } }, [isOpen, imageSrc]) const handleMouseDown = (e: React.MouseEvent) => { e.preventDefault() setIsDragging(true) dragStart.current = { x: e.clientX - offset.x, y: e.clientY - offset.y } } const handleMouseMove = (e: React.MouseEvent) => { if (!isDragging) return setOffset({ x: e.clientX - dragStart.current.x, y: e.clientY - dragStart.current.y, }) } const handleMouseUp = () => { setIsDragging(false) } // Touch support for mobile devices const handleTouchStart = (e: React.TouchEvent) => { if (e.touches.length !== 1) return setIsDragging(true) const touch = e.touches[0] dragStart.current = { x: touch.clientX - offset.x, y: touch.clientY - offset.y } } const handleTouchMove = (e: React.TouchEvent) => { if (!isDragging || e.touches.length !== 1) return const touch = e.touches[0] setOffset({ x: touch.clientX - dragStart.current.x, y: touch.clientY - dragStart.current.y, }) } const handleConfirm = async () => { const img = imageRef.current const container = containerRef.current if (!img || !container) return setIsSaving(true) try { // Natural dimensions of the image vs rendered dimensions const rect = img.getBoundingClientRect() // Canvas dimensions (high quality cropped resolution) const CROP_RESOLUTION = 512 const canvas = document.createElement('canvas') canvas.width = CROP_RESOLUTION canvas.height = CROP_RESOLUTION const ctx = canvas.getContext('2d') if (ctx) { // Clear canvas ctx.clearRect(0, 0, CROP_RESOLUTION, CROP_RESOLUTION) // Draw square crop (no circular clipping path to allow square renders) // Calculate translation and scaling factors // Rendered image center in container coordinates: // center_x = container_width / 2 + offset.x // center_y = container_height / 2 + offset.y // Image rendered width = rect.width, height = rect.height // We map these container coordinate space values to the canvas coordinate space: // Scale factor between canvas and UI container viewport (e.g. 512 / 250) const scaleToCanvas = CROP_RESOLUTION / VIEWPORT_SIZE // Rendered width/height at zoom 1.0 (before scaling) const baseWidth = rect.width / zoom const baseHeight = rect.height / zoom // Calculate centered offset in canvas coordinates const canvasX = CROP_RESOLUTION / 2 + offset.x * scaleToCanvas const canvasY = CROP_RESOLUTION / 2 + offset.y * scaleToCanvas // Draw the image scaled and panned ctx.translate(canvasX, canvasY) ctx.scale(zoom * scaleToCanvas, zoom * scaleToCanvas) ctx.drawImage(img, -baseWidth / 2, -baseHeight / 2, baseWidth, baseHeight) // Export canvas as blob const blob = await new Promise((resolve) => { canvas.toBlob((b) => resolve(b), 'image/jpeg', 0.9) }) if (blob) { await onConfirm(blob) } } } catch (err) { console.error('Failed to crop image', err) } finally { setIsSaving(false) } } return ( !open && onClose()}> {m.position_and_resize()}
{/* Viewport Container */}
{imageSrc && ( {m.avatar_source()} )} {/* Circular Mask Overlay */}
{/* Zoom Slider */}
setZoom(parseFloat(e.target.value))} className="flex-1 h-1.5 bg-muted rounded-lg appearance-none cursor-pointer accent-primary focus:outline-none" />
) }