import { useCallback, useRef, useState } from "react"; import { UploadCloud, Loader2, Check } from "lucide-react"; import { supabase } from "@/integrations/supabase/client"; import { toast } from "sonner"; export interface ExtractedData { total_phone_hours: number | null; notifications: number | null; social_hours: number | null; top_apps: { name: string; hours: number; category: string }[] | null; } interface Props { onExtracted: (data: ExtractedData) => void; uploadCount: number; } export default function ScreenshotUploader({ onExtracted, uploadCount }: Props) { const [busy, setBusy] = useState(false); const [dragOver, setDragOver] = useState(false); const [justAdded, setJustAdded] = useState(false); const inputRef = useRef(null); const handleFile = useCallback(async (file: File) => { if (!file.type.startsWith("image/")) { toast.error("Please upload an image (PNG/JPEG)."); return; } if (file.size > 10 * 1024 * 1024) { toast.error("Image too large. Max 10MB."); return; } const dataUrl = await new Promise((resolve, reject) => { const r = new FileReader(); r.onload = () => resolve(r.result as string); r.onerror = reject; r.readAsDataURL(file); }); setBusy(true); try { const { data, error } = await supabase.functions.invoke("extract-screenshot", { body: { imageDataUrl: dataUrl }, }); if (error) throw error; if ((data as any)?.error) throw new Error((data as any).error); onExtracted(data as ExtractedData); setJustAdded(true); setTimeout(() => setJustAdded(false), 1800); toast.success("Screenshot decoded."); } catch (e: any) { console.error(e); toast.error(e?.message ?? "Could not extract data. Try a clearer screenshot."); } finally { setBusy(false); if (inputRef.current) inputRef.current.value = ""; } }, [onExtracted]); return (
{uploadCount > 0 && !busy && (

{uploadCount} screenshot{uploadCount > 1 ? "s" : ""} processed

)}
); }