Skip to content

Commit ceff2db

Browse files
committed
feat: add comment count
1 parent c332ee4 commit ceff2db

File tree

2 files changed

+70
-11
lines changed

2 files changed

+70
-11
lines changed

modules/home/home.page.tsx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface Post {
99
content: string;
1010
timestamp: string;
1111
author: string;
12+
commentCount?: number;
1213
}
1314

1415
export default function Home({ data }: PageProps<{
@@ -287,11 +288,50 @@ export default function Home({ data }: PageProps<{
287288
{/* Make the content clickable to view details */}
288289
<a href={`/post/${post.id}`} className="block">
289290
<p
290-
className={`${themeStyles.text} whitespace-pre-wrap`}
291+
className={`${themeStyles.text} whitespace-pre-wrap mb-3`}
291292
>
292293
{post.content}
293294
</p>
294295
</a>
296+
297+
{/* Comment count indicator - only shown when comments exist */}
298+
<div className="mt-4 pt-3 border-t border-gray-700/30 flex items-center">
299+
<a
300+
href={`/post/${post.id}`}
301+
className={`flex items-center ${themeStyles.footer} hover:${
302+
themeStyles.link.split(" ")[0]
303+
}`}
304+
>
305+
<svg
306+
xmlns="http://www.w3.org/2000/svg"
307+
width="16"
308+
height="16"
309+
viewBox="0 0 24 24"
310+
fill="none"
311+
stroke="currentColor"
312+
strokeWidth="2"
313+
strokeLinecap="round"
314+
strokeLinejoin="round"
315+
className="mr-1"
316+
>
317+
<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z">
318+
</path>
319+
</svg>
320+
<span>
321+
{post.commentCount
322+
? (
323+
<>
324+
{post.commentCount} {post.commentCount === 1
325+
? "comment"
326+
: "comments"}
327+
</>
328+
)
329+
: (
330+
"Add comment"
331+
)}
332+
</span>
333+
</a>
334+
</div>
295335
</div>
296336
))
297337
)

modules/home/home.service.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ interface Post {
1111
content: string;
1212
timestamp: string;
1313
author: string;
14+
commentCount?: number;
1415
}
1516

1617
export async function createPost(input: PostInput): Promise<Post> {
@@ -37,21 +38,39 @@ export async function createPost(input: PostInput): Promise<Post> {
3738
}
3839

3940
export async function getPosts(limit = 20): Promise<Post[]> {
40-
console.log("Fetching posts, limit:", limit);
41-
const iterator = kv.list<Post>({ prefix: ["posts"] }, { limit });
41+
console.log("Fetching posts");
42+
43+
// Get all posts
44+
const postsResults: Post[] = [];
45+
const iterator = kv.list<Post>({ prefix: ["posts"] });
4246

43-
// Collect all entries from the iterator
44-
const results: Post[] = [];
4547
for await (const entry of iterator) {
46-
results.push(entry.value);
48+
postsResults.push(entry.value);
49+
}
50+
51+
// Get comment counts for each post
52+
const commentCounts = new Map<string, number>();
53+
const commentsIterator = kv.list<Comment>({ prefix: ["comments"] });
54+
55+
for await (const entry of commentsIterator) {
56+
const postId = entry.value.postId;
57+
commentCounts.set(postId, (commentCounts.get(postId) || 0) + 1);
4758
}
4859

60+
// Add comment counts to posts
61+
const postsWithComments = postsResults.map((post) => ({
62+
...post,
63+
commentCount: commentCounts.get(post.id) || 0,
64+
}));
65+
4966
// Sort posts by timestamp (newest first)
50-
const posts = results.sort((a, b) =>
51-
new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime()
52-
);
67+
const posts = postsWithComments
68+
.sort((a, b) =>
69+
new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime()
70+
)
71+
.slice(0, limit);
5372

54-
console.log(`Retrieved ${posts.length} posts from database`);
73+
console.log(`Retrieved ${posts.length} posts`);
5574
return posts;
5675
}
5776

@@ -159,7 +178,7 @@ export async function getCommentsByPostId(postId: string): Promise<Comment[]> {
159178

160179
// Sort comments by timestamp (newest first)
161180
const comments = results.sort((a, b) =>
162-
new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime()
181+
new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime()
163182
);
164183

165184
console.log(`Retrieved ${comments.length} comments for post ${postId}`);

0 commit comments

Comments
 (0)