import React from "react"; import {Camera, Upload} from "lucide-react"; import {Button} from "@/components/ui/button"; interface ImageUploadProps { onImageUpload: (file: File) => void; sideEffect: (text: string | null) => void; } export default function ImageUpload({onImageUpload, sideEffect}: ImageUploadProps) { const [isDragging, setIsDragging] = React.useState(false); const fileInputRef = React.useRef(null); const videoRef = React.useRef(null); const [isCameraActive, setIsCameraActive] = React.useState(false); const handleDragOver = (e: React.DragEvent) => { e.preventDefault(); setIsDragging(true); }; const handleDragLeave = () => { setIsDragging(false); }; const handleDrop = (e: React.DragEvent) => { e.preventDefault(); setIsDragging(false); const file = e.dataTransfer.files[0]; if (file) { onImageUpload(file); } }; const handleFileInput = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { onImageUpload(file); } }; const activateCamera = async () => { try { const stream = await navigator.mediaDevices.getUserMedia({video: true}); if (videoRef.current) { videoRef.current.srcObject = stream; setIsCameraActive(true); } } catch (error) { console.error("Error accessing camera:", error); } }; const capturePhoto = () => { if (videoRef.current) { const canvas = document.createElement("canvas"); canvas.width = videoRef.current.videoWidth; canvas.height = videoRef.current.videoHeight; canvas.getContext("2d")?.drawImage(videoRef.current, 0, 0); canvas.toBlob((blob) => { if (blob) { const file = new File([blob], "camera-photo.jpg", { type: "image/jpeg", }); onImageUpload(file); } }, "image/jpeg"); setIsCameraActive(false); } }; return (
{isCameraActive ? (
) : ( <>
event.preventDefault()} encType="multipart/form-data" >

Drag and drop an image here, or click to select a file

{ handleFileInput(event); sideEffect(null) }} className="hidden" ref={fileInputRef} />
)}
); }