import { useState, useRef, useEffect } from 'react'; import { Send, Sparkles, Bot, User, Check, X, RefreshCw, Lightbulb, Wand2 } from 'lucide-react'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; import { Badge } from '@/components/ui/badge'; import { ScrollArea } from '@/components/ui/scroll-area'; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from '@/components/ui/command'; import type { IAMessage, IASugerencia, CampoEstructura } from '@/types/materia'; import { cn } from '@/lib/utils'; //import { toast } from 'sonner'; interface IAMateriaTabProps { campos: CampoEstructura[]; datosGenerales: Record; messages: IAMessage[]; onSendMessage: (message: string, campoId?: string) => void; onAcceptSuggestion: (sugerencia: IASugerencia) => void; onRejectSuggestion: (messageId: string) => void; } const quickActions = [ { id: 'mejorar-objetivos', label: 'Mejorar objetivos', icon: Wand2, prompt: 'Mejora el :objetivo_general para que sea más específico y medible' }, { id: 'generar-contenido', label: 'Generar contenido temático', icon: Lightbulb, prompt: 'Sugiere un contenido temático completo basado en los objetivos y competencias' }, { id: 'alinear-perfil', label: 'Alinear con perfil de egreso', icon: RefreshCw, prompt: 'Revisa las :competencias y alinéalas con el perfil de egreso del plan' }, { id: 'ajustar-biblio', label: 'Recomendar bibliografía', icon: Sparkles, prompt: 'Recomienda bibliografía actualizada basándote en el contenido temático' }, ]; export function IAMateriaTab({ campos, datosGenerales, messages, onSendMessage, onAcceptSuggestion, onRejectSuggestion }: IAMateriaTabProps) { const [input, setInput] = useState(''); const [isLoading, setIsLoading] = useState(false); const [showFieldSelector, setShowFieldSelector] = useState(false); const [fieldSelectorPosition, setFieldSelectorPosition] = useState({ top: 0, left: 0 }); const [cursorPosition, setCursorPosition] = useState(0); const textareaRef = useRef(null); const scrollRef = useRef(null); useEffect(() => { if (scrollRef.current) { scrollRef.current.scrollTop = scrollRef.current.scrollHeight; } }, [messages]); const handleInputChange = (e: React.ChangeEvent) => { const value = e.target.value; const pos = e.target.selectionStart; setInput(value); setCursorPosition(pos); // Check for : character to trigger field selector const lastChar = value.charAt(pos - 1); if (lastChar === ':') { const rect = textareaRef.current?.getBoundingClientRect(); if (rect) { setFieldSelectorPosition({ top: rect.bottom + 8, left: rect.left }); setShowFieldSelector(true); } } else if (showFieldSelector && (lastChar === ' ' || !value.includes(':'))) { setShowFieldSelector(false); } }; const insertFieldMention = (campoId: string) => { const beforeCursor = input.slice(0, cursorPosition); const afterCursor = input.slice(cursorPosition); const lastColonIndex = beforeCursor.lastIndexOf(':'); const newInput = beforeCursor.slice(0, lastColonIndex) + `:${campoId}` + afterCursor; setInput(newInput); setShowFieldSelector(false); textareaRef.current?.focus(); }; const handleSend = async () => { if (!input.trim() || isLoading) return; // Extract field mention if any const fieldMatch = input.match(/:(\w+)/); const campoId = fieldMatch ? fieldMatch[1] : undefined; setIsLoading(true); onSendMessage(input, campoId); setInput(''); // Simulate AI response delay setTimeout(() => { setIsLoading(false); }, 1500); }; const handleQuickAction = (prompt: string) => { setInput(prompt); textareaRef.current?.focus(); }; const renderMessageContent = (content: string) => { // Render field mentions as styled badges return content.split(/(:[\w_]+)/g).map((part, i) => { if (part.startsWith(':')) { const campo = campos.find(c => c.id === part.slice(1)); return ( {campo?.nombre || part} ); } return part; }); }; return (

IA de la materia

Usa : para mencionar campos específicos

{/* Chat area */} Conversación
{messages.length === 0 ? (

Inicia una conversación para mejorar tu materia con IA

) : ( messages.map((message) => (
{message.role === 'assistant' && (
)}

{renderMessageContent(message.content)}

{message.sugerencia && !message.sugerencia.aceptada && (

Sugerencia para: {message.sugerencia.campoNombre}

{message.sugerencia.valorSugerido}
)} {message.sugerencia?.aceptada && ( Sugerencia aplicada )}
{message.role === 'user' && (
)}
)) )} {isLoading && (
)}
{/* Input area */}