"use client"; import { useState, useRef, useEffect } from "react"; import { lmsApi } from "@/lib/api"; import { Send, Bot, User, X, MessageSquare, Loader2 } from "lucide-react"; interface Message { role: 'tutor' | 'user'; content: string; } export default function AITutor({ lessonId }: { lessonId: string }) { const [messages, setMessages] = useState([ { role: 'tutor', content: '¡Hola! Soy tu tutor de IA. ¿Tienes alguna duda sobre esta lección?' } ]); const [input, setInput] = useState(""); const [isLoading, setIsLoading] = useState(false); const [isOpen, setIsOpen] = useState(false); const scrollRef = useRef(null); useEffect(() => { if (scrollRef.current) { scrollRef.current.scrollTop = scrollRef.current.scrollHeight; } }, [messages]); const handleSend = async () => { if (!input.trim() || isLoading) return; const userMessage = input.trim(); setInput(""); setMessages(prev => [...prev, { role: 'user', content: userMessage }]); setIsLoading(true); try { const { response } = await lmsApi.chatWithTutor(lessonId, userMessage); setMessages(prev => [...prev, { role: 'tutor', content: response }]); } catch (error) { console.error("Chat error:", error); setMessages(prev => [...prev, { role: 'tutor', content: "Lo siento, hubo un error conectando con el tutor. Por favor intenta de nuevo." }]); } finally { setIsLoading(false); } }; if (!isOpen) { return ( ); } return (
{/* Header */}

Tutor de IA

En Línea
{/* Messages */}
{messages.map((msg, i) => (
{msg.role === 'user' ? : }
{msg.content}
))} {isLoading && (
El tutor está pensando...
)}
{/* Input */}
setInput(e.target.value)} onKeyDown={(e) => e.key === 'Enter' && handleSend()} placeholder="Escribe tu duda aquí..." className="w-full bg-white/5 border border-white/10 rounded-xl py-3 px-4 pr-12 text-xs font-medium focus:outline-none focus:border-blue-500/50 transition-colors placeholder:text-gray-600" />

IA entrenada con el contenido de esta lección

); }