'use client'; import { useState, useEffect } from 'react'; import { settingsApi, type StoreSettings } from '@/lib/api-client'; type FormData = StoreSettings; const TABS = [ { id: 'general', label: 'General', icon: '⚙️' }, { id: 'social', label: 'Redes sociales', icon: '🌐' }, { id: 'footer', label: 'Footer', icon: '📄' }, { id: 'ai', label: 'IA para SEO', icon: '✨' }, { id: 'smtp', label: 'SMTP / Email', icon: '✉️' }, ] as const; type TabId = (typeof TABS)[number]['id']; export default function SettingsPage() { const [data, setData] = useState(null); const [form, setForm] = useState(null); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [msg, setMsg] = useState(''); const [err, setErr] = useState(''); const [tab, setTab] = useState('general'); useEffect(() => { settingsApi.get().then(d => { setData(d); setForm(d); }).catch(() => setErr('Error al cargar ajustes')).finally(() => setLoading(false)); }, []); const handleSave = async (e: React.FormEvent) => { e.preventDefault(); if (!form) return; setSaving(true); setErr(''); setMsg(''); try { const { aiApiKey, smtpPass, ...settingsWithoutSecrets } = form; const updated = await settingsApi.update({ ...settingsWithoutSecrets, ...(aiApiKey ? { aiApiKey } : {}), ...(smtpPass ? { smtpPass } : {}), }); setData(updated); setForm(updated); setMsg('Cambios guardados correctamente'); setTimeout(() => setMsg(''), 4000); } catch (er) { setErr(er instanceof Error ? er.message : 'Error al guardar'); } finally { setSaving(false); } }; const field = (key: Exclude, label: string, opts?: { type?: string; placeholder?: string; rows?: number; hint?: string }) => (
{opts?.rows ? (