import React from 'react'; import { dbConnection } from '../../db'; import { formatDate } from '../utils'; // Extract the post type from the database return type type Post = { id: number; path: string; title: string; date: string; readingTime: string; summary: string; content: string; tags: string[]; }; export function Home({ searchParams }: { searchParams: URLSearchParams }) { const postsPerPage = 10; const tags = searchParams.getAll('tag'); const currentPage = parseInt(searchParams.get('page') || "1", 10); const totalPages = Math.ceil(dbConnection.getNumOfPosts(tags) / postsPerPage); const offset = (currentPage - 1) * postsPerPage; const posts = dbConnection.getPosts(postsPerPage, offset, tags); // Get posts for the current page return (
{posts.length > 0 ? ( posts.map((post) => ( )) ) : (

No posts yet

Check back soon for new blog posts!

)}
); } function PostCard({ post }: { post: Post }) { const tags = post.tags; const formattedDate = formatDate(post.date); return (

{post.title}

5 min read
{tags.length > 0 && (
{tags.map((tag, index) => ( {tag} ))}
)}
{post.summary && (

{post.summary}

)}
); } function Pagination({ currentPage, totalPages, searchParams }:{ currentPage: number; totalPages: number, searchParams: URLSearchParams }) { // Calculate the range of page numbers to show let startPage: number; let endPage: number; if (totalPages <= 9) { // If there are fewer than 9 pages, show all of them startPage = 1; endPage = totalPages; } else { // If we have more than 9 pages, calculate the window if (currentPage <= 5) { // When we're at the start, show pages 1-9 startPage = 1; endPage = 9; } else if (currentPage >= totalPages - 4) { // When we're near the end, show the last 9 pages startPage = totalPages - 8; endPage = totalPages; } else { // Otherwise, center the window around the current page startPage = currentPage - 4; endPage = currentPage + 4; } } const pages = Array.from({ length: endPage - startPage + 1 }, (_, i) => startPage + i); let passThroughParams = '' searchParams.forEach((val, key) => { if(key != 'page') passThroughParams += `&${key}=${val}` }) return ( ); }