Skip to content

Commit ad1b5d1

Browse files
committed
feat: add "updated" tab and sort tag groups by last updated date
- closes issue #20
1 parent 8bdfcea commit ad1b5d1

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

pages/post.tsx

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,40 @@ type Props = {
2020
tagCounts: Record<string, number>;
2121
};
2222

23+
const byDesc = (a: string | undefined, b: string | undefined) =>
24+
new Date(b ?? '1970-01-01').getTime() - new Date(a ?? '1970-01-01').getTime();
25+
2326
export const getStaticProps: GetStaticProps<Props> = async () => {
2427
const posts = getPostsMetaOnly();
2528

29+
const byCreatedDesc = [...posts].sort((a, b) => byDesc(a.date, b.date));
30+
const byUpdatedDesc = [...posts].sort(
31+
(a, b) => byDesc(a.updated ?? a.date, b.updated ?? b.date)
32+
);
33+
2634
const postsByTag: Record<string, Post[]> = {};
2735
const tagCounts: Record<string, number> = {};
2836

2937
postsByTag['latest'] = posts;
3038
tagCounts['latest'] = posts.length;
3139

40+
postsByTag['updated'] = byUpdatedDesc;
41+
tagCounts['updated'] = byUpdatedDesc.length;
42+
43+
const grouped: Record<string, Post[]> = {};
3244
posts.forEach((post) => {
33-
(post.tags || []).forEach((tag) => {
34-
if (!postsByTag[tag]) postsByTag[tag] = [];
35-
postsByTag[tag].push(post);
36-
tagCounts[tag] = (tagCounts[tag] || 0) + 1;
45+
(post.tags ?? []).forEach((tag) => {
46+
if (!grouped[tag]) grouped[tag] = [];
47+
grouped[tag].push(post);
3748
});
3849
});
3950

51+
Object.keys(grouped).forEach((tag) => {
52+
grouped[tag].sort((a, b) => byDesc(a.updated ?? a.date, b.updated ?? b.date));
53+
postsByTag[tag] = grouped[tag];
54+
tagCounts[tag] = grouped[tag].length;
55+
});
56+
4057
return {
4158
props: { postsByTag, tagCounts },
4259
};
@@ -86,10 +103,14 @@ export default function PostPage({ postsByTag, tagCounts }: Props) {
86103

87104
const title = selectedTag === "latest"
88105
? "Latest Posts"
106+
: selectedTag === "updated"
107+
? "Recently Updated"
89108
: `${selectedTag.charAt(0).toUpperCase() + selectedTag.slice(1)} Posts`;
90109

91110
const description = selectedTag === "latest"
92111
? "Check out the latest posts — and filter by tag if you'd like."
112+
: selectedTag === "updated"
113+
? "Posts sorted by last updated timestamp (falls back to created date)."
93114
: `Posts related to the '${selectedTag}' category.`;
94115

95116
const gradientOptions = [
@@ -111,9 +132,11 @@ export default function PostPage({ postsByTag, tagCounts }: Props) {
111132
}, [selectedTag]);
112133

113134
// 태그 목록
135+
const otherTags = Object.keys(tagCounts).filter(t => t !== 'latest' && t !== 'updated');
114136
const tagList = [
115-
"latest",
116-
...Object.keys(tagCounts).filter(tag => tag !== "latest").sort((a, b) => a.localeCompare(b)),
137+
'latest',
138+
'updated',
139+
...otherTags.sort((a, b) => a.localeCompare(b))
117140
];
118141

119142
// 태그 변경 시 검색/페이지 초기화

0 commit comments

Comments
 (0)