import { Check, Loader2 } from 'lucide-react' import { useState } from 'react' import { Button } from '@/components/ui/button' import { useUpdatePlanFields, useUpdateRecommendationApplied } from '@/data' export const ImprovementCard = ({ suggestions, onApply, planId, dbMessageId, currentDatos, activeChatId, onApplySuccess, }: { suggestions: Array onApply?: (key: string, value: string) => void planId: string currentDatos: any dbMessageId: string activeChatId: any onApplySuccess?: (key: string) => void }) => { const [localApplied, setLocalApplied] = useState>([]) const updatePlan = useUpdatePlanFields() const updateAppliedStatus = useUpdateRecommendationApplied() const handleApply = (key: string, newValue: string) => { if (!currentDatos) return const currentValue = currentDatos[key] let finalValue: any if ( typeof currentValue === 'object' && currentValue !== null && 'description' in currentValue ) { finalValue = { ...currentValue, description: newValue } } else { finalValue = newValue } const datosActualizados = { ...currentDatos, [key]: finalValue, } updatePlan.mutate( { planId: planId as any, patch: { datos: datosActualizados }, }, { onSuccess: () => { setLocalApplied((prev) => [...prev, key]) if (onApplySuccess) onApplySuccess(key) // --- CAMBIO AQUƍ: Ahora enviamos el ID del mensaje --- if (dbMessageId) { updateAppliedStatus.mutate({ conversacionId: dbMessageId, // Cambiamos el nombre de la propiedad si es necesario campoAfectado: key, }) } if (onApply) onApply(key, newValue) }, }, ) } return (
{suggestions.map((sug) => { const isApplied = sug.applied === true || localApplied.includes(sug.key) const isUpdating = updatePlan.isPending && updatePlan.variables.patch.datos?.[sug.key] !== undefined return (

{sug.label}

{sug.newValue}
) })}
) }