import { APPWRITE_ALMEIRA_CONFIG } from '@/lib/appwrite'; import { createSessionClient } from '@/lib/appwrite.server'; import { Query } from 'node-appwrite'; import Link from 'next/link'; import { collectionMap, SCHEMA_MAP } from '@/lib/schema'; import { DataTable } from '@/components/dashboard/DataTable'; import { getUserPreferences } from '@/lib/preferences'; export default async function EntityPage({ params, searchParams }: { params: Promise<{ entity: string }>, searchParams: Promise<{ q?: string, page?: string, sort?: string, dir?: string }> }) { const { entity } = await params; const sParams = await searchParams; const q = sParams.q || ''; const page = Number(sParams.page) || 1; const sort = sParams.sort || '$createdAt'; const dir = (sParams.dir === 'asc' || sParams.dir === 'desc') ? sParams.dir : 'desc'; const limit = 15; const offset = (page - 1) * limit; const collectionId = collectionMap[entity]; const fieldsConfig = SCHEMA_MAP[entity] || []; const entityNames: Record = { posts: 'Publicaciones', categories: 'Categorías', clients: 'Clientes', client_types: 'Tipos de Cliente', countries: 'Países', departments: 'Departamentos', cities: 'Ciudades', contracts: 'Contratos', payment_methods: 'Formas de Pago', publication_types: 'Tipos de Publicación' }; const entityName = entityNames[entity] || entity.replace(/_/g, ' '); if (!collectionId) { return (

Colección no encontrada

La ruta /dashboard/{entity} no está configurada.

← Volver al inicio
); } // Fetch preferences const prefs = await getUserPreferences(); const hiddenColumns = prefs.hiddenColumns?.[entity] || []; let documents: any[] = []; let total = 0; try { const sessionClient = await createSessionClient(); if (!sessionClient) { throw new Error("No sesión activa"); } const queries = [ Query.limit(limit), Query.offset(offset), ]; // Apply Sorting if (dir === 'asc') { queries.push(Query.orderAsc(sort)); } else { queries.push(Query.orderDesc(sort)); } if (q) { const searchableFields = fieldsConfig .filter(f => f.type === 'text' || f.type === 'textarea') .map(f => f.name) .slice(0, 2); if (searchableFields.length > 0) { queries.push(Query.contains(searchableFields[0], q)); } } const response = await sessionClient.databases.listDocuments( APPWRITE_ALMEIRA_CONFIG.databaseId, collectionId, queries ); documents = response.documents; total = response.total; } catch (error) { console.error(`Error fetching from ${collectionId}:`, error); } return (

{entityName}

Total en sistema: {total}

+ Nuevo
{/* Pagination */} {total > limit && (
ANTERIOR Página {page} de {Math.ceil(total / limit)} = Math.ceil(total / limit) ? 'opacity-30 pointer-events-none bg-slate-900' : 'bg-slate-900 text-white hover:border-cyan-500'}`} > SIGUIENTE
)}
); }