const { useState, useEffect } = React;
// --- SISTEMA DE ÍCONOS VECTORIALES (Sin dependencias externas, carga ultrarrápida) ---
const SvgIcon = ({ name, className, style }) => {
const icons = {
FileText: <>>,
Video: <>>,
Mic: <>>,
Tag: <>>,
BookOpen: <>>,
ImageIcon: <>>,
Headphones: <>>,
Globe: <>>,
Newspaper: <>>,
Search: <>>,
Pin: <>>,
ArrowLeft: <>>,
ArrowRight: <>>,
LinkIcon: <>>
};
return (
);
};
function FrontendApp() {
// Validar conexión a WP
if (typeof wpdhPublicApi === 'undefined') {
return
Cargando directorio...
;
}
const [loading, setLoading] = useState(true);
const [links, setLinks] = useState([]);
const [featured, setFeatured] = useState([]);
const [categories, setCategories] = useState([]);
// Filtros
const [searchQuery, setSearchQuery] = useState('');
const [selectedCategory, setSelectedCategory] = useState('all');
const [currentPage, setCurrentPage] = useState(1);
const itemsPerPage = 50;
// Fetch a la Base de Datos
const fetchPublicData = async () => {
setLoading(true);
try {
const queryParams = new URLSearchParams({
search: searchQuery,
cat: selectedCategory
});
const res = await fetch(`${wpdhPublicApi.root}/public-data?${queryParams.toString()}`);
const data = await res.json();
if (data) {
setCategories(data.categories || []);
setFeatured(data.featured || []);
// Mezclar los enlaces aleatoriamente para una vista más orgánica
let mixedLinks = data.links || [];
for (let i = mixedLinks.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[mixedLinks[i], mixedLinks[j]] = [mixedLinks[j], mixedLinks[i]];
}
setLinks(mixedLinks);
}
} catch(e) {
console.error("Error obteniendo datos públicos:", e);
}
setLoading(false);
};
// Registrar click silencioso
const trackClick = (id) => {
fetch(`${wpdhPublicApi.root}/click/${id}`, { method: 'POST' }).catch(()=>console.log("No se pudo registrar el click"));
};
// Traer datos cada vez que cambia el buscador o categoría
useEffect(() => {
// Debounce para no saturar el servidor al escribir rápido
const timer = setTimeout(() => {
setCurrentPage(1); // Volver a página 1
fetchPublicData();
}, 400);
return () => clearTimeout(timer);
}, [searchQuery, selectedCategory]);
const totalPages = Math.ceil(links.length / itemsPerPage);
const currentLinks = links.slice((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage);
const getCategoryData = (catId) => {
return categories.find(c => c.id === catId) || { name: catId, icon: 'LinkIcon', cta: 'Visitar', color: '#64748b' };
};
return (
{/* COLUMNA IZQUIERDA (2/3) - Filtros y Enlaces */}
{/* Cabecera dinámica (Buscador + Filtro) */}
{searchQuery.trim().length > 0 ? `Resultados: "${searchQuery}"` : 'Navega el Pasado'}
{/* Lista de enlaces */}
{/* Paginación */}
{totalPages > 1 && (
Página {currentPage} de {totalPages}
)}
{/* COLUMNA DERECHA (1/3) - Destacados */}
);
}
const rootElement = document.getElementById('wpdh-frontend-root');
if (rootElement) {
const root = ReactDOM.createRoot(rootElement);
root.render();
}