import { useState, useEffect, useRef } from 'react'; import { rateLimitCooldownUntil, uploadScanCapture } from '../lib/scan-capture-upload.js'; import { detectCardShapesFromFrame, mergeDetectedShapesIntoTrackedCards, } from '../lib/scanner-card-detection.js'; import { buildScannedCardPayload, getScanAuthHeaders, identifyTrackedCardCapture, resolveDisambiguationRefineAction, submitScanForReview, VISION_RATE_LIMIT_MS, fetchVisionIdentify, } from '../lib/scanner-card-identify.js'; import ScanDisambiguationDialog from './ScanDisambiguationDialog.js'; export default function CameraScanner({ onCardScanned, onError }) { const [isStreaming, setIsStreaming] = useState(false); const [isDetecting, setIsDetecting] = useState(false); const [disambiguation, setDisambiguation] = useState(null); const [scanNotice, setScanNotice] = useState(null); const [submittingReview, setSubmittingReview] = useState(false); const videoRef = useRef(null); const canvasRef = useRef(null); const detectionCanvasRef = useRef(null); const streamRef = useRef(null); const detectionIntervalRef = useRef(null); const trackingIntervalRef = useRef(null); // Card tracking state const [trackedCards, setTrackedCards] = useState([]); // Array of tracked card objects const trackedCardsRef = useRef([]); const nextCardIdRef = useRef(1); const visionCooldownUntilRef = useRef(0); const activeVerificationRef = useRef(0); const lastErrorAtRef = useRef(0); const disambiguationRefineRef = useRef(null); const [videoMetrics, setVideoMetrics] = useState({ width: 0, height: 0 }); // Configure canvas contexts for optimal performance useEffect(() => { if (canvasRef.current) { const ctx = canvasRef.current.getContext('2d', { willReadFrequently: true }); } if (detectionCanvasRef.current) { const ctx = detectionCanvasRef.current.getContext('2d', { willReadFrequently: true }); } }, []); // Continuous shape detection for card-like rectangles const detectCardShapes = () => { if (!videoRef.current || !detectionCanvasRef.current || !isStreaming) return []; return detectCardShapesFromFrame(videoRef.current, detectionCanvasRef.current); }; const updateTrackedCards = (detectedShapes) => { const { cards, nextCardId } = mergeDetectedShapesIntoTrackedCards( trackedCardsRef.current, detectedShapes, nextCardIdRef.current ); nextCardIdRef.current = nextCardId; trackedCardsRef.current = cards; setTrackedCards(cards); }; const emitScannedCard = async (cardTracker, imageData, finalCard, ocrMeta = {}) => { cardTracker.status = 'scanned'; const originalTitle = document.title; document.title = `📸 ${finalCard.name} - Card Scanner`; setTimeout(() => { document.title = originalTitle; }, 3000); let scanImageUrl = null; if (imageData) { try { scanImageUrl = await uploadScanCapture(imageData); } catch (uploadError) { console.warn('Scan image upload failed:', uploadError); } } onCardScanned(buildScannedCardPayload(finalCard, { imageData, scanImageUrl, ocrMeta })); }; const handleDisambiguationPick = async (candidate) => { if (!disambiguation) return; const { cardTracker, imageData, ocrMeta } = disambiguation; await emitScannedCard(cardTracker, imageData, candidate, ocrMeta); setDisambiguation(null); disambiguationRefineRef.current = null; }; const showScanNotice = (message) => { setScanNotice(message); setTimeout(() => setScanNotice(null), 8000); }; const handleReviewSubmitted = (cardTracker, message) => { if (cardTracker) cardTracker.status = 'confirmed'; setDisambiguation(null); disambiguationRefineRef.current = null; showScanNotice(message); }; const handleNotInCatalog = async () => { if (!disambiguation || submittingReview) return; setSubmittingReview(true); const { cardTracker, imageData, candidates, ocrMeta } = disambiguation; const guessedName = ocrMeta?.cardName || ocrMeta?.query || candidates?.[0]?.name || null; try { const result = await submitScanForReview({ imageData, name: guessedName, candidateCardIds: (candidates || []).map((c) => c.id), authHeaders: getScanAuthHeaders(), }); if (result.rateLimited) { visionCooldownUntilRef.current = rateLimitCooldownUntil(VISION_RATE_LIMIT_MS); reportScannerError('Too many scan attempts. Please wait a moment and try again.'); return; } handleReviewSubmitted(cardTracker, result.message); } catch (error) { reportScannerError(error.message || 'Failed to submit scan for review'); } finally { setSubmittingReview(false); } }; const applyIdentifyOutcome = async (cardTracker, imageData, outcome) => { cardTracker.status = outcome.cardStatus; switch (outcome.type) { case 'emit': await emitScannedCard(cardTracker, imageData, outcome.card, outcome.ocrMeta); break; case 'disambiguation': setDisambiguation({ cardTracker, imageData, candidates: outcome.matches, ocrMeta: outcome.ocrMeta, message: outcome.message, }); break; case 'notice': showScanNotice(outcome.message); break; case 'error': reportScannerError(outcome.message); break; default: break; } }; const reportScannerError = (message) => { if (disambiguation) return; const now = Date.now(); if (now - lastErrorAtRef.current < 4000) return; lastErrorAtRef.current = now; onError?.(message); }; useEffect(() => { if (!disambiguation?.imageData) return; if (Date.now() < visionCooldownUntilRef.current) return; const refineKey = disambiguation.cardTracker?.id ?? 'modal'; if (disambiguationRefineRef.current === refineKey) return; disambiguationRefineRef.current = refineKey; let cancelled = false; (async () => { try { const authHeaders = getScanAuthHeaders(); const vision = await fetchVisionIdentify(disambiguation.imageData, authHeaders); if (cancelled) return; if (vision.rateLimited) { visionCooldownUntilRef.current = Date.now() + VISION_RATE_LIMIT_MS; return; } const action = resolveDisambiguationRefineAction(vision.result, { candidates: disambiguation.candidates, }); if (cancelled) return; switch (action.type) { case 'review_submitted': handleReviewSubmitted(disambiguation.cardTracker, action.message); break; case 'emit': await emitScannedCard( disambiguation.cardTracker, disambiguation.imageData, action.card, action.ocrMeta ); setDisambiguation(null); disambiguationRefineRef.current = null; break; case 'pick': await handleDisambiguationPick(action.candidate); break; case 'catalog_gap': try { const submitResult = await submitScanForReview({ imageData: disambiguation.imageData, ...action.submitPayload, authHeaders, }); if (submitResult.ok) { handleReviewSubmitted(disambiguation.cardTracker, submitResult.message); } } catch { setDisambiguation((current) => current ? { ...current, visionHint: action.setHint, message: action.hintMessage, } : current ); } break; case 'narrow': setDisambiguation((current) => current ? { ...current, candidates: action.filtered, message: action.message, visionHint: action.visionHint, } : current ); break; default: break; } } catch (error) { console.warn('Disambiguation vision refine failed:', error); } })(); return () => { cancelled = true; }; // eslint-disable-next-line react-hooks/exhaustive-deps -- refine runs per disambiguation session, not per handler identity }, [disambiguation?.cardTracker?.id, disambiguation?.imageData]); const verifyCardShape = async (cardTracker) => { if (!videoRef.current || !canvasRef.current || cardTracker.status !== 'detecting') return; if (disambiguation) return; if (activeVerificationRef.current >= 1) return; activeVerificationRef.current += 1; cardTracker.status = 'verifying'; try { cardTracker.scanAttempts++; const identification = await identifyTrackedCardCapture({ video: videoRef.current, canvas: canvasRef.current, cardTracker, authHeaders: getScanAuthHeaders(), visionCooldownUntilMs: visionCooldownUntilRef.current, }); if (identification.retry) { cardTracker.status = 'detecting'; return; } if (identification.rateLimited) { visionCooldownUntilRef.current = Date.now() + VISION_RATE_LIMIT_MS; reportScannerError('Too many scan attempts. Please wait a moment and try again.'); cardTracker.status = 'negative'; return; } if (identification.handled && identification.outcome) { cardTracker.status = 'confirmed'; await applyIdentifyOutcome(cardTracker, identification.imageData, identification.outcome); } } catch (error) { console.error(`Error verifying card ${cardTracker.id}:`, error); cardTracker.status = 'negative'; reportScannerError(error.message || 'Scan failed'); } finally { activeVerificationRef.current = Math.max(0, activeVerificationRef.current - 1); } }; // Start continuous detection const startDetection = () => { if (detectionIntervalRef.current || !isStreaming) return; console.log('🎯 Starting continuous card detection...'); setIsDetecting(true); // Shape detection every 200ms detectionIntervalRef.current = setInterval(() => { const shapes = detectCardShapes(); updateTrackedCards(shapes); }, 200); // Card verification every 1 second trackingIntervalRef.current = setInterval(() => { const cardsToVerify = trackedCardsRef.current.filter(card => card.status === 'detecting' && card.stableCount >= 6 && card.scanAttempts < 1 && Date.now() - card.firstSeen > 2500 ); const cardsToProcess = cardsToVerify.slice(0, 1); cardsToProcess.forEach(card => { verifyCardShape(card); }); }, 1000); // Back to 1 second intervals }; // Stop detection const stopDetection = () => { console.log('🛑 Stopping card detection...'); setIsDetecting(false); if (detectionIntervalRef.current) { clearInterval(detectionIntervalRef.current); detectionIntervalRef.current = null; } if (trackingIntervalRef.current) { clearInterval(trackingIntervalRef.current); trackingIntervalRef.current = null; } // Clear tracked cards trackedCardsRef.current = []; setTrackedCards([]); }; // Start camera stream const startCamera = async () => { try { console.log('🎥 Starting camera...'); const stream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment', width: { ideal: 1280 }, height: { ideal: 720 }, aspectRatio: { ideal: 16/9 } } }); console.log('📹 Camera stream obtained:', stream); if (videoRef.current) { videoRef.current.srcObject = stream; streamRef.current = stream; videoRef.current.onloadedmetadata = () => { console.log('📺 Video metadata loaded, attempting to play...'); videoRef.current?.play().then(() => { console.log('▶️ Video playback started successfully'); setIsStreaming(true); }).catch((err) => { console.error('❌ Video playback failed:', err); onError(`Video playback failed: ${err.message}`); }); }; videoRef.current.onerror = (err) => { console.error('❌ Video element error:', err); onError('Video element error occurred'); }; // Add a fallback timeout setTimeout(() => { if (!isStreaming && videoRef.current && videoRef.current.readyState >= 2) { console.log('🔄 Fallback: Attempting to play video directly...'); videoRef.current.play().then(() => { console.log('▶️ Fallback video playback started'); setIsStreaming(true); }).catch(console.error); } }, 2000); } else { console.error('❌ Video element not available'); onError('Video element not available'); } } catch (err) { console.error('❌ Camera access error:', err); onError(`Unable to access camera: ${err.message}`); } }; // Stop camera stream const stopCamera = () => { setIsStreaming(false); stopDetection(); if (streamRef.current) { streamRef.current.getTracks().forEach(track => track.stop()); streamRef.current = null; } if (videoRef.current) { videoRef.current.srcObject = null; } }; useEffect(() => { const video = videoRef.current; if (!video || !isStreaming) return undefined; const syncVideoMetrics = () => { setVideoMetrics({ width: video.videoWidth || 0, height: video.videoHeight || 0, }); }; video.addEventListener('loadedmetadata', syncVideoMetrics); video.addEventListener('resize', syncVideoMetrics); syncVideoMetrics(); return () => { video.removeEventListener('loadedmetadata', syncVideoMetrics); video.removeEventListener('resize', syncVideoMetrics); }; }, [isStreaming]); // Auto-start detection when camera starts useEffect(() => { if (isStreaming && !isDetecting) { // Small delay to let camera stabilize const timerId = setTimeout(() => { startDetection(); }, 1000); return () => clearTimeout(timerId); } return undefined; // eslint-disable-next-line react-hooks/exhaustive-deps -- intentional: start detection once per stream session }, [isStreaming]); // Cleanup on unmount useEffect(() => { return () => { stopCamera(); }; // eslint-disable-next-line react-hooks/exhaustive-deps -- run stopCamera only on unmount }, []); return (
{scanNotice && (
{scanNotice}
)} {/* Camera Feed Container */}
{/* Video Element - Always rendered but visibility controlled */}
{/* Hidden canvases for image processing */} {/* Detection Info Panel - Only show when streaming */} {isStreaming && (
🎯

Smart Detection Active

Shape recognition
Server identification
Position tracking
Database lookup
)} { setDisambiguation(null); disambiguationRefineRef.current = null; }} />
); }