diff --git a/app/writing/page.tsx b/app/writing/page.tsx index 47b8d3e..4256281 100644 --- a/app/writing/page.tsx +++ b/app/writing/page.tsx @@ -1,10 +1,11 @@ -import React from 'react' +import React, {ReactNode} from 'react' import {SimpleLayout} from '@/components/layouts/SimpleLayout' +import {groupArticlesByYear} from '@/lib/getAllArticles' +import {metadata as _metadata} from '@/lib/generateMetadata' +import {Section} from '@/components/ui/Section' +import {Article} from '@/types' import {Card} from '@/components/ui/Card' import {Views} from '@/components/ui/Views' -import {getAllArticles} from '@/lib/getAllArticles' -import {metadata as _metadata} from '@/lib/generateMetadata' -import type {Article} from '@/types' import {format} from 'date-fns' const meta = { @@ -33,31 +34,39 @@ export let metadata: { metadata = _metadata({...meta, heading: meta.heading}) -function Article({article}: { article: Article }) { +function ArticleSection({year, children}: { year: string, children: ReactNode }) { return ( -
- - - {article.title} - -

- - {format(article.date, 'd MMMM yyyy')} - - -

-
-
+
+ {children} +
+ ) +} + +function ArticleList({articles}: { articles: Article[] }) { + return ( + ) } export default async function Writing() { - const articles = (await getAllArticles()).map(({component, ...meta}) => meta) + const groupedArticles = await groupArticlesByYear() return ( -
-
- {articles.map((article) => ( -
- ))} -
+
+ {Object.entries(groupedArticles) + .sort(([a], [b]) => parseInt(b) - parseInt(a)) + .map(([year, articles]) => ( + + + + ) + ) + }
) diff --git a/lib/getAllArticles.ts b/lib/getAllArticles.ts index 31d91cb..4e8264b 100644 --- a/lib/getAllArticles.ts +++ b/lib/getAllArticles.ts @@ -1,5 +1,6 @@ import glob from 'fast-glob' import * as path from 'path' +import {Article} from '@/types' async function importArticle(articleFilename: string) { let {meta, default: component} = await import( @@ -22,3 +23,16 @@ export async function getAllArticles(dateDesc = true) { return dateDesc ? articles.sort((a, z) => a.date < z.date ? 1 : -1) : articles.sort((a, z) => a.date > z.date ? 1 : -1) } + +export async function groupArticlesByYear() { + return (await getAllArticles()) + .map(({component, ...meta}) => meta) + .reduce<{ [year: string]: Article[] }>((acc, article) => { + const year = new Date(article.date).getFullYear() + if (!acc[year]) { + acc[year] = [] + } + acc[year].push(article) + return acc + }, {}) +}