Skip to content

Commit

Permalink
feat: use Astro's built-in pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
No1ceTea authored Oct 7, 2024
1 parent d4a3ac6 commit aced187
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 180 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,15 @@
"react": "^18.3.1",
"react-dom": "^18.3.1",
"typescript-eslint": "^8.8.0"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"lint-staged": {
"*.{js,jsx,ts,tsx,md,mdx,json}": [
"prettier --plugin-search-dir=. --write"
]
}
}
40 changes: 21 additions & 19 deletions src/components/Pagination.astro
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
---
import type { Page } from "astro";
import LinkButton from "./LinkButton.astro";
import type { CollectionEntry } from "astro:content";
export interface Props {
currentPage: number;
totalPages: number;
prevUrl: string;
nextUrl: string;
page: Page<CollectionEntry<"blog">>;
}
const { currentPage, totalPages, prevUrl, nextUrl } = Astro.props;
const prev = currentPage > 1 ? "" : "disabled";
const next = currentPage < totalPages ? "" : "disabled";
const { page } = Astro.props;
---

{
totalPages > 1 && (
page.lastPage > 1 && (
<nav class="pagination-wrapper" aria-label="Pagination">
<LinkButton
disabled={prev === "disabled"}
href={prevUrl}
className={`mr-4 select-none ${prev}`}
disabled={!page.url.prev}
href={page.url.prev as string}
className={`mr-4 select-none ${page.url.prev ? "" : "disabled"}`}
ariaLabel="Previous"
>
<svg xmlns="http://www.w3.org/2000/svg" class={`${prev}-svg`}>
<svg
xmlns="http://www.w3.org/2000/svg"
class:list={[{ "disabled-svg": !page.url.prev }]}
>
<path d="M12.707 17.293 8.414 13H18v-2H8.414l4.293-4.293-1.414-1.414L4.586 12l6.707 6.707z" />
</svg>
Prev
</LinkButton>
{currentPage} / {totalPages}
{page.currentPage} / {page.lastPage}
<LinkButton
disabled={next === "disabled"}
href={nextUrl}
className={`ml-4 select-none ${next}`}
disabled={!page.url.next}
href={page.url.next as string}
className={`mx-4 select-none ${page.url.next ? "" : "disabled"}`}
ariaLabel="Next"
>
Next
<svg xmlns="http://www.w3.org/2000/svg" class={`${next}-svg`}>
<svg
xmlns="http://www.w3.org/2000/svg"
class:list={[{ "disabled-svg": !page.url.next }]}
>
<path d="m11.293 17.293 1.414 1.414L19.414 12l-6.707-6.707-1.414 1.414L15.586 11H6v2h9.586z" />
</svg>
</LinkButton>
Expand All @@ -54,4 +56,4 @@ const next = currentPage < totalPages ? "" : "disabled";
.disabled-svg {
@apply group-hover:!fill-skin-base;
}
</style>
</style>
24 changes: 9 additions & 15 deletions src/layouts/Posts.astro
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
---
import type { CollectionEntry } from "astro:content";
import Layout from "@layouts/Layout.astro";
import Main from "@layouts/Main.astro";
import Header from "@components/Header.astro";
import Footer from "@components/Footer.astro";
import Pagination from "@components/Pagination.astro";
import Card from "@components/Card";
import { SITE } from "@config";
import type { Page } from "astro";
import type { CollectionEntry } from "astro:content";
export interface Props {
currentPage: number;
totalPages: number;
paginatedPosts: CollectionEntry<"blog">[];
page: Page<CollectionEntry<"blog">>;
}
const { currentPage, totalPages, paginatedPosts } = Astro.props;
const { page } = Astro.props;
---

<Layout title={`Posts | ${SITE.title}`}>
<Header activeNav="posts" />
<Main pageTitle="Articles" pageDesc="Tous les articles.">
<Main pageTitle="Posts" pageDesc="All the articles I've posted.">
<ul>
{
paginatedPosts.map(({ data, slug }) => (
page.data.map(({ data, slug }) => (
<Card href={`/posts/${slug}/`} frontmatter={data} />
))
}
</ul>
</Main>

<Pagination
{currentPage}
{totalPages}
prevUrl={`/posts${currentPage - 1 !== 1 ? "/" + (currentPage - 1) : ""}/`}
nextUrl={`/posts/${currentPage + 1}/`}
/>
<Pagination {page} />

<Footer noMarginTop={totalPages > 1} />
</Layout>
<Footer noMarginTop={page.lastPage > 1} />
</Layout>
26 changes: 9 additions & 17 deletions src/layouts/TagPosts.astro
Original file line number Diff line number Diff line change
@@ -1,49 +1,41 @@
---
import { type CollectionEntry } from "astro:content";
import Layout from "@layouts/Layout.astro";
import Main from "@layouts/Main.astro";
import Header from "@components/Header.astro";
import Footer from "@components/Footer.astro";
import Card from "@components/Card";
import Pagination from "@components/Pagination.astro";
import { SITE } from "@config";
import type { Page } from "astro";
import type { CollectionEntry } from "astro:content";
export interface Props {
currentPage: number;
totalPages: number;
paginatedPosts: CollectionEntry<"blog">[];
page: Page<CollectionEntry<"blog">>;
tag: string;
tagName: string;
}
const { currentPage, totalPages, paginatedPosts, tag, tagName } = Astro.props;
const { page, tag, tagName } = Astro.props;
---

<Layout title={`Tag: ${tagName} | ${SITE.title}`}>
<Header activeNav="tags" />
<Main
pageTitle={[`Tag:`, `${tagName}`]}
titleTransition={tag}
pageDesc={`All the articles with the tag "${tagName}".`}
pageDesc={`Tous les articles avec le tag "${tagName}".`}
>
<h1 slot="title" transition:name={tag}>{`Tag:${tag}`}</h1>
<ul>
{
paginatedPosts.map(({ data, slug }) => (
page.data.map(({ data, slug }) => (
<Card href={`/posts/${slug}/`} frontmatter={data} />
))
}
</ul>
</Main>

<Pagination
{currentPage}
{totalPages}
prevUrl={`/tags/${tag}${
currentPage - 1 !== 1 ? "/" + (currentPage - 1) : ""
}/`}
nextUrl={`/tags/${tag}/${currentPage + 1}/`}
/>
<Pagination {page} />

<Footer noMarginTop={totalPages > 1} />
</Layout>
<Footer noMarginTop={page.lastPage > 1} />
</Layout>
16 changes: 16 additions & 0 deletions src/pages/posts/[...page].astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
import { SITE } from "@config";
import Posts from "@layouts/Posts.astro";
import type { GetStaticPaths } from "astro";
import { getCollection } from "astro:content";
import getSortedPosts from "@utils/getSortedPosts";
export const getStaticPaths = (async ({ paginate }) => {
const posts = await getCollection("blog", ({ data }) => !data.draft);
return paginate(getSortedPosts(posts), { pageSize: SITE.postPerPage });
}) satisfies GetStaticPaths;
const { page } = Astro.props;
---

<Posts {page} />
18 changes: 0 additions & 18 deletions src/pages/posts/index.astro

This file was deleted.

41 changes: 13 additions & 28 deletions src/pages/tags/[tag]/[page].astro
Original file line number Diff line number Diff line change
@@ -1,44 +1,29 @@
---
import { type CollectionEntry, getCollection } from "astro:content";
import { getCollection } from "astro:content";
import TagPosts from "@layouts/TagPosts.astro";
import getUniqueTags from "@utils/getUniqueTags";
import getPostsByTag from "@utils/getPostsByTag";
import getPageNumbers from "@utils/getPageNumbers";
import getPagination from "@utils/getPagination";
import type { GetStaticPathsOptions } from "astro";
import { SITE } from "@config";
export interface Props {
post: CollectionEntry<"blog">;
tag: string;
tagName: string;
}
export async function getStaticPaths() {
export async function getStaticPaths({ paginate }: GetStaticPathsOptions) {
const posts = await getCollection("blog");
const tags = getUniqueTags(posts);
return tags.flatMap(({ tag, tagName }) => {
const tagPosts = getPostsByTag(posts, tag);
const totalPages = getPageNumbers(tagPosts.length);
return totalPages.map((page) => ({
params: { tag, page },
props: { tag, tagName },
}));
return paginate(tagPosts, {
params: { tag },
props: { tagName },
pageSize: SITE.postPerPage,
});
});
}
const { page } = Astro.params;
const { tag, tagName } = Astro.props;
const posts = await getCollection("blog", ({ data }) => !data.draft);
const postsByTag = getPostsByTag(posts, tag);
const pagination = getPagination({
posts: postsByTag,
page,
});
const params = Astro.params;
const { tag } = params;
const { page, tagName } = Astro.props;
---

<TagPosts {...pagination} {tag} {tagName} />
<TagPosts {page} {tag} {tagName} />
32 changes: 0 additions & 32 deletions src/pages/tags/[tag]/index.astro

This file was deleted.

14 changes: 0 additions & 14 deletions src/utils/getPageNumbers.ts

This file was deleted.

35 changes: 0 additions & 35 deletions src/utils/getPagination.ts

This file was deleted.

1 comment on commit aced187

@vercel
Copy link

@vercel vercel bot commented on aced187 Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.