import React, { useEffect, useMemo, useState } from 'react'; interface Message { id: string; role: 'user' | 'assistant' | 'error'; content: string; avatar: string; } interface ApiMessage { role: 'user' | 'assistant'; content: string; } interface Props { chatId: string | null; uid: string; onRefreshChats: () => Promise; onEnsureChat: () => Promise; onError: (error: string | null) => void; } interface ExamplePrompt { label: string; prompt: string; } const EXAMPLE_PROMPTS: ExamplePrompt[] = [ { label: 'Definir audiencia 🎯', prompt: 'Ayúdame a definir una audiencia concreta para este correo: dolor principal, deseo y nivel de conciencia.', }, { label: 'Propuesta de valor 💎', prompt: 'Convierte mi producto en una promesa clara de transformación sin listar características aburridas.', }, { label: 'CTA que convierte 🚀', prompt: 'Dame 3 opciones de CTA claras para este email, con baja fricción y orientadas a una sola acción.', }, { label: 'Asunto + gancho ✉️', prompt: 'Propón 5 asuntos y 3 ganchos de apertura para aumentar aperturas y clics de este correo.', }, ]; const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); const escapeHtml = (text: string) => text .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, '''); const inlineMarkdown = (text: string) => { let html = escapeHtml(text); html = html.replace(/\*\*(.+?)\*\*/g, '$1'); html = html.replace(/\*(.+?)\*/g, '$1'); return html; }; const markdownToHtml = (raw: string) => { const lines = raw.split('\n'); const output: string[] = []; let inUl = false; let inOl = false; const closeLists = () => { if (inUl) { output.push(''); inUl = false; } if (inOl) { output.push(''); inOl = false; } }; lines.forEach((line) => { const trimmed = line.trim(); if (!trimmed) { closeLists(); output.push('
'); return; } if (trimmed.startsWith('### ')) { closeLists(); output.push(`

${inlineMarkdown(trimmed.slice(4))}

`); return; } if (trimmed.startsWith('## ')) { closeLists(); output.push(`

${inlineMarkdown(trimmed.slice(3))}

`); return; } if (trimmed.startsWith('# ')) { closeLists(); output.push(`

${inlineMarkdown(trimmed.slice(2))}

`); return; } const ordered = trimmed.match(/^(\d+)\.\s+(.*)$/); if (ordered) { if (inUl) { output.push(''); inUl = false; } if (!inOl) { output.push('
    '); inOl = true; } output.push(`
  1. ${inlineMarkdown(ordered[2])}
  2. `); return; } const bullet = trimmed.match(/^[-*]\s+(.*)$/); if (bullet) { if (inOl) { output.push('
'); inOl = false; } if (!inUl) { output.push('