/* global React, Icon */
const { useState, useRef, useEffect } = React;
function AIChat() {
const [open, setOpen] = useState(false);
const [messages, setMessages] = useState([
{ role: "assistant", content: "¡Hola! Soy el agente AI de Contarte. Puedo ayudarte con dudas de contabilidad, CFDI, SAT o sobre nuestros planes. ¿En qué te ayudo?" }
]);
const [input, setInput] = useState("");
const [busy, setBusy] = useState(false);
const scrollRef = useRef(null);
useEffect(() => {
if (scrollRef.current) scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
}, [messages, busy]);
const send = async (text) => {
const q = (text ?? input).trim();
if (!q || busy) return;
const next = [...messages, { role: "user", content: q }];
setMessages(next);
setInput("");
setBusy(true);
try {
const res = await fetch("/api/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
credentials: "same-origin",
body: JSON.stringify({ message: q })
});
if (!res.ok) throw new Error("HTTP " + res.status);
const data = await res.json();
const reply = (data && data.reply ? String(data.reply) : "").trim();
if (!reply) throw new Error("empty reply");
setMessages([...next, { role: "assistant", content: reply }]);
} catch (e) {
setMessages([...next, { role: "assistant", content: "Ups, tuve un problema al responder. Intenta de nuevo en un momento." }]);
} finally {
setBusy(false);
}
};
const suggestions = [
"¿Qué plan me conviene?",
"¿Qué es CFDI 4.0?",
"¿Cómo conecto el SAT?"
];
return (
<>
{/* CTA section — lives between Trust and Pricing */}
Resuelve dudas de CFDI, SAT, cobros o planes en segundos. 100% gratis, sin registro — responde en español, al instante.Chatea con nuestro agente AI sobre contarte.mx o tu contabilidad.