import { APPWRITE_ALMEIRA_CONFIG } from '@/lib/appwrite'; import { createAdminClient, getLoggedInUser } from '@/lib/appwrite.server'; import { Query } from 'node-appwrite'; import { NewsCard } from '@/components/NewsCard'; import { GlobalSidebar, hasSidebarContent } from '@/components/AdWrappers'; import { RadioTower, Sparkles, ChevronDown } from 'lucide-react'; import { decodeHtmlEntities } from '@/lib/utils'; import { Settings } from '@/types/settings'; import { Post, MediaItem } from '@/types/publications'; async function fetchHomeData() { try { const client = await createAdminClient(); // 1. Settings const settingsRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'settings', []); const settings = JSON.parse(JSON.stringify(settingsRes.documents.length > 0 ? settingsRes.documents[0] : { ticker_count: 10, rotation_seconds: 5, feed_news_count: 12, show_news_date: true })) as Settings; const postsCount = settings.feed_news_count || 12; const cardSettings = { showCategory: settings.news_card_show_category ?? false, showFooterDate: String(settings.show_news_date) === 'true', showFooterButton: settings.news_card_show_footer_button ?? true, footerText: settings.news_card_footer_text || "" }; // 2. Main News (NOTICIA) const typeRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'publication_types', [ Query.equal('codigo', 'NOTICIA') ]); let posts: Post[] = []; if (typeRes.documents.length > 0) { const firstType = typeRes.documents[0]; const typeId = firstType?.$id; if (!typeId) return { posts: [], cardSettings }; const postsRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'posts', [ Query.equal('tipo_publicacion_id', typeId), Query.equal('estado', 'Publicado'), Query.orderDesc('fecha_publicacion'), Query.limit(postsCount) ]); posts = postsRes.documents as unknown as Post[]; // 3. Resolve Media const mediaIds = posts.map(p => p.imagen_destacada_id).filter(id => id && typeof id === 'string'); if (mediaIds.length > 0) { const mediaRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'media', [ Query.equal('$id', mediaIds) ]); const mediaMap = new Map(mediaRes.documents.map(m => [m.$id, m as unknown as MediaItem])); posts = posts.map(p => JSON.parse(JSON.stringify({ ...p, resolved_media: mediaMap.get(p.imagen_destacada_id as string) }))); } } return { posts, cardSettings }; } catch (e) { console.error("Home fetch error", e); return { posts: [], cardSettings: { showCategory: false, showFooterDate: true, showFooterButton: true, footerText: "" } }; } } export default async function Home() { const { posts, cardSettings } = await fetchHomeData(); const hasSidebar = await hasSidebarContent(); const user = await getLoggedInUser(); const isAdmin = (user as any)?.labels?.includes('admin') || (user as any)?.labels?.includes('editor') || user?.$id === 'admin-emergency'; return (
{/* Sci-Fi Hero Section */}
{/* Dynamic Orbs background */}
Señal En Vivo 24/7

NOTICIAS AL
SIGUIENTE NIVEL

El primer portal transaccional hiperconectado. Sumérgete en la actualidad de la región en tiempo real con una interfaz sin precedentes.

EXPLORAR CONTENIDO
{/* Scroll Indicator */} Descubrir
{/* Feed Section */}
Último Minuto

Feed de Noticias

{posts.length === 0 ? (

Sin Conexión Reciente

El portal se encuentra a la espera de señales desde el núcleo central.

) : (
{posts.map((post) => { const PROJECT_ID = process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID || '69b84df5003bd8957c5c'; const ENDPOINT = process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT || 'https://appwrite.almeira.com.co/v1'; let imagenUrl = post.og_image_url || null; if (post.resolved_media) { imagenUrl = `${ENDPOINT}/storage/buckets/${post.resolved_media.bucket_id}/files/${post.resolved_media.file_id}/preview?project=${PROJECT_ID}&width=800&height=500&quality=85`; } return ( ) })}
)}
{hasSidebar && ( )}
); }