Skip to content

Commit

Permalink
Group thoughts by year
Browse files Browse the repository at this point in the history
  • Loading branch information
bhekanik committed Mar 3, 2024
1 parent 2110e1d commit 1486529
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/components/ThoughtCard.astro
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const {
>
{format(date, "MMMM dd, yyyy")}
</time>
&middot;
<!-- &middot; -->
<ShowViews slug={slug} />
</div>
<ThoughtDescription description={title} slug={slug} />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
---
title: "How to remove file from Git history"
pubDate: 2024-01-08
date: 2024-01-08
published: true
author: "Bhekani Khumalo"
tags: ["git"]
---

I have googled this enough times for me to keep it in my own notes.
Expand Down
56 changes: 40 additions & 16 deletions src/pages/thoughts/index.astro
Original file line number Diff line number Diff line change
@@ -1,34 +1,58 @@
---
import { getCollection } from "astro:content";
import AllTags from "../../components/AllTags.astro";
import LatestThoughtCard from "../../components/LatestThoughtCard.astro";
import MainContainer from "../../components/MainContainer.astro";
import ThoughtCard from "../../components/ThoughtCard.astro";
import BaseLayout from "../../layouts/BaseLayout.astro";
import { getCollection, type CollectionEntry } from "astro:content"
import AllTags from "../../components/AllTags.astro"
import MainContainer from "../../components/MainContainer.astro"
import ThoughtCard from "../../components/ThoughtCard.astro"
import BaseLayout from "../../layouts/BaseLayout.astro"
const allThoughts = await getCollection("thoughts");
const allThoughts = await getCollection("thoughts")
const sortedThoughts = allThoughts.sort(
(a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf()
);
const firstThought = sortedThoughts.shift();
const pageTitle = "Thoughts";
(a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf(),
)
// const firstThought = sortedThoughts.shift()
const yearToThoughts = sortedThoughts.reduce<
Record<number, CollectionEntry<"thoughts">[]>
>((acc, thought) => {
const year = new Date(thought.data.pubDate).getFullYear()
if (!acc[year]) {
acc[year] = []
}
acc[year].push(thought)
return acc
}, {})
const pageTitle = "Thoughts"
---

<BaseLayout pageTitle={pageTitle}>
<MainContainer>
<div class="w-full flex justify-center mb-8 font-bold">
<a
class="px-3 p-2 text-[hsl(var(--foreground))] border-b-[1px] border-l-[1px] border-r-[1px] border-[hsl(var(--muted))]"
class="px-3 p-2 text- border-b-[1px] border-l-[1px] border-r-[1px] border-[hsl(var(--muted))]"
href="/rss.xml"
>
Subscribe to RSS
</a>
</div>

<AllTags title="Filter by tag" />
{firstThought && <LatestThoughtCard thought={firstThought} />}
<div class="grid gap-4">
{sortedThoughts.map((thought) => <ThoughtCard thought={thought} />)}
</div>
<!-- {firstThought && <LatestThoughtCard thought={firstThought} />} -->

{
Object.entries(yearToThoughts)
.sort((a, b) => Number(b[0]) - Number(a[0]))
.map(([year, thoughts]) => (
<>
<h2 class="text-2xl mb-4 border-b-2 mt-8 pb-2">
{year} ({thoughts.length})
</h2>
<div class="grid gap-4">
{thoughts.map((thought) => (
<ThoughtCard thought={thought} />
))}
</div>
</>
))
}
</MainContainer>
</BaseLayout>

0 comments on commit 1486529

Please sign in to comment.