Skip to content

Commit 39e0079

Browse files
committed
年度や月ごとにブログ記事を分けてリストにする機能を実装しました。
なんのため...?
1 parent c7dcfaa commit 39e0079

File tree

6 files changed

+79
-9
lines changed

6 files changed

+79
-9
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { getArticleIndexes, Article } from "../../../article/article";
2+
import ArticleList from "../../../article/components/list";
3+
4+
export default function ArticlePage({ year, month }: { year: string; month: string }) {
5+
const articles: Article[] = getArticleIndexes().filter(article => article.date.trim().startsWith(`${year}-${month}-`));
6+
return <ArticleList articles={articles} />;
7+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use client";
2+
import { useTranslations } from "next-intl";
3+
4+
export default function Explains({ year, month }: { year: string; month: string }) {
5+
const t = useTranslations();
6+
return (
7+
<>
8+
<section className="title">
9+
<h1>{t("pages.article.title") + " of " + year + "-" + month}</h1>
10+
</section>
11+
<section className="description">
12+
<p>{t("pages.article.description.msg1")}</p>
13+
<p>{t("pages.article.description.msg2")}</p>
14+
</section></>)
15+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import "../../article/article.css"; // CSSファイルをインポート
2+
import "../../article/page.css";
3+
import Explains from "./components/explains";
4+
import ArticlePage from "./components/articlepage"; // クライアントコンポーネントをインポート
5+
import { getArticleIndexes } from "@/app/article/article";
6+
7+
export async function generateStaticParams() {
8+
const indexes = getArticleIndexes();
9+
// 静的に生成するパスを返す
10+
return indexes.map((article) => {
11+
const [article_year, month] = article.slug.split("/");
12+
return { article_year, month };
13+
});
14+
}
15+
16+
17+
export default async function Home({
18+
params,
19+
}: {
20+
params: Promise<{ article_year: string; month: string; }>;
21+
}) {
22+
const resolvedParams = await params;
23+
const article_year = parseInt(resolvedParams.article_year.replace("article-", "")).toString();
24+
const month = resolvedParams.month;
25+
return (
26+
<>
27+
<Explains year={article_year} month={month} />
28+
<ArticlePage year={article_year} month={month} />
29+
</>
30+
);
31+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getArticleIndexes, Article } from "../../article/article";
22
import ArticleList from "../../article/components/list";
33

4-
export default function ArticlePage() {
5-
const articles: Article[] = getArticleIndexes();
4+
export default function ArticlePage({ year }: { year: string }) {
5+
const articles: Article[] = getArticleIndexes().filter(article => article.date.trim().startsWith(year));
66
return <ArticleList articles={articles} />;
77
}

src/app/[article_year]/components/explains.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"use client";
22
import { useTranslations } from "next-intl";
33

4-
export default function Explains() {
4+
export default function Explains({ year }: { year: string }) {
55
const t = useTranslations();
66
return (
77
<>
88
<section className="title">
9-
<h1>{t("pages.article.title")}</h1>
9+
<h1>{t("pages.article.title") + " of " + year}</h1>
1010
</section>
1111
<section className="description">
1212
<p>{t("pages.article.description.msg1")}</p>

src/app/[article_year]/page.tsx

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1-
import "./article.css"; // CSSファイルをインポート
2-
import "./page.css";
1+
import "../article/article.css"; // CSSファイルをインポート
2+
import "../article/page.css";
33
import Explains from "./components/explains";
44
import ArticlePage from "./components/articlepage"; // クライアントコンポーネントをインポート
5+
import { getArticleIndexes } from "@/app/article/article";
56

6-
export default function Home() {
7+
export async function generateStaticParams() {
8+
const indexes = getArticleIndexes();
9+
// 静的に生成するパスを返す
10+
return indexes.map((article) => {
11+
const [article_year] = article.slug.split("/");
12+
return { article_year };
13+
});
14+
}
15+
16+
17+
export default async function Home({
18+
params,
19+
}: {
20+
params: Promise<{ article_year: string }>;
21+
}) {
22+
const resolvedParams = await params;
23+
const article_year = parseInt(resolvedParams.article_year.replace("article-", "")).toString();
724
return (
825
<>
9-
<Explains />
10-
<ArticlePage />
26+
<Explains year={article_year} />
27+
<ArticlePage year={article_year} />
1128
</>
1229
);
1330
}

0 commit comments

Comments
 (0)