"use client"; import { useState } from "react"; import Link from "next/link"; import Pagination from "./Pagination"; import "public/assets/css/Pagination.css"; const truncateWords = (text, limit = 20) => { const words = text.split(" "); return words.length > limit ? words.slice(0, limit).join(" ") + " ..." : text; }; const stripHtml = (html) => { if (!html) return ""; return html.replace(/<[^>]*>/g, ""); }; const truncateTitle = (text, limit) => { if (!text) return ""; return text.length > limit ? text.slice(0, limit) + "..." : text; }; const BlogList = ({ blogs }) => { const [currentPage, setCurrentPage] = useState(1); const postsPerPage = 12; // Sort posts by date descending (newest first) - exactly like original code const sortedPosts = [...blogs].sort((a, b) => new Date(b.date) - new Date(a.date)); // Calculate pagination const totalPages = Math.ceil(sortedPosts.length / postsPerPage); const indexOfLastPost = currentPage * postsPerPage; const indexOfFirstPost = indexOfLastPost - postsPerPage; const currentPosts = sortedPosts.slice(indexOfFirstPost, indexOfLastPost); const handlePageChange = (page) => { setCurrentPage(page); }; return (
{/* Wrapper for scroll functionality */}
{currentPosts.map((blog) => (
{/* Blog Image */}
{blog.title}
{/* Blog Title */}

{truncateTitle(stripHtml(blog.title), 40)}

{/* Blog Excerpt */}

{truncateWords(stripHtml(blog.para))}

{/* Read More Button */} Read More
))}
{/* Pagination Component */} {totalPages > 1 && ( )}
); }; export default BlogList;