import React from "react"; import Link from "next/link"; import Image from "next/image"; import { ExternalLink } from "lucide-react"; import { client, urlFor } from "@/lib/sanity"; import { Card, CardContent } from "../ui/card"; import { Pagination, PaginationContent, PaginationItem, PaginationLink, PaginationNext, PaginationPrevious, } from "@/components/ui/pagination"; interface BlogPostcard { title: string; smallDescription: string; currentSlug: string; titleImage: string; } interface BlogCardsProps { searchParams: { page?: string }; } const BlogCards: React.FC = async ({ searchParams }) => { const cardsPerPage = 6; const currentPage = searchParams.page ? parseInt(searchParams.page) : 1; // Fetch the blog posts const query = `*[_type == 'blog'] | order(_createdAt desc) { title, smallDescription, "currentSlug": slug.current, titleImage }[${(currentPage - 1) * cardsPerPage}...${currentPage * cardsPerPage}]`; const posts: BlogPostcard[] = await client.fetch(query); // Fetch the total number of blog posts const totalPostsQuery = `count(*[_type == 'blog'])`; const totalPosts: number = await client.fetch(totalPostsQuery); const totalPages = Math.ceil(totalPosts / cardsPerPage); return ( <>
{posts.map((post, idx) => (
{post.title}

{post.title}

{post.smallDescription}

))}
{totalPages > 1 && ( {currentPage > 1 && ( )} {Array.from({ length: totalPages }).map((_, i) => ( {i + 1} ))} {currentPage < totalPages && ( )} )}
); }; export default BlogCards;