summaryrefslogtreecommitdiff
path: root/src/components/BlogPosts.astro
blob: 54b75297ec27e0b368dfa49daa2d8c1831ff5dc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
---
import { getCollection } from 'astro:content';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";

// Fetch all blog posts from the content collection
const posts = await getCollection('blog');

// Sort posts by date (newest first)
const sortedPosts = posts.sort((a, b) => 
  new Date(b.data.publishDate).getTime() - new Date(a.data.publishDate).getTime()
);
---

<div class="w-full max-w-2xl mx-auto p-4">
    <h1 class="text-3xl font-bold mb-8">Blog Posts</h1>
    <div class="space-y-4 max-h-96 overflow-y-auto">
        {sortedPosts.map((post) => (
            <a href={`/posts/${post.id}`} class="block hover:opacity-80 transition-opacity">
                <Card>
                    <CardHeader>
                        <CardTitle>{post.data.title}</CardTitle>
                        <CardDescription>{post.data.description}</CardDescription>
                    </CardHeader>
                    <CardContent>
                        <div class="flex justify-between text-sm text-gray-500">
                            <span>{post.data.publishDate.toLocaleDateString()}</span>
                            <span>by {post.data.author}</span>
                        </div>
                    </CardContent>
                </Card>
            </a>
        ))}
    </div>
</div>