import { useState, useRef, useCallback } from 'react'; import { PhotoType } from '@/types/sale'; export function useCamera() { const [isOpen, setIsOpen] = useState(false); const [currentPhotoType, setCurrentPhotoType] = useState(null); const [isCapturing, setIsCapturing] = useState(false); const videoRef = useRef(null); const canvasRef = useRef(null); const streamRef = useRef(null); const startCamera = useCallback(async (photoType: PhotoType) => { try { const stream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' } }); streamRef.current = stream; setCurrentPhotoType(photoType); setIsOpen(true); if (videoRef.current) { videoRef.current.srcObject = stream; } return true; } catch (error) { console.error('Camera error:', error); throw new Error('Impossible d\'accéder à la caméra'); } }, []); const capturePhoto = useCallback((): Promise => { return new Promise((resolve, reject) => { if (!videoRef.current || !canvasRef.current) { reject(new Error('Video or canvas not available')); return; } setIsCapturing(true); const video = videoRef.current; const canvas = canvasRef.current; const context = canvas.getContext('2d'); if (!context) { reject(new Error('Canvas context not available')); return; } canvas.width = video.videoWidth; canvas.height = video.videoHeight; context.drawImage(video, 0, 0); canvas.toBlob( (blob) => { if (!blob) { reject(new Error('Failed to create image blob')); return; } const reader = new FileReader(); reader.onload = (e) => { setIsCapturing(false); resolve(e.target?.result as string); }; reader.onerror = () => { setIsCapturing(false); reject(new Error('Failed to read image')); }; reader.readAsDataURL(blob); }, 'image/jpeg', 0.8 ); }); }, []); const stopCamera = useCallback(() => { if (streamRef.current) { streamRef.current.getTracks().forEach(track => track.stop()); streamRef.current = null; } setIsOpen(false); setCurrentPhotoType(null); setIsCapturing(false); }, []); return { isOpen, currentPhotoType, isCapturing, videoRef, canvasRef, startCamera, capturePhoto, stopCamera }; }