Spaces:
Sleeping
Sleeping
File size: 7,817 Bytes
4000a4c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
'use client';
import { useState, useRef, useEffect } from 'react';
import { X, Camera, RotateCcw, Check } from 'lucide-react';
import clsx from 'clsx';
interface CameraModalProps {
isOpen: boolean;
onClose: () => void;
onCapture: (file: File) => void;
}
export default function CameraModal({ isOpen, onClose, onCapture }: CameraModalProps) {
const videoRef = useRef<HTMLVideoElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
const [stream, setStream] = useState<MediaStream | null>(null);
const [capturedImage, setCapturedImage] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
const [facingMode, setFacingMode] = useState<'user' | 'environment'>('environment');
useEffect(() => {
if (isOpen) {
startCamera();
} else {
stopCamera();
setCapturedImage(null);
}
return () => stopCamera();
}, [isOpen, facingMode]);
const startCamera = async () => {
try {
setError(null);
const mediaStream = await navigator.mediaDevices.getUserMedia({
video: {
facingMode: facingMode,
width: { ideal: 1280 },
height: { ideal: 720 }
},
audio: false
});
setStream(mediaStream);
if (videoRef.current) {
videoRef.current.srcObject = mediaStream;
}
} catch (err) {
console.error('Camera error:', err);
setError('Unable to access camera. Please check permissions.');
}
};
const stopCamera = () => {
if (stream) {
stream.getTracks().forEach(track => track.stop());
setStream(null);
}
};
const switchCamera = () => {
setFacingMode(prev => prev === 'user' ? 'environment' : 'user');
};
const capturePhoto = () => {
if (videoRef.current && canvasRef.current) {
const video = videoRef.current;
const canvas = canvasRef.current;
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
const ctx = canvas.getContext('2d');
if (ctx) {
ctx.drawImage(video, 0, 0);
const imageDataUrl = canvas.toDataURL('image/jpeg', 0.9);
setCapturedImage(imageDataUrl);
stopCamera();
}
}
};
const retake = () => {
setCapturedImage(null);
startCamera();
};
const confirmCapture = () => {
if (capturedImage && canvasRef.current) {
canvasRef.current.toBlob((blob) => {
if (blob) {
const file = new File([blob], `photo_${Date.now()}.jpg`, { type: 'image/jpeg' });
onCapture(file);
onClose();
}
}, 'image/jpeg', 0.9);
}
};
if (!isOpen) return null;
return (
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/90 backdrop-blur-sm animate-fade-in-up">
<div className="w-full max-w-xl bg-neutral-900 rounded-2xl shadow-2xl border border-neutral-800 overflow-hidden">
{/* Header */}
<div className="flex items-center justify-between p-4 border-b border-neutral-800">
<div className="flex items-center gap-2">
<Camera size={20} className="text-sky-500" />
<h2 className="text-lg font-semibold text-white">Take Photo</h2>
</div>
<button
onClick={onClose}
className="p-2 hover:bg-neutral-800 rounded-full"
>
<X size={20} className="text-gray-400" />
</button>
</div>
{/* Camera View */}
<div className="relative aspect-[4/3] bg-black">
{error ? (
<div className="absolute inset-0 flex items-center justify-center text-center p-6">
<div>
<Camera size={48} className="mx-auto mb-4 text-gray-600" />
<p className="text-gray-400">{error}</p>
</div>
</div>
) : capturedImage ? (
<img
src={capturedImage}
alt="Captured"
className="w-full h-full object-contain"
/>
) : (
<video
ref={videoRef}
autoPlay
playsInline
muted
className="w-full h-full object-cover"
/>
)}
<canvas ref={canvasRef} className="hidden" />
</div>
{/* Controls */}
<div className="p-4 flex items-center justify-center gap-4">
{!capturedImage ? (
<>
{/* Switch Camera */}
<button
onClick={switchCamera}
className="p-3 bg-neutral-800 hover:bg-neutral-700 rounded-full transition-colors"
title="Switch Camera"
>
<RotateCcw size={20} className="text-white" />
</button>
{/* Capture Button */}
<button
onClick={capturePhoto}
disabled={!stream}
className={clsx(
"w-16 h-16 rounded-full border-4 border-white flex items-center justify-center transition-all",
stream
? "bg-white hover:bg-gray-200 active:scale-95"
: "bg-gray-600 border-gray-600 cursor-not-allowed"
)}
>
<div className="w-12 h-12 rounded-full bg-sky-500"></div>
</button>
{/* Placeholder for symmetry */}
<div className="w-11"></div>
</>
) : (
<>
{/* Retake */}
<button
onClick={retake}
className="flex-1 py-3 bg-neutral-800 text-white rounded-xl font-medium hover:bg-neutral-700 flex items-center justify-center gap-2"
>
<RotateCcw size={18} />
Retake
</button>
{/* Confirm */}
<button
onClick={confirmCapture}
className="flex-1 py-3 bg-sky-500 text-white rounded-xl font-medium hover:bg-sky-600 flex items-center justify-center gap-2"
>
<Check size={18} />
Use Photo
</button>
</>
)}
</div>
</div>
</div>
);
}
|