import { useEffect, useRef, useState } from 'react'; import { api } from '../../lib/api.js'; import { copyRichEmail } from '../../lib/emailClipboard.js'; const INITIAL = { subject: 'Actualités du Business Club SLUC Nancy', kicker: 'Business Club · SLUC Nancy', title: 'Votre titre ici', body: "Bonjour,\n\nRédigez ici votre message. Vous pouvez créer plusieurs paragraphes en laissant une ligne vide.\n\nLe modèle conserve automatiquement l'identité visuelle du Business Club.", buttonLabel: 'Découvrir', buttonUrl: window.location.origin, signature: "À très bientôt,\nL'équipe du Business Club SLUC Nancy", }; export function EmailCreatorTab() { const [form, setForm] = useState(INITIAL); const [data, setData] = useState(null); const [error, setError] = useState(''); const [copied, setCopied] = useState(''); const requestId = useRef(0); useEffect(() => { const id = ++requestId.current; const timer = setTimeout(() => { const completeButton = Boolean(form.buttonLabel && form.buttonUrl); api.post('/api/admin/emails/preview', { ...form, base: window.location.origin, buttonLabel: completeButton ? form.buttonLabel : '', buttonUrl: completeButton ? form.buttonUrl : '', }) .then((result) => { if (requestId.current !== id) return; setData(result); setError(''); }) .catch((err) => { if (requestId.current === id) setError(err.message); }); }, 350); return () => clearTimeout(timer); }, [form]); const onChange = (event) => setForm({ ...form, [event.target.name]: event.target.value }); const flash = (key) => { setCopied(key); setTimeout(() => setCopied(''), 1800); }; const copyEmail = async () => { try { await copyRichEmail(data.html, `${form.subject}\n\n${form.body}`); flash('email'); } catch { setError('La copie enrichie a échoué dans ce navigateur. Utilisez le téléchargement HTML.'); } }; const copySubject = async () => { try { await navigator.clipboard.writeText(form.subject); flash('subject'); } catch { setError("Impossible de copier l'objet."); } }; const download = () => { const blob = new Blob([data.html], { type: 'text/html' }); const url = URL.createObjectURL(blob); const anchor = document.createElement('a'); const slug = form.subject.normalize('NFD').replace(/[\u0300-\u036f]/g, '').replace(/[^a-zA-Z0-9]+/g, '-').replace(/^-|-$/g, '').toLowerCase() || 'email'; anchor.href = url; anchor.download = `${slug}.html`; document.body.appendChild(anchor); anchor.click(); anchor.remove(); setTimeout(() => URL.revokeObjectURL(url), 2000); }; return (

Composer un e-mail

Le logo, les couleurs et le pied de page sont ajoutés automatiquement.