From 4c7c4d146853959c3fb2ea0f4906ac60bd37a7bf Mon Sep 17 00:00:00 2001 From: "Carlos (Almeira)" Date: Fri, 27 Mar 2026 21:17:28 -0500 Subject: [PATCH] fix(stabilization): resolved media object fragility and refined sidebar error boundaries --- src/components/AdWrappers.tsx | 28 +++++++++++++++++----------- src/components/ClientSidebars.tsx | 9 ++++++++- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/components/AdWrappers.tsx b/src/components/AdWrappers.tsx index a2f4544..e1a71db 100644 --- a/src/components/AdWrappers.tsx +++ b/src/components/AdWrappers.tsx @@ -51,12 +51,14 @@ export async function GlobalTopBanner() { } export async function GlobalSidebar() { - const { createAdminClient } = await import('@/lib/appwrite.server'); - const client = await createAdminClient(); + let settings = { rotation_seconds: 5 }; let groupedAds: Record = {}; - let settings = await fetchSettings(); try { + settings = await fetchSettings(); + const client = await createAdminClient(); + + // 1. Obtener tipos vĂ¡lidos para sidebar const typesRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'publication_types', []); const validCodes = ['DISPLAY_SLIDER', 'EVENTO', 'CLASIFICADO', 'AD_BANNER']; const validTypeIds = typesRes.documents.filter(t => validCodes.includes(t.codigo)).map(t => t.$id); @@ -70,7 +72,9 @@ export async function GlobalSidebar() { const now = new Date(); const activePosts = postsRes.documents.filter(post => { - if (!post.tipo_publicacion_id || !validTypeIds.includes(post.tipo_publicacion_id)) return false; + if (!post.tipo_publicacion_id) return false; + const typeId = typeof post.tipo_publicacion_id === 'string' ? post.tipo_publicacion_id : post.tipo_publicacion_id.$id; + if (!validTypeIds.includes(typeId)) return false; if (post.fecha_finalizacion && new Date(post.fecha_finalizacion) < now) return false; return true; }); @@ -79,18 +83,16 @@ export async function GlobalSidebar() { let slogan = 'Patrocinado'; if (post.cliente_id) { try { + const client_id = typeof post.cliente_id === 'string' ? post.cliente_id : post.cliente_id.$id; const contracts = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'contracts', [ - Query.equal('cliente_id', post.cliente_id), + Query.equal('cliente_id', client_id), Query.equal('estado', 'Activo'), Query.limit(1) ]); if (contracts.documents.length > 0 && contracts.documents[0].eslogan) { slogan = contracts.documents[0].eslogan; - } else { - // Fallback to client name if we can fetch it or if it's in the object - slogan = 'Patrocinado'; } - } catch(e) { slogan = 'Patrocinado'; } + } catch(e) {} } if (!groupedAds[slogan]) groupedAds[slogan] = []; @@ -108,7 +110,6 @@ export async function GlobalSidebar() { export async function hasSidebarContent(): Promise { try { - const { createAdminClient } = await import('@/lib/appwrite.server'); const client = await createAdminClient(); const typesRes = await client.databases.listDocuments(APPWRITE_ALMEIRA_CONFIG.databaseId, 'publication_types', []); const validCodes = ['DISPLAY_SLIDER', 'EVENTO', 'CLASIFICADO', 'AD_BANNER']; @@ -123,11 +124,16 @@ export async function hasSidebarContent(): Promise { const now = new Date(); return postsRes.documents.some(post => { - if (!post.tipo_publicacion_id || !validTypeIds.includes(post.tipo_publicacion_id)) return false; + if (!post.tipo_publicacion_id) return false; + const typeId = typeof post.tipo_publicacion_id === 'string' ? post.tipo_publicacion_id : post.tipo_publicacion_id.$id; + if (!validTypeIds.includes(typeId)) return false; if (post.fecha_finalizacion && new Date(post.fecha_finalizacion) < now) return false; return true; }); } catch(e) { return false; } +} + return false; + } } diff --git a/src/components/ClientSidebars.tsx b/src/components/ClientSidebars.tsx index c3b3d94..617be0d 100644 --- a/src/components/ClientSidebars.tsx +++ b/src/components/ClientSidebars.tsx @@ -45,7 +45,14 @@ function ClientCarouselGroup({ eslogan, ads, rotationSeconds }: { eslogan: strin const ENDPOINT = process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT || 'https://appwrite.almeira.com.co/v1'; const PROJECT_ID = process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID || '69b84df5003bd8957c5c'; - const imgUrl = `${ENDPOINT}/storage/buckets/${activeAd.imagen_destacada_id.bucket_id}/files/${activeAd.imagen_destacada_id.file_id}/view?project=${PROJECT_ID}`; + let imgUrl = activeAd.og_image_url || null; + if (activeAd.imagen_destacada_id) { + if (typeof activeAd.imagen_destacada_id === 'object' && activeAd.imagen_destacada_id.bucket_id) { + imgUrl = `${ENDPOINT}/storage/buckets/${activeAd.imagen_destacada_id.bucket_id}/files/${activeAd.imagen_destacada_id.file_id}/view?project=${PROJECT_ID}`; + } else if (typeof activeAd.imagen_destacada_id === 'string') { + imgUrl = `${ENDPOINT}/storage/buckets/media/files/${activeAd.imagen_destacada_id}/view?project=${PROJECT_ID}`; + } + } const recordClickAndGo = () => { fetch('/api/metrics', {