File size: 4,764 Bytes
af980d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useEffect, useRef, useState } from 'react';
import { Camera, CameraOff, RefreshCcw, CheckCircle2 } from 'lucide-react';
import styles from './CameraCapture.module.css';

export default function CameraCapture({ onCapture }) {
  const videoRef = useRef(null);
  const canvasRef = useRef(null);
  const streamRef = useRef(null);
  const [status, setStatus] = useState('idle'); // idle | requesting | ready | error
  const [error, setError] = useState('');

  useEffect(() => {
    return () => {
      if (streamRef.current) {
        streamRef.current.getTracks().forEach((track) => track.stop());
      }
    };
  }, []);

  useEffect(() => {
    const video = videoRef.current;
    const stream = streamRef.current;
    if (!video || !stream) return;

    let cancelled = false;

    const attachStream = async () => {
      try {
        video.srcObject = stream;
        await video.play();
      } catch (cameraError) {
        if (!cancelled) {
          setStatus('error');
          setError(cameraError?.message || 'Unable to render camera preview');
        }
      }
    };

    attachStream();

    return () => {
      cancelled = true;
    };
  }, [status]);

  const startCamera = async () => {
    setStatus('requesting');
    setError('');
    try {
      if (streamRef.current) {
        streamRef.current.getTracks().forEach((track) => track.stop());
      }

      const stream = await navigator.mediaDevices.getUserMedia({
        video: {
          facingMode: 'user',
          width: { ideal: 1280 },
          height: { ideal: 720 },
        },
        audio: false,
      });
      streamRef.current = stream;
      setStatus('ready');
    } catch (cameraError) {
      setStatus('error');
      setError(cameraError?.message || 'Camera access failed');
    }
  };

  const stopCamera = () => {
    if (streamRef.current) {
      streamRef.current.getTracks().forEach((track) => track.stop());
      streamRef.current = null;
    }
    if (videoRef.current) {
      videoRef.current.srcObject = null;
    }
    setStatus('idle');
  };

  const capturePhoto = async () => {
    if (!videoRef.current || !canvasRef.current) return;
    const video = videoRef.current;
    const canvas = canvasRef.current;
    canvas.width = video.videoWidth || 1280;
    canvas.height = video.videoHeight || 720;
    const context = canvas.getContext('2d');
    if (!context) return;
    context.drawImage(video, 0, 0, canvas.width, canvas.height);

    const blob = await new Promise((resolve) => canvas.toBlob(resolve, 'image/png', 0.95));
    if (!blob) return;
    const file = new File([blob], `uaide-capture-${Date.now()}.png`, { type: 'image/png' });
    onCapture(file);
  };

  return (
    <div className={styles.card}>
      <div className={styles.header}>
        <div className={styles.titleWrap}>
          <Camera size={16} />
          <span>Real-Time Capture</span>
        </div>
        {status === 'ready' && (
          <button type="button" className={styles.secondaryBtn} onClick={stopCamera}>
            <CameraOff size={14} />
            <span>Stop</span>
          </button>
        )}
      </div>

      <div className={styles.viewport}>
        <video
          ref={videoRef}
          playsInline
          muted
          autoPlay
          className={`${styles.video} ${status === 'ready' ? styles.videoVisible : styles.videoHidden}`}
        />
        {status !== 'ready' && (
          <div className={styles.placeholder}>
            <Camera size={26} />
            <p>Capture a live image for immediate forensic analysis.</p>
          </div>
        )}
        <canvas ref={canvasRef} className={styles.canvas} />
      </div>

      <div className={styles.actions}>
        {status === 'idle' && (
          <button type="button" className={styles.primaryBtn} onClick={startCamera}>
            <Camera size={15} />
            <span>Open Camera</span>
          </button>
        )}
        {status === 'requesting' && (
          <button type="button" className={styles.primaryBtn} disabled>
            <RefreshCcw size={15} className={styles.spin} />
            <span>Requesting Access…</span>
          </button>
        )}
        {status === 'ready' && (
          <button type="button" className={styles.primaryBtn} onClick={capturePhoto}>
            <CheckCircle2 size={15} />
            <span>Capture & Analyse</span>
          </button>
        )}
        {status === 'error' && (
          <>
            <p className={styles.error}>{error}</p>
            <button type="button" className={styles.primaryBtn} onClick={startCamera}>
              <RefreshCcw size={15} />
              <span>Retry Camera</span>
            </button>
          </>
        )}
      </div>
    </div>
  );
}