"use client"; import React, { useState, useEffect } from "react"; import { Mail, Plus, CheckCircle, RefreshCw, AlertCircle } from "lucide-react"; export default function GmailAgentUI() { const [accounts, setAccounts] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [newAccountName, setNewAccountName] = useState(""); const [authUrl, setAuthUrl] = useState(""); const [authCode, setAuthCode] = useState(""); const [authStep, setAuthStep] = useState(0); // 0 = list, 1 = get url, 2 = enter code const fetchAccounts = async () => { setLoading(true); try { const res = await fetch("/proxy/gmail/api/accounts"); const data = await res.json(); if (data.success) { setAccounts(data.accounts || []); } else { setError(data.error); } } catch (err) { setError("Failed to connect to Gmail Agent backend: " + err.message); } finally { setLoading(false); } }; useEffect(() => { fetchAccounts(); }, []); const handleGenerateUrl = async (e) => { e.preventDefault(); if (!newAccountName.trim()) return; try { const res = await fetch("/proxy/gmail/api/auth/url", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ accountName: newAccountName.trim() }) }); const data = await res.json(); if (data.success) { setAuthUrl(data.url); setAuthStep(2); } else { alert("Error generating URL: " + data.error); } } catch (err) { alert("Error: " + err.message); } }; const handleVerifyCode = async (e) => { e.preventDefault(); if (!authCode.trim()) return; try { const res = await fetch("/proxy/gmail/api/auth/token", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ accountName: newAccountName.trim(), code: authCode.trim() }) }); const data = await res.json(); if (data.success) { alert("Account added successfully!"); setAuthStep(0); setNewAccountName(""); setAuthCode(""); fetchAccounts(); } else { alert("Verification failed: " + data.error); } } catch (err) { alert("Error: " + err.message); } }; return (
No Gmail accounts connected yet.