import React from "react"; import Link from "next/link"; import Image from "next/image"; import { ChevronLeft, ChevronRight, ExternalLink } from "lucide-react"; import { client, urlFor } from "@/sanity/lib/client"; import { format } from "date-fns"; import PropTypes from "prop-types"; const BlogCards = async (props) => { "use server"; // Change in /blog/page/[id] route and in /api/revalidate route too! const cardsPerPage = 6; const currentPage = props.page; const query = `*[_type == 'blog'] | order(_createdAt desc) { title, smallDescription, "currentSlug": slug.current, titleImage, _createdAt }[${(currentPage - 1) * cardsPerPage}...${currentPage * cardsPerPage}]`; const posts = await client.fetch(query, {}, { cache: "no-store" }); const totalPostsQuery = `count(*[_type == 'blog'])`; const totalPosts = await client.fetch( totalPostsQuery, {}, { cache: "no-store" } ); const totalPages = Math.ceil(totalPosts / cardsPerPage); let begPage = currentPage - 2; let endPage = currentPage + 2; if (endPage > totalPages) { begPage -= endPage - totalPages; endPage = totalPages; } if (begPage < 1) { endPage += 1 - begPage; begPage = 1; } return ( <>
{posts.map((post, idx) => { const formattedDate = format( new Date(post._createdAt), "MMMM d, yyyy" ); const truncatedDescription = post.smallDescription.length > 130 ? post.smallDescription.substring(0, 130) + "..." : post.smallDescription; return (
{post.title}

{post.title}

{truncatedDescription}

Published on: {formattedDate}

); })}
{
{totalPages > 1 && ( )}
} ); }; BlogCards.propTypes = { page: PropTypes.number.isRequired }; export default BlogCards;