diff --git a/src/components/CameraCapture.tsx b/src/components/CameraCapture.tsx index 164ce87..88160e2 100644 --- a/src/components/CameraCapture.tsx +++ b/src/components/CameraCapture.tsx @@ -1,7 +1,7 @@ 'use client'; import React, { useRef, useState } from 'react'; -import { Camera, Upload, CheckCircle2, AlertCircle, Sparkles, ExternalLink, ChevronRight } from 'lucide-react'; +import { Camera, Upload, CheckCircle2, AlertCircle, Sparkles, ExternalLink, ChevronRight, Search, Loader2 } from 'lucide-react'; import { createClientComponentClient } from '@supabase/auth-helpers-nextjs'; import { useRouter, useSearchParams } from 'next/navigation'; import { analyzeBottle } from '@/services/analyze-bottle'; @@ -11,6 +11,8 @@ import { savePendingBottle } from '@/lib/offline-db'; import { v4 as uuidv4 } from 'uuid'; import { findMatchingBottle } from '@/services/find-matching-bottle'; import { validateSession } from '@/services/validate-session'; +import { discoverWhiskybaseId } from '@/services/discover-whiskybase'; +import { updateBottle } from '@/services/update-bottle'; import Link from 'next/link'; interface CameraCaptureProps { @@ -47,6 +49,8 @@ export default function CameraCapture({ onImageCaptured, onAnalysisComplete, onS const [isQueued, setIsQueued] = useState(false); const [matchingBottle, setMatchingBottle] = useState<{ id: string; name: string } | null>(null); const [lastSavedId, setLastSavedId] = useState(null); + const [wbDiscovery, setWbDiscovery] = useState<{ id: string; url: string; title: string } | null>(null); + const [isDiscovering, setIsDiscovering] = useState(false); const handleCapture = async (event: React.ChangeEvent) => { const file = event.target.files?.[0]; @@ -153,6 +157,33 @@ export default function CameraCapture({ onImageCaptured, onAnalysisComplete, onS } }; + const handleDiscoverWb = async () => { + if (!lastSavedId || !analysisResult) return; + setIsDiscovering(true); + const result = await discoverWhiskybaseId({ + name: analysisResult.name || '', + distillery: analysisResult.distillery || undefined, + abv: analysisResult.abv || undefined, + age: analysisResult.age || undefined + }); + + if (result.success && result.id) { + setWbDiscovery({ id: result.id, url: result.url!, title: result.title! }); + } + setIsDiscovering(false); + }; + + const handleLinkWb = async () => { + if (!lastSavedId || !wbDiscovery) return; + const res = await updateBottle(lastSavedId, { + whiskybase_id: wbDiscovery.id + }); + if (res.success) { + setWbDiscovery(null); + // Show some success feedback if needed, but the button will disappear anyway + } + }; + const compressImage = (file: File): Promise => { return new Promise((resolve, reject) => { const reader = new FileReader(); @@ -245,6 +276,50 @@ export default function CameraCapture({ onImageCaptured, onAnalysisComplete, onS + {!wbDiscovery && !isDiscovering && ( + + )} + + {isDiscovering && ( +
+ + Suche auf Whiskybase... +
+ )} + + {wbDiscovery && ( +
+
+ Treffer gefunden +
+

+ {wbDiscovery.title} +

+
+ + + Prüfen + +
+
+ )} + + setFormData({ ...formData, whiskybase_id: e.target.value })} className="w-full px-4 py-2 bg-zinc-50 dark:bg-zinc-800 border border-zinc-200 dark:border-zinc-700 rounded-xl outline-none focus:ring-2 focus:ring-amber-500" /> + {discoveryResult && ( +
+

Treffer gefunden:

+

{discoveryResult.title}

+
+ + + Prüfen + +
+
+ )}