feat: implement pagination
This commit is contained in:
parent
72baf20564
commit
aaaa2302f2
10 changed files with 255 additions and 55 deletions
4
package-lock.json
generated
4
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "astro-erudite",
|
"name": "astro-erudite",
|
||||||
"version": "0.0.1",
|
"version": "1.1.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "astro-erudite",
|
"name": "astro-erudite",
|
||||||
"version": "0.0.1",
|
"version": "1.1.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@astrojs/check": "^0.7.0",
|
"@astrojs/check": "^0.7.0",
|
||||||
"@astrojs/markdown-remark": "^5.2.0",
|
"@astrojs/markdown-remark": "^5.2.0",
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "astro-erudite",
|
"name": "astro-erudite",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"version": "0.0.1",
|
"version": "1.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "astro dev",
|
"dev": "astro dev",
|
||||||
|
|
|
@ -10,7 +10,7 @@ import SocialIcons from './SocialIcons.astro'
|
||||||
class="flex flex-col items-center justify-center gap-y-2 sm:flex-row sm:justify-between"
|
class="flex flex-col items-center justify-center gap-y-2 sm:flex-row sm:justify-between"
|
||||||
>
|
>
|
||||||
<div class="flex items-center">
|
<div class="flex items-center">
|
||||||
<p class="text-sm text-muted-foreground">
|
<p class="text-center text-sm text-muted-foreground">
|
||||||
© {new Date().getFullYear()} All rights reserved.
|
© {new Date().getFullYear()} All rights reserved.
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
174
src/components/ui/pagination.tsx
Normal file
174
src/components/ui/pagination.tsx
Normal file
|
@ -0,0 +1,174 @@
|
||||||
|
import * as React from 'react'
|
||||||
|
import { ChevronLeft, ChevronRight, MoreHorizontal } from 'lucide-react'
|
||||||
|
|
||||||
|
import { cn } from '@/lib/utils'
|
||||||
|
import { type ButtonProps, buttonVariants } from '@/components/ui/button'
|
||||||
|
|
||||||
|
const Pagination = ({ className, ...props }: React.ComponentProps<'nav'>) => (
|
||||||
|
<nav
|
||||||
|
role="navigation"
|
||||||
|
aria-label="pagination"
|
||||||
|
className={cn('mx-auto flex w-full justify-center', className)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
Pagination.displayName = 'Pagination'
|
||||||
|
|
||||||
|
const PaginationContent = React.forwardRef<
|
||||||
|
HTMLUListElement,
|
||||||
|
React.ComponentProps<'ul'>
|
||||||
|
>(({ className, ...props }, ref) => (
|
||||||
|
<ul
|
||||||
|
ref={ref}
|
||||||
|
className={cn('flex flex-row items-center gap-1', className)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
))
|
||||||
|
PaginationContent.displayName = 'PaginationContent'
|
||||||
|
|
||||||
|
const PaginationItem = React.forwardRef<
|
||||||
|
HTMLLIElement,
|
||||||
|
React.ComponentProps<'li'>
|
||||||
|
>(({ className, ...props }, ref) => (
|
||||||
|
<li ref={ref} className={cn('', className)} {...props} />
|
||||||
|
))
|
||||||
|
PaginationItem.displayName = 'PaginationItem'
|
||||||
|
|
||||||
|
type PaginationLinkProps = {
|
||||||
|
isActive?: boolean
|
||||||
|
isDisabled?: boolean
|
||||||
|
} & Pick<ButtonProps, 'size'> &
|
||||||
|
React.ComponentProps<'a'>
|
||||||
|
|
||||||
|
const PaginationLink = ({
|
||||||
|
className,
|
||||||
|
isActive,
|
||||||
|
isDisabled,
|
||||||
|
size = 'icon',
|
||||||
|
...props
|
||||||
|
}: PaginationLinkProps) => (
|
||||||
|
<a
|
||||||
|
aria-current={isActive ? 'page' : undefined}
|
||||||
|
className={cn(
|
||||||
|
buttonVariants({
|
||||||
|
variant: isActive ? 'outline' : 'ghost',
|
||||||
|
size,
|
||||||
|
}),
|
||||||
|
isDisabled && 'pointer-events-none opacity-50',
|
||||||
|
className,
|
||||||
|
)}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
PaginationLink.displayName = 'PaginationLink'
|
||||||
|
|
||||||
|
const PaginationPrevious = ({
|
||||||
|
className,
|
||||||
|
isDisabled,
|
||||||
|
...props
|
||||||
|
}: React.ComponentProps<typeof PaginationLink>) => (
|
||||||
|
<PaginationLink
|
||||||
|
aria-label="Go to previous page"
|
||||||
|
size="default"
|
||||||
|
className={cn('gap-1 pl-2.5', className)}
|
||||||
|
isDisabled={isDisabled}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
<ChevronLeft className="h-4 w-4" />
|
||||||
|
<span>Previous</span>
|
||||||
|
</PaginationLink>
|
||||||
|
)
|
||||||
|
PaginationPrevious.displayName = 'PaginationPrevious'
|
||||||
|
|
||||||
|
const PaginationNext = ({
|
||||||
|
className,
|
||||||
|
isDisabled,
|
||||||
|
...props
|
||||||
|
}: React.ComponentProps<typeof PaginationLink>) => (
|
||||||
|
<PaginationLink
|
||||||
|
aria-label="Go to next page"
|
||||||
|
size="default"
|
||||||
|
className={cn('gap-1 pr-2.5', className)}
|
||||||
|
isDisabled={isDisabled}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
<span>Next</span>
|
||||||
|
<ChevronRight className="h-4 w-4" />
|
||||||
|
</PaginationLink>
|
||||||
|
)
|
||||||
|
PaginationNext.displayName = 'PaginationNext'
|
||||||
|
|
||||||
|
const PaginationEllipsis = ({
|
||||||
|
className,
|
||||||
|
...props
|
||||||
|
}: React.ComponentProps<'span'>) => (
|
||||||
|
<span
|
||||||
|
aria-hidden
|
||||||
|
className={cn('flex h-9 w-9 items-center justify-center', className)}
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
<MoreHorizontal className="h-4 w-4" />
|
||||||
|
<span className="sr-only">More pages</span>
|
||||||
|
</span>
|
||||||
|
)
|
||||||
|
PaginationEllipsis.displayName = 'PaginationEllipsis'
|
||||||
|
|
||||||
|
interface PaginationProps {
|
||||||
|
currentPage: number
|
||||||
|
totalPages: number
|
||||||
|
baseUrl: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const PaginationComponent: React.FC<PaginationProps> = ({
|
||||||
|
currentPage,
|
||||||
|
totalPages,
|
||||||
|
baseUrl,
|
||||||
|
}) => {
|
||||||
|
const pages = Array.from({ length: totalPages }, (_, i) => i + 1)
|
||||||
|
|
||||||
|
const getPageUrl = (page: number) => {
|
||||||
|
if (page === 1) return baseUrl
|
||||||
|
return `${baseUrl}${page}`
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Pagination>
|
||||||
|
<PaginationContent className="flex-wrap">
|
||||||
|
<PaginationItem>
|
||||||
|
<PaginationPrevious
|
||||||
|
href={currentPage > 1 ? getPageUrl(currentPage - 1) : undefined}
|
||||||
|
isDisabled={currentPage === 1}
|
||||||
|
/>
|
||||||
|
</PaginationItem>
|
||||||
|
|
||||||
|
{pages.map((page) => (
|
||||||
|
<PaginationItem key={page}>
|
||||||
|
<PaginationLink
|
||||||
|
href={getPageUrl(page)}
|
||||||
|
isActive={page === currentPage}
|
||||||
|
>
|
||||||
|
{page}
|
||||||
|
</PaginationLink>
|
||||||
|
</PaginationItem>
|
||||||
|
))}
|
||||||
|
|
||||||
|
{totalPages > 5 && (
|
||||||
|
<PaginationItem>
|
||||||
|
<PaginationEllipsis />
|
||||||
|
</PaginationItem>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<PaginationItem>
|
||||||
|
<PaginationNext
|
||||||
|
href={
|
||||||
|
currentPage < totalPages ? getPageUrl(currentPage + 1) : undefined
|
||||||
|
}
|
||||||
|
isDisabled={currentPage === totalPages}
|
||||||
|
/>
|
||||||
|
</PaginationItem>
|
||||||
|
</PaginationContent>
|
||||||
|
</Pagination>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default PaginationComponent
|
|
@ -3,6 +3,7 @@ export type Site = {
|
||||||
DESCRIPTION: string
|
DESCRIPTION: string
|
||||||
EMAIL: string
|
EMAIL: string
|
||||||
NUM_POSTS_ON_HOMEPAGE: number
|
NUM_POSTS_ON_HOMEPAGE: number
|
||||||
|
POSTS_PER_PAGE: number
|
||||||
SITEURL: string
|
SITEURL: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +18,7 @@ export const SITE: Site = {
|
||||||
'astro-erudite is a opinionated, no-frills blogging template—built with Astro, Tailwind, and shadcn/ui.',
|
'astro-erudite is a opinionated, no-frills blogging template—built with Astro, Tailwind, and shadcn/ui.',
|
||||||
EMAIL: 'jason@enscribe.dev',
|
EMAIL: 'jason@enscribe.dev',
|
||||||
NUM_POSTS_ON_HOMEPAGE: 2,
|
NUM_POSTS_ON_HOMEPAGE: 2,
|
||||||
|
POSTS_PER_PAGE: 3,
|
||||||
SITEURL: 'https://astro-erudite.vercel.app',
|
SITEURL: 'https://astro-erudite.vercel.app',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
---
|
---
|
||||||
title: '2022 Post'
|
title: '2022 Post'
|
||||||
description: 'This a dummy post written in the year 2022.'
|
description: 'This a dummy post written in the year 2022.'
|
||||||
date: 2022-01-01
|
date: 2022-06-01
|
||||||
tags: ['dummy', 'placeholder']
|
tags: ['dummy', 'placeholder']
|
||||||
image: './2022.png'
|
image: './2022.png'
|
||||||
---
|
---
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
---
|
---
|
||||||
title: '2023 Post'
|
title: '2023 Post'
|
||||||
description: 'This a dummy post written in the year 2023.'
|
description: 'This a dummy post written in the year 2023.'
|
||||||
date: 2023-01-01
|
date: 2023-06-01
|
||||||
tags: ['dummy', 'placeholder']
|
tags: ['dummy', 'placeholder']
|
||||||
image: './2023.png'
|
image: './2023.png'
|
||||||
authors: ['enscribe']
|
authors: ['enscribe']
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
---
|
---
|
||||||
title: '2024 Post'
|
title: '2024 Post'
|
||||||
description: 'This a dummy post written in the year 2024 (with multiple authors).'
|
description: 'This a dummy post written in the year 2024 (with multiple authors).'
|
||||||
date: 2024-01-01
|
date: 2024-06-01
|
||||||
tags: ['dummy', 'placeholder']
|
tags: ['dummy', 'placeholder']
|
||||||
image: './2024.png'
|
image: './2024.png'
|
||||||
authors: ['enscribe', 'jktrn']
|
authors: ['enscribe', 'jktrn']
|
||||||
|
|
70
src/pages/blog/[page].astro
Normal file
70
src/pages/blog/[page].astro
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
---
|
||||||
|
import Breadcrumbs from '@/components/Breadcrumbs.astro'
|
||||||
|
import PaginationComponent from '@/components/ui/pagination'
|
||||||
|
import { SITE } from '@/consts'
|
||||||
|
import BlogCard from '@components/BlogCard.astro'
|
||||||
|
import Container from '@components/Container.astro'
|
||||||
|
import Layout from '@layouts/Layout.astro'
|
||||||
|
import type { PaginateFunction } from 'astro'
|
||||||
|
import { type CollectionEntry, getCollection } from 'astro:content'
|
||||||
|
|
||||||
|
export async function getStaticPaths({
|
||||||
|
paginate,
|
||||||
|
}: {
|
||||||
|
paginate: PaginateFunction
|
||||||
|
}) {
|
||||||
|
const allPosts = await getCollection('blog', ({ data }) => !data.draft)
|
||||||
|
return paginate(
|
||||||
|
allPosts.sort((a, b) => b.data.date.valueOf() - a.data.date.valueOf()),
|
||||||
|
{ pageSize: SITE.POSTS_PER_PAGE },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const { page } = Astro.props
|
||||||
|
|
||||||
|
const postsByYear = page.data.reduce(
|
||||||
|
(acc: Record<string, CollectionEntry<'blog'>[]>, post) => {
|
||||||
|
const year = post.data.date.getFullYear().toString()
|
||||||
|
;(acc[year] ??= []).push(post)
|
||||||
|
return acc
|
||||||
|
},
|
||||||
|
{},
|
||||||
|
)
|
||||||
|
|
||||||
|
const years = Object.keys(postsByYear).sort((a, b) => parseInt(b) - parseInt(a))
|
||||||
|
---
|
||||||
|
|
||||||
|
<Layout title="Blog" description="Blog">
|
||||||
|
<Container class="flex grow flex-col gap-y-6">
|
||||||
|
<Breadcrumbs
|
||||||
|
items={[
|
||||||
|
{ label: 'Blog', href: '/blog' },
|
||||||
|
{ label: `Page ${page.currentPage}` },
|
||||||
|
]}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<div class="flex min-h-[calc(100vh-18rem)] flex-col gap-y-8">
|
||||||
|
{
|
||||||
|
years.map((year) => (
|
||||||
|
<section class="flex flex-col gap-y-4">
|
||||||
|
<div class="font-semibold">{year}</div>
|
||||||
|
<ul class="not-prose flex flex-col gap-4">
|
||||||
|
{postsByYear[year].map((post) => (
|
||||||
|
<li>
|
||||||
|
<BlogCard entry={post} />
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</section>
|
||||||
|
))
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<PaginationComponent
|
||||||
|
currentPage={page.currentPage}
|
||||||
|
totalPages={page.lastPage}
|
||||||
|
baseUrl="/blog/"
|
||||||
|
client:load
|
||||||
|
/>
|
||||||
|
</Container>
|
||||||
|
</Layout>
|
|
@ -1,51 +1,5 @@
|
||||||
---
|
---
|
||||||
import Breadcrumbs from '@/components/Breadcrumbs.astro'
|
export const prerender = true
|
||||||
import BlogCard from '@components/BlogCard.astro'
|
|
||||||
import Container from '@components/Container.astro'
|
|
||||||
import Layout from '@layouts/Layout.astro'
|
|
||||||
import { type CollectionEntry, getCollection } from 'astro:content'
|
|
||||||
|
|
||||||
const data = (await getCollection('blog'))
|
return Astro.redirect('/blog/1')
|
||||||
.filter((post) => !post.data.draft)
|
|
||||||
.sort((a, b) => b.data.date.valueOf() - a.data.date.valueOf())
|
|
||||||
|
|
||||||
type Acc = {
|
|
||||||
[year: string]: CollectionEntry<'blog'>[]
|
|
||||||
}
|
|
||||||
|
|
||||||
const posts = data.reduce((acc: Acc, post) => {
|
|
||||||
const year = post.data.date.getFullYear().toString()
|
|
||||||
if (!acc[year]) {
|
|
||||||
acc[year] = []
|
|
||||||
}
|
|
||||||
acc[year].push(post)
|
|
||||||
return acc
|
|
||||||
}, {})
|
|
||||||
|
|
||||||
const years = Object.keys(posts).sort((a, b) => parseInt(b) - parseInt(a))
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<Layout title="Blog" description="Blog">
|
|
||||||
<Container class="flex flex-col gap-y-6">
|
|
||||||
<Breadcrumbs items={[{ label: 'Blog' }]} />
|
|
||||||
|
|
||||||
<div class="flex flex-col gap-y-8">
|
|
||||||
{
|
|
||||||
years.map((year) => (
|
|
||||||
<section class="flex flex-col gap-y-4">
|
|
||||||
<div class="font-semibold">{year}</div>
|
|
||||||
<div>
|
|
||||||
<ul class="not-prose flex flex-col gap-4">
|
|
||||||
{posts[year].map((post) => (
|
|
||||||
<li>
|
|
||||||
<BlogCard entry={post} />
|
|
||||||
</li>
|
|
||||||
))}
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</section>
|
|
||||||
))
|
|
||||||
}
|
|
||||||
</div>
|
|
||||||
</Container>
|
|
||||||
</Layout>
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue