"use client"; import { useState, useEffect, useRef } from "react"; import { motion } from "framer-motion"; interface Message { id: string; role: "user" | "assistant"; content: string; timestamp: number; } export default function VoiceChat() { const [input, setInput] = useState(""); const [messages, setMessages] = useState([ { id: "welcome", role: "assistant", content: "👁️ ¡Hola! Soy Horus. Chatea conmigo aquí o en Telegram. ¿En qué puedo ayudarte?", timestamp: Date.now(), } ]); const [isProcessing, setIsProcessing] = useState(false); const messagesEndRef = useRef(null); useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: "smooth" }); }, [messages]); const handleSend = async () => { if (!input.trim() || isProcessing) return; const userMessage: Message = { id: `user_${Date.now()}`, role: "user", content: input, timestamp: Date.now(), }; setMessages(prev => [...prev, userMessage]); setInput(""); setIsProcessing(true); try { // Send to SiteMente AI agent const res = await fetch("/api/chat/agent", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ message: userMessage.content, locale: "es" }), }); const data = await res.json(); const botMessage: Message = { id: `bot_${Date.now()}`, role: "assistant", content: data.response || "Entendido.", timestamp: Date.now(), }; setMessages(prev => [...prev, botMessage]); } catch (e) { console.error("Chat error:", e); setMessages(prev => [...prev, { id: `error_${Date.now()}`, role: "assistant", content: "Error de conexión. Prueba en Telegram.", timestamp: Date.now(), }]); } finally { setIsProcessing(false); } }; const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); handleSend(); } }; return (
{/* Header */}
💬 Horus Chat
{isProcessing && ( Escribiendo... )}
{/* Messages */}
{messages.map((msg) => (
{msg.content}
))}
{/* Input */}
setInput(e.target.value)} onKeyPress={handleKeyPress} placeholder="Escribe un mensaje..." className="flex-1 bg-white/5 border border-white/10 rounded-lg px-4 py-2 text-sm text-white placeholder:text-white/40 focus:outline-none focus:border-brand-pink" disabled={isProcessing} />

O chatea directamente en Telegram

); }