"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(); // Check for OAuth redirect const params = new URLSearchParams(window.location.search); const codeParam = params.get('code'); const stateParam = params.get('state'); if (codeParam && stateParam) { setAuthStep(2); setAuthCode(codeParam); setNewAccountName(stateParam); // Auto-submit the token verification const verifyRedirect = async () => { try { const res = await fetch("/proxy/gmail/api/auth/token", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ accountName: stateParam.trim(), code: codeParam.trim() }) }); const data = await res.json(); if (data.success) { alert(`Account '${stateParam}' added successfully!`); setAuthStep(0); setNewAccountName(""); setAuthCode(""); window.history.replaceState({}, document.title, window.location.pathname); fetchAccounts(); } else { alert("OAuth verification failed: " + data.error); } } catch (err) { alert("Error verifying OAuth: " + err.message); } }; verifyRedirect(); } }, []); 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 (
{authStep === 0 && ( <>

Connected Gmail Accounts

{error && (
{error}
)}
{loading ? (
Loading...
) : accounts.length === 0 ? (

No Gmail accounts connected yet.

) : ( accounts.map((acc, idx) => (

{acc.name}

Active & Syncing
)) )}
)} {authStep === 1 && (

Connect New Gmail

setNewAccountName(e.target.value)} required style={{ width: "100%", padding: "12px", borderRadius: "8px", border: "1px solid rgba(255,255,255,0.1)", background: "rgba(0,0,0,0.2)", color: "white" }} placeholder="Enter account label..." />
)} {authStep === 2 && (

Step 2: Authenticate

  1. Click the link below to open Google's secure login page.
  2. Sign in with your Gmail account and allow the requested permissions.
  3. If you are not automatically redirected back, copy the verification code provided by Google and paste it below.
{authUrl}
setAuthCode(e.target.value)} required style={{ width: "100%", padding: "12px", borderRadius: "8px", border: "1px solid rgba(255,255,255,0.1)", background: "rgba(0,0,0,0.2)", color: "white" }} placeholder="Paste code here..." />
)}
); }