feat: include pagination in news feed and clean up report scripts

This commit is contained in:
Carlos (Almeira) 2026-06-21 09:36:39 -05:00
parent 81ccb397f5
commit 1179f0ed54
2 changed files with 39 additions and 7 deletions

Binary file not shown.

View File

@ -9,7 +9,7 @@ import { decodeHtmlEntities } from '@/lib/utils';
import { Settings } from '@/types/settings';
import { Post, MediaItem } from '@/types/publications';
async function fetchHomeData() {
async function fetchHomeData(page: number = 1) {
try {
const client = await createAdminClient();
@ -36,17 +36,22 @@ async function fetchHomeData() {
]);
let posts: Post[] = [];
let totalPages = 1;
if (typeRes.documents.length > 0) {
const firstType = typeRes.documents[0];
const typeId = firstType?.$id;
if (!typeId) return { posts: [], cardSettings };
if (!typeId) return { posts: [], cardSettings, totalPages: 1 };
const offset = (page - 1) * postsCount;
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)
Query.limit(postsCount),
Query.offset(offset)
]);
posts = postsRes.documents as unknown as Post[];
totalPages = Math.ceil(postsRes.total / postsCount);
// 3. Resolve Media
const mediaIds = posts.map(p => p.imagen_destacada_id).filter(id => id && typeof id === 'string');
@ -62,17 +67,19 @@ async function fetchHomeData() {
}
}
return { posts, cardSettings };
return { posts, cardSettings, totalPages };
} catch (e) {
console.error("Home fetch error", e);
return { posts: [], cardSettings: { showCategory: false, showFooterDate: true, showFooterButton: true, footerText: "" } };
return { posts: [], cardSettings: { showCategory: false, showFooterDate: true, showFooterButton: true, footerText: "" }, totalPages: 1 };
}
}
export const dynamic = "force-dynamic";
export default async function Home() {
const { posts, cardSettings } = await fetchHomeData();
export default async function Home(props: { searchParams: Promise<{ [key: string]: string | string[] | undefined }> }) {
const searchParams = await props.searchParams;
const page = Number(searchParams?.page) || 1;
const { posts, cardSettings, totalPages } = await fetchHomeData(page);
const hasSidebar = await hasSidebarContent();
const user = await getLoggedInUser();
@ -173,6 +180,31 @@ export default async function Home() {
})}
</div>
)}
{/* Pagination Controls */}
{totalPages > 1 && (
<div className="flex justify-center items-center gap-4 mt-12">
{page > 1 ? (
<a href={`/?page=${page - 1}#noticias`} className="px-6 py-3 rounded-xl bg-slate-800/80 text-white font-bold hover:bg-slate-700 transition-colors border border-white/10">
Anterior
</a>
) : (
<div className="px-6 py-3 rounded-xl bg-slate-800/40 text-slate-500 font-bold border border-white/5 cursor-not-allowed">
Anterior
</div>
)}
<span className="text-slate-400 font-medium">Página {page} de {totalPages}</span>
{page < totalPages ? (
<a href={`/?page=${page + 1}#noticias`} className="px-6 py-3 rounded-xl bg-cyan-600 text-white font-bold hover:bg-cyan-500 transition-colors shadow-[0_0_15px_rgba(6,182,212,0.3)]">
Siguiente
</a>
) : (
<div className="px-6 py-3 rounded-xl bg-slate-800/40 text-slate-500 font-bold border border-white/5 cursor-not-allowed">
Siguiente
</div>
)}
</div>
)}
</div>
{hasSidebar && (