@@ -1,357 +1,406 @@
|
|||||||
import { useState, useRef, useEffect } from 'react';
|
import { useRouterState } from '@tanstack/react-router'
|
||||||
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 {
|
import {
|
||||||
Command,
|
Sparkles,
|
||||||
CommandEmpty,
|
Send,
|
||||||
CommandGroup,
|
Target,
|
||||||
CommandInput,
|
UserCheck,
|
||||||
CommandItem,
|
Lightbulb,
|
||||||
CommandList,
|
FileText,
|
||||||
} from '@/components/ui/command';
|
GraduationCap,
|
||||||
import type { IAMessage, IASugerencia, CampoEstructura } from '@/types/materia';
|
BookOpen,
|
||||||
import { cn } from '@/lib/utils';
|
Check,
|
||||||
//import { toast } from 'sonner';
|
X,
|
||||||
|
} from 'lucide-react'
|
||||||
|
import { useState, useEffect, useRef, useMemo } from 'react'
|
||||||
|
|
||||||
interface IAMateriaTabProps {
|
import type { IAMessage, IASugerencia, CampoEstructura } from '@/types/materia'
|
||||||
campos: CampoEstructura[];
|
|
||||||
datosGenerales: Record<string, any>;
|
import { Avatar, AvatarFallback } from '@/components/ui/avatar'
|
||||||
messages: IAMessage[];
|
import { Badge } from '@/components/ui/badge'
|
||||||
onSendMessage: (message: string, campoId?: string) => void;
|
import { Button } from '@/components/ui/button'
|
||||||
onAcceptSuggestion: (sugerencia: IASugerencia) => void;
|
import { ScrollArea } from '@/components/ui/scroll-area'
|
||||||
onRejectSuggestion: (messageId: string) => void;
|
import { Textarea } from '@/components/ui/textarea'
|
||||||
|
import { cn } from '@/lib/utils'
|
||||||
|
|
||||||
|
// Tipos importados de tu archivo de materia
|
||||||
|
|
||||||
|
const PRESETS = [
|
||||||
|
{
|
||||||
|
id: 'mejorar-objetivo',
|
||||||
|
label: 'Mejorar objetivo',
|
||||||
|
icon: Target,
|
||||||
|
prompt: 'Mejora la redacción del objetivo de esta asignatura...',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'contenido-tematico',
|
||||||
|
label: 'Sugerir contenido',
|
||||||
|
icon: BookOpen,
|
||||||
|
prompt: 'Genera un desglose de temas para esta materia...',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'actividades',
|
||||||
|
label: 'Actividades de aprendizaje',
|
||||||
|
icon: GraduationCap,
|
||||||
|
prompt: 'Sugiere actividades prácticas para los temas seleccionados...',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'bibliografia',
|
||||||
|
label: 'Actualizar bibliografía',
|
||||||
|
icon: FileText,
|
||||||
|
prompt: 'Recomienda bibliografía reciente para esta asignatura...',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
interface SelectedField {
|
||||||
|
key: string
|
||||||
|
label: string
|
||||||
|
value: string
|
||||||
}
|
}
|
||||||
|
|
||||||
const quickActions = [
|
interface IAMateriaTabProps {
|
||||||
{ id: 'mejorar-objetivos', label: 'Mejorar objetivos', icon: Wand2, prompt: 'Mejora el :objetivo_general para que sea más específico y medible' },
|
campos: Array<CampoEstructura>
|
||||||
{ id: 'generar-contenido', label: 'Generar contenido temático', icon: Lightbulb, prompt: 'Sugiere un contenido temático completo basado en los objetivos y competencias' },
|
datosGenerales: Record<string, any>
|
||||||
{ 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' },
|
messages: Array<IAMessage>
|
||||||
{ id: 'ajustar-biblio', label: 'Recomendar bibliografía', icon: Sparkles, prompt: 'Recomienda bibliografía actualizada basándote en el contenido temático' },
|
onSendMessage: (message: string, campoId?: string) => void
|
||||||
];
|
onAcceptSuggestion: (sugerencia: IASugerencia) => void
|
||||||
|
onRejectSuggestion: (messageId: string) => void
|
||||||
|
}
|
||||||
|
|
||||||
export function IAMateriaTab({ campos, datosGenerales, messages, onSendMessage, onAcceptSuggestion, onRejectSuggestion }: IAMateriaTabProps) {
|
export function IAMateriaTab({
|
||||||
const [input, setInput] = useState('');
|
campos,
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
datosGenerales,
|
||||||
const [showFieldSelector, setShowFieldSelector] = useState(false);
|
messages,
|
||||||
const [fieldSelectorPosition, setFieldSelectorPosition] = useState({ top: 0, left: 0 });
|
onSendMessage,
|
||||||
const [cursorPosition, setCursorPosition] = useState(0);
|
onAcceptSuggestion,
|
||||||
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
onRejectSuggestion,
|
||||||
const scrollRef = useRef<HTMLDivElement>(null);
|
}: IAMateriaTabProps) {
|
||||||
|
const routerState = useRouterState()
|
||||||
|
|
||||||
|
// ESTADOS PRINCIPALES (Igual que en Planes)
|
||||||
|
const [input, setInput] = useState('')
|
||||||
|
const [selectedFields, setSelectedFields] = useState<Array<SelectedField>>([])
|
||||||
|
const [showSuggestions, setShowSuggestions] = useState(false)
|
||||||
|
const [isLoading, setIsLoading] = useState(false)
|
||||||
|
const scrollRef = useRef<HTMLDivElement>(null)
|
||||||
|
|
||||||
|
// 1. Transformar datos de la materia para el menú
|
||||||
|
const availableFields = useMemo(() => {
|
||||||
|
// Extraemos las claves directamente del objeto datosGenerales
|
||||||
|
// ["nombre", "descripcion", "perfil_de_egreso", "fines_de_aprendizaje_o_formacion"]
|
||||||
|
return Object.keys(datosGenerales).map((key) => {
|
||||||
|
// Buscamos si existe un nombre amigable en la estructura de campos
|
||||||
|
const estructuraCampo = campos.find((c) => c.id === key)
|
||||||
|
|
||||||
|
// Si existe en 'campos', usamos su nombre; si no, formateamos la clave (ej: perfil_de_egreso -> Perfil De Egreso)
|
||||||
|
const labelAmigable =
|
||||||
|
estructuraCampo?.nombre ||
|
||||||
|
key.replace(/_/g, ' ').replace(/\b\w/g, (l) => l.toUpperCase())
|
||||||
|
|
||||||
|
return {
|
||||||
|
key: key,
|
||||||
|
label: labelAmigable,
|
||||||
|
value: String(datosGenerales[key] || ''),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}, [campos, datosGenerales])
|
||||||
|
|
||||||
|
// 2. Manejar el estado inicial si viene de "Datos de Materia" (Prefill)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const state = routerState.location.state as any
|
||||||
|
|
||||||
|
if (state?.prefillCampo && availableFields.length > 0) {
|
||||||
|
const field = availableFields.find((f) => f.key === state.prefillCampo)
|
||||||
|
|
||||||
|
if (field && !selectedFields.find((sf) => sf.key === field.key)) {
|
||||||
|
setSelectedFields([field])
|
||||||
|
// Sincronizamos el texto inicial con el campo pre-seleccionado
|
||||||
|
setInput(`Mejora el campo ${field.key}: `)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [availableFields])
|
||||||
|
|
||||||
|
// Scroll automático
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (scrollRef.current) {
|
if (scrollRef.current) {
|
||||||
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
|
scrollRef.current.scrollTop = scrollRef.current.scrollHeight
|
||||||
}
|
}
|
||||||
}, [messages]);
|
}, [messages, isLoading])
|
||||||
|
|
||||||
|
// 3. Lógica para el disparador ":"
|
||||||
const handleInputChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
const handleInputChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
||||||
const value = e.target.value;
|
const val = e.target.value
|
||||||
const pos = e.target.selectionStart;
|
setInput(val)
|
||||||
setInput(value);
|
setShowSuggestions(val.endsWith(':'))
|
||||||
setCursorPosition(pos);
|
}
|
||||||
|
|
||||||
// Check for : character to trigger field selector
|
const toggleField = (field: SelectedField) => {
|
||||||
const lastChar = value.charAt(pos - 1);
|
setSelectedFields((prev) => {
|
||||||
if (lastChar === ':') {
|
const isSelected = prev.find((f) => f.key === field.key)
|
||||||
const rect = textareaRef.current?.getBoundingClientRect();
|
|
||||||
if (rect) {
|
// Si lo estamos seleccionando (no estaba antes)
|
||||||
setFieldSelectorPosition({ top: rect.bottom + 8, left: rect.left });
|
if (!isSelected) {
|
||||||
setShowFieldSelector(true);
|
// Actualizamos el texto del input:
|
||||||
|
// Si termina en ":", lo reemplazamos por el key para que sea "Mejora perfil_de_egreso "
|
||||||
|
// Si no, simplemente lo añadimos al final.
|
||||||
|
setInput((prevText) => {
|
||||||
|
const [beforeColon, afterColon = ''] = prevText.split(':')
|
||||||
|
|
||||||
|
// Campos ya escritos después de :
|
||||||
|
const existingKeys = afterColon
|
||||||
|
.split(',')
|
||||||
|
.map((k) => k.trim())
|
||||||
|
.filter(Boolean)
|
||||||
|
|
||||||
|
// Si ya existe, no lo volvemos a agregar
|
||||||
|
if (existingKeys.includes(field.key)) {
|
||||||
|
return prevText
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatedKeys = [...existingKeys, field.key].join(', ')
|
||||||
|
|
||||||
|
return `${beforeColon.trim()}: ${updatedKeys} `
|
||||||
|
})
|
||||||
|
|
||||||
|
return [field]
|
||||||
}
|
}
|
||||||
} else if (showFieldSelector && (lastChar === ' ' || !value.includes(':'))) {
|
|
||||||
setShowFieldSelector(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const insertFieldMention = (campoId: string) => {
|
// Si lo estamos deseleccionando, solo quitamos el tag
|
||||||
const beforeCursor = input.slice(0, cursorPosition);
|
return prev.filter((f) => f.key !== field.key)
|
||||||
const afterCursor = input.slice(cursorPosition);
|
})
|
||||||
const lastColonIndex = beforeCursor.lastIndexOf(':');
|
setShowSuggestions(false)
|
||||||
const newInput = beforeCursor.slice(0, lastColonIndex) + `:${campoId}` + afterCursor;
|
}
|
||||||
setInput(newInput);
|
|
||||||
setShowFieldSelector(false);
|
|
||||||
textareaRef.current?.focus();
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSend = async () => {
|
const buildPrompt = (userInput: string) => {
|
||||||
if (!input.trim() || isLoading) return;
|
if (selectedFields.length === 0) return userInput
|
||||||
|
const fieldsText = selectedFields
|
||||||
|
.map((f) => `- ${f.label}: ${f.value || '(vacio)'}`)
|
||||||
|
.join('\n')
|
||||||
|
|
||||||
// Extract field mention if any
|
return `${userInput}\n\nCampos a analizar:\n${fieldsText}`.trim()
|
||||||
const fieldMatch = input.match(/:(\w+)/);
|
}
|
||||||
const campoId = fieldMatch ? fieldMatch[1] : undefined;
|
|
||||||
|
|
||||||
setIsLoading(true);
|
const handleSend = async (promptOverride?: string) => {
|
||||||
onSendMessage(input, campoId);
|
const rawText = promptOverride || input
|
||||||
setInput('');
|
if (!rawText.trim() && selectedFields.length === 0) return
|
||||||
|
|
||||||
// Simulate AI response delay
|
const finalPrompt = buildPrompt(rawText)
|
||||||
setTimeout(() => {
|
|
||||||
setIsLoading(false);
|
|
||||||
}, 1500);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleQuickAction = (prompt: string) => {
|
setIsLoading(true)
|
||||||
setInput(prompt);
|
// Llamamos a la función que viene por props
|
||||||
textareaRef.current?.focus();
|
onSendMessage(finalPrompt, selectedFields[0]?.key)
|
||||||
};
|
|
||||||
|
|
||||||
const renderMessageContent = (content: string) => {
|
setInput('')
|
||||||
// Render field mentions as styled badges
|
setSelectedFields([])
|
||||||
return content.split(/(:[\w_]+)/g).map((part, i) => {
|
|
||||||
if (part.startsWith(':')) {
|
// Simular carga local para el feedback visual
|
||||||
const campo = campos.find(c => c.id === part.slice(1));
|
setTimeout(() => setIsLoading(false), 1200)
|
||||||
return (
|
}
|
||||||
<span key={i} className="field-mention mx-0.5">
|
|
||||||
{campo?.nombre || part}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return part;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-6 animate-fade-in">
|
<div className="flex h-[calc(100vh-160px)] max-h-[calc(100vh-160px)] w-full gap-6 overflow-hidden p-4">
|
||||||
<div className="flex items-center justify-between">
|
{/* PANEL DE CHAT PRINCIPAL */}
|
||||||
<div>
|
<div className="relative flex min-w-0 flex-[3] flex-col overflow-hidden rounded-xl border border-slate-200 bg-slate-50/50 shadow-sm">
|
||||||
<h2 className="font-display text-2xl font-semibold text-foreground flex items-center gap-2">
|
{/* Barra superior */}
|
||||||
<Sparkles className="w-6 h-6 text-accent" />
|
<div className="shrink-0 border-b bg-white p-3">
|
||||||
IA de la materia
|
<div className="flex flex-wrap items-center gap-2">
|
||||||
</h2>
|
<span className="text-[10px] font-bold tracking-wider text-slate-400 uppercase">
|
||||||
<p className="text-sm text-muted-foreground mt-1">
|
IA de Asignatura
|
||||||
Usa <kbd className="px-1.5 py-0.5 bg-muted rounded text-xs font-mono">:</kbd> para mencionar campos específicos
|
</span>
|
||||||
</p>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
||||||
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
|
{/* CONTENIDO DEL CHAT */}
|
||||||
{/* Chat area */}
|
<div className="relative min-h-0 flex-1">
|
||||||
<Card className="lg:col-span-2 card-elevated flex flex-col h-[600px]">
|
<ScrollArea ref={scrollRef} className="h-full w-full">
|
||||||
<CardHeader className="pb-2 border-b">
|
<div className="mx-auto max-w-3xl space-y-6 p-6">
|
||||||
<CardTitle className="text-sm font-medium text-muted-foreground">
|
{messages.map((msg) => (
|
||||||
Conversación
|
<div
|
||||||
</CardTitle>
|
key={msg.id}
|
||||||
</CardHeader>
|
className={`flex ${msg.role === 'user' ? 'flex-row-reverse' : 'flex-row'} items-start gap-3`}
|
||||||
<CardContent className="flex-1 flex flex-col p-0">
|
>
|
||||||
<ScrollArea className="flex-1 p-4" ref={scrollRef}>
|
<Avatar
|
||||||
<div className="space-y-4">
|
className={`h-8 w-8 shrink-0 border ${msg.role === 'assistant' ? 'bg-teal-50' : 'bg-slate-200'}`}
|
||||||
{messages.length === 0 ? (
|
>
|
||||||
<div className="text-center py-12">
|
<AvatarFallback className="text-[10px]">
|
||||||
<Bot className="w-12 h-12 mx-auto text-muted-foreground/50 mb-4" />
|
{msg.role === 'assistant' ? (
|
||||||
<p className="text-muted-foreground">
|
<Sparkles size={14} className="text-teal-600" />
|
||||||
Inicia una conversación para mejorar tu materia con IA
|
) : (
|
||||||
</p>
|
<UserCheck size={14} />
|
||||||
</div>
|
|
||||||
) : (
|
|
||||||
messages.map((message) => (
|
|
||||||
<div key={message.id} className={cn(
|
|
||||||
"flex gap-3",
|
|
||||||
message.role === 'user' ? "justify-end" : "justify-start"
|
|
||||||
)}>
|
|
||||||
{message.role === 'assistant' && (
|
|
||||||
<div className="w-8 h-8 rounded-full bg-accent/20 flex items-center justify-center flex-shrink-0">
|
|
||||||
<Bot className="w-4 h-4 text-accent" />
|
|
||||||
</div>
|
|
||||||
)}
|
)}
|
||||||
<div className={cn(
|
</AvatarFallback>
|
||||||
"max-w-[80%] rounded-lg px-4 py-3",
|
</Avatar>
|
||||||
message.role === 'user'
|
<div
|
||||||
? "bg-primary text-primary-foreground"
|
className={`flex max-w-[85%] flex-col ${msg.role === 'user' ? 'items-end' : 'items-start'}`}
|
||||||
: "bg-muted"
|
>
|
||||||
)}>
|
<div
|
||||||
<p className="text-sm whitespace-pre-wrap">
|
className={cn(
|
||||||
{renderMessageContent(message.content)}
|
'rounded-2xl p-3 text-sm whitespace-pre-wrap shadow-sm',
|
||||||
</p>
|
msg.role === 'user'
|
||||||
{message.sugerencia && !message.sugerencia.aceptada && (
|
? 'rounded-tr-none bg-teal-600 text-white'
|
||||||
<div className="mt-3 p-3 bg-background/80 rounded-md border">
|
: 'rounded-tl-none border bg-white text-slate-700',
|
||||||
<p className="text-xs font-medium text-muted-foreground mb-2">
|
)}
|
||||||
Sugerencia para: {message.sugerencia.campoNombre}
|
>
|
||||||
</p>
|
{msg.content}
|
||||||
<div className="text-sm text-foreground bg-accent/10 p-2 rounded mb-3 max-h-32 overflow-y-auto">
|
</div>
|
||||||
{message.sugerencia.valorSugerido}
|
|
||||||
</div>
|
{/* Renderizado de Sugerencias (Homologado con lógica de Materia) */}
|
||||||
<div className="flex items-center gap-2">
|
{msg.sugerencia && !msg.sugerencia.aceptada && (
|
||||||
<Button
|
<div className="animate-in fade-in slide-in-from-top-1 mt-3 w-full">
|
||||||
size="sm"
|
<div className="rounded-xl border border-teal-100 bg-white p-4 shadow-md">
|
||||||
onClick={() => onAcceptSuggestion(message.sugerencia!)}
|
<p className="mb-2 text-[10px] font-bold text-slate-400 uppercase">
|
||||||
className="bg-success hover:bg-success/90 text-success-foreground"
|
Propuesta para: {msg.sugerencia.campoNombre}
|
||||||
>
|
</p>
|
||||||
<Check className="w-3 h-3 mr-1" />
|
<div className="mb-4 max-h-40 overflow-y-auto rounded-lg bg-slate-50 p-3 text-xs text-slate-600 italic">
|
||||||
Aplicar
|
{msg.sugerencia.valorSugerido}
|
||||||
</Button>
|
</div>
|
||||||
<Button
|
<div className="flex gap-2">
|
||||||
size="sm"
|
<Button
|
||||||
variant="outline"
|
size="sm"
|
||||||
onClick={() => onRejectSuggestion(message.id)}
|
onClick={() =>
|
||||||
>
|
onAcceptSuggestion(msg.sugerencia!)
|
||||||
<X className="w-3 h-3 mr-1" />
|
}
|
||||||
Rechazar
|
className="h-8 bg-teal-600 text-xs hover:bg-teal-700"
|
||||||
</Button>
|
>
|
||||||
</div>
|
<Check size={14} className="mr-1" /> Aplicar
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
size="sm"
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => onRejectSuggestion(msg.id)}
|
||||||
|
className="h-8 text-xs"
|
||||||
|
>
|
||||||
|
<X size={14} className="mr-1" /> Descartar
|
||||||
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
)}
|
|
||||||
{message.sugerencia?.aceptada && (
|
|
||||||
<Badge className="mt-2 badge-library">
|
|
||||||
<Check className="w-3 h-3 mr-1" />
|
|
||||||
Sugerencia aplicada
|
|
||||||
</Badge>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
{message.role === 'user' && (
|
|
||||||
<div className="w-8 h-8 rounded-full bg-primary flex items-center justify-center flex-shrink-0">
|
|
||||||
<User className="w-4 h-4 text-primary-foreground" />
|
|
||||||
</div>
|
</div>
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
))
|
|
||||||
)}
|
|
||||||
{isLoading && (
|
|
||||||
<div className="flex gap-3">
|
|
||||||
<div className="w-8 h-8 rounded-full bg-accent/20 flex items-center justify-center flex-shrink-0">
|
|
||||||
<Bot className="w-4 h-4 text-accent animate-pulse" />
|
|
||||||
</div>
|
|
||||||
<div className="bg-muted rounded-lg px-4 py-3">
|
|
||||||
<div className="flex items-center gap-1">
|
|
||||||
<div className="w-2 h-2 bg-accent rounded-full animate-bounce [animation-delay:-0.3s]" />
|
|
||||||
<div className="w-2 h-2 bg-accent rounded-full animate-bounce [animation-delay:-0.15s]" />
|
|
||||||
<div className="w-2 h-2 bg-accent rounded-full animate-bounce" />
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
)}
|
||||||
|
{msg.sugerencia?.aceptada && (
|
||||||
|
<Badge className="mt-2 border-teal-200 bg-teal-100 text-teal-700 hover:bg-teal-100">
|
||||||
|
<Check className="mr-1 h-3 w-3" /> Sugerencia aplicada
|
||||||
|
</Badge>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
</div>
|
||||||
</div>
|
))}
|
||||||
</ScrollArea>
|
{isLoading && (
|
||||||
|
<div className="flex gap-2 p-4">
|
||||||
|
<div className="h-2 w-2 animate-bounce rounded-full bg-teal-400" />
|
||||||
|
<div className="h-2 w-2 animate-bounce rounded-full bg-teal-400 [animation-delay:0.2s]" />
|
||||||
|
<div className="h-2 w-2 animate-bounce rounded-full bg-teal-400 [animation-delay:0.4s]" />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</ScrollArea>
|
||||||
|
</div>
|
||||||
|
|
||||||
{/* Input area */}
|
{/* INPUT FIJO AL FONDO */}
|
||||||
<div className="p-4 border-t">
|
<div className="shrink-0 border-t bg-white p-4">
|
||||||
<div className="relative">
|
<div className="relative mx-auto max-w-4xl">
|
||||||
|
{/* MENÚ DE SUGERENCIAS FLOTANTE */}
|
||||||
|
{showSuggestions && (
|
||||||
|
<div className="animate-in slide-in-from-bottom-2 absolute bottom-full z-50 mb-2 w-72 overflow-hidden rounded-xl border bg-white shadow-2xl">
|
||||||
|
<div className="border-b bg-slate-50 px-3 py-2 text-[10px] font-bold tracking-wider text-slate-500 uppercase">
|
||||||
|
Seleccionar campo de materia
|
||||||
|
</div>
|
||||||
|
<div className="max-h-64 overflow-y-auto p-1">
|
||||||
|
{availableFields.map((field) => (
|
||||||
|
<button
|
||||||
|
key={field.key}
|
||||||
|
onClick={() => toggleField(field)}
|
||||||
|
className="group flex w-full items-center justify-between rounded-lg px-3 py-2 text-left text-sm transition-colors hover:bg-teal-50"
|
||||||
|
>
|
||||||
|
<span className="text-slate-700 group-hover:text-teal-700">
|
||||||
|
{field.label}
|
||||||
|
</span>
|
||||||
|
{selectedFields.find((f) => f.key === field.key) && (
|
||||||
|
<Check size={14} className="text-teal-600" />
|
||||||
|
)}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* CONTENEDOR DEL INPUT */}
|
||||||
|
<div className="flex flex-col gap-2 rounded-xl border bg-slate-50 p-2 transition-all focus-within:bg-white focus-within:ring-1 focus-within:ring-teal-500">
|
||||||
|
{/* Visualización de Tags */}
|
||||||
|
{selectedFields.length > 0 && (
|
||||||
|
<div className="flex flex-wrap gap-2 px-2 pt-1">
|
||||||
|
{selectedFields.map((field) => (
|
||||||
|
<div
|
||||||
|
key={field.key}
|
||||||
|
className="animate-in zoom-in-95 flex items-center gap-1 rounded-md border border-teal-200 bg-teal-100 px-2 py-0.5 text-[11px] font-semibold text-teal-800"
|
||||||
|
>
|
||||||
|
<span className="opacity-70">Campo:</span> {field.label}
|
||||||
|
<button
|
||||||
|
onClick={() => toggleField(field)}
|
||||||
|
className="ml-1 rounded-full p-0.5 transition-colors hover:bg-teal-200"
|
||||||
|
>
|
||||||
|
<X size={10} />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="flex items-end gap-2">
|
||||||
<Textarea
|
<Textarea
|
||||||
ref={textareaRef}
|
|
||||||
value={input}
|
value={input}
|
||||||
onChange={handleInputChange}
|
onChange={handleInputChange}
|
||||||
onKeyDown={(e) => {
|
onKeyDown={(e) => {
|
||||||
if (e.key === 'Enter' && !e.shiftKey) {
|
if (e.key === 'Enter' && !e.shiftKey) {
|
||||||
e.preventDefault();
|
e.preventDefault()
|
||||||
handleSend();
|
handleSend()
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
placeholder="Escribe tu mensaje... Usa : para mencionar campos"
|
placeholder={
|
||||||
className="min-h-[80px] pr-12 resize-none"
|
selectedFields.length > 0
|
||||||
disabled={isLoading}
|
? 'Instrucciones para los campos seleccionados...'
|
||||||
|
: 'Escribe tu solicitud o ":" para campos...'
|
||||||
|
}
|
||||||
|
className="max-h-[120px] min-h-[40px] flex-1 resize-none border-none bg-transparent py-2 text-sm shadow-none focus-visible:ring-0"
|
||||||
/>
|
/>
|
||||||
<Button
|
<Button
|
||||||
size="sm"
|
onClick={() => handleSend()}
|
||||||
onClick={handleSend}
|
disabled={
|
||||||
disabled={!input.trim() || isLoading}
|
(!input.trim() && selectedFields.length === 0) || isLoading
|
||||||
className="absolute bottom-3 right-3 h-8 w-8 p-0"
|
}
|
||||||
|
size="icon"
|
||||||
|
className="mb-1 h-9 w-9 shrink-0 bg-teal-600 hover:bg-teal-700"
|
||||||
>
|
>
|
||||||
<Send className="w-4 h-4" />
|
<Send size={16} className="text-white" />
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Field selector popover */}
|
|
||||||
{showFieldSelector && (
|
|
||||||
<div className="absolute z-50 mt-1 w-64 bg-popover border rounded-lg shadow-lg">
|
|
||||||
<Command>
|
|
||||||
<CommandInput placeholder="Buscar campo..." />
|
|
||||||
<CommandList>
|
|
||||||
<CommandEmpty>No se encontró el campo</CommandEmpty>
|
|
||||||
<CommandGroup heading="Campos disponibles">
|
|
||||||
{campos.map((campo) => (
|
|
||||||
<CommandItem
|
|
||||||
key={campo.id}
|
|
||||||
value={campo.id}
|
|
||||||
onSelect={() => insertFieldMention(campo.id)}
|
|
||||||
className="cursor-pointer"
|
|
||||||
>
|
|
||||||
<span className="font-mono text-xs text-accent mr-2">
|
|
||||||
:{campo.id}
|
|
||||||
</span>
|
|
||||||
<span>{campo.nombre}</span>
|
|
||||||
</CommandItem>
|
|
||||||
))}
|
|
||||||
</CommandGroup>
|
|
||||||
</CommandList>
|
|
||||||
</Command>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</div>
|
</div>
|
||||||
</CardContent>
|
</div>
|
||||||
</Card>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
{/* Sidebar with quick actions and fields */}
|
{/* PANEL LATERAL (ACCIONES RÁPIDAS) */}
|
||||||
<div className="space-y-4">
|
<div className="flex flex-[1] flex-col gap-4 overflow-y-auto pr-2">
|
||||||
{/* Quick actions */}
|
<h4 className="flex items-center gap-2 text-left text-sm font-bold text-slate-800">
|
||||||
<Card className="card-elevated">
|
<Lightbulb size={18} className="text-orange-500" /> Acciones rápidas
|
||||||
<CardHeader className="pb-2">
|
</h4>
|
||||||
<CardTitle className="text-sm font-medium">Acciones rápidas</CardTitle>
|
<div className="space-y-2">
|
||||||
</CardHeader>
|
{PRESETS.map((preset) => (
|
||||||
<CardContent className="space-y-2">
|
<button
|
||||||
{quickActions.map((action) => {
|
key={preset.id}
|
||||||
const Icon = action.icon;
|
onClick={() => handleSend(preset.prompt)}
|
||||||
return (
|
className="group flex w-full items-center gap-3 rounded-xl border bg-white p-3 text-left text-sm shadow-sm transition-all hover:border-teal-500 hover:bg-teal-50"
|
||||||
<Button
|
>
|
||||||
key={action.id}
|
<div className="rounded-lg bg-slate-100 p-2 text-slate-500 group-hover:bg-teal-100 group-hover:text-teal-600">
|
||||||
variant="outline"
|
<preset.icon size={16} />
|
||||||
className="w-full justify-start text-left h-auto py-3"
|
</div>
|
||||||
onClick={() => handleQuickAction(action.prompt)}
|
<span className="leading-tight font-medium text-slate-700">
|
||||||
>
|
{preset.label}
|
||||||
<Icon className="w-4 h-4 mr-2 text-accent flex-shrink-0" />
|
</span>
|
||||||
<span className="text-sm">{action.label}</span>
|
</button>
|
||||||
</Button>
|
))}
|
||||||
);
|
|
||||||
})}
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
|
|
||||||
{/* Available fields */}
|
|
||||||
<Card className="card-elevated">
|
|
||||||
<CardHeader className="pb-2">
|
|
||||||
<CardTitle className="text-sm font-medium">Campos de la materia</CardTitle>
|
|
||||||
</CardHeader>
|
|
||||||
<CardContent>
|
|
||||||
<ScrollArea className="h-[280px]">
|
|
||||||
<div className="space-y-2">
|
|
||||||
{campos.map((campo) => {
|
|
||||||
const hasValue = !!datosGenerales[campo.id];
|
|
||||||
return (
|
|
||||||
<div
|
|
||||||
key={campo.id}
|
|
||||||
className={cn(
|
|
||||||
"p-2 rounded-md border cursor-pointer transition-colors hover:bg-muted/50",
|
|
||||||
hasValue ? "border-success/30" : "border-warning/30"
|
|
||||||
)}
|
|
||||||
onClick={() => {
|
|
||||||
setInput(prev => prev + `:${campo.id} `);
|
|
||||||
textareaRef.current?.focus();
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<div className="flex items-center justify-between">
|
|
||||||
<span className="text-xs font-mono text-accent">:{campo.id}</span>
|
|
||||||
{hasValue ? (
|
|
||||||
<Badge variant="outline" className="text-xs text-success border-success/30">
|
|
||||||
Completo
|
|
||||||
</Badge>
|
|
||||||
) : (
|
|
||||||
<Badge variant="outline" className="text-xs text-warning border-warning/30">
|
|
||||||
Vacío
|
|
||||||
</Badge>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
<p className="text-sm text-foreground mt-1">{campo.nombre}</p>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</div>
|
|
||||||
</ScrollArea>
|
|
||||||
</CardContent>
|
|
||||||
</Card>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,7 +106,7 @@ function RouteComponent() {
|
|||||||
if (field && !selectedFields.find((sf) => sf.key === field.key)) {
|
if (field && !selectedFields.find((sf) => sf.key === field.key)) {
|
||||||
setSelectedFields([field])
|
setSelectedFields([field])
|
||||||
}
|
}
|
||||||
setInput(`Mejora este campo: [${field?.label}] `)
|
setInput(`Mejora este campo: ${field?.label} `)
|
||||||
}
|
}
|
||||||
}, [availableFields])
|
}, [availableFields])
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user