-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPostFeed.tsx
45 lines (38 loc) · 1.56 KB
/
PostFeed.tsx
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
35
36
37
38
39
40
41
42
43
44
45
import Link from 'next/link';
export default function PostFeed({ posts, admin = false }: { posts: any[], admin?: boolean }) {
return posts && posts.length ? <>{posts.map((post: any, i: number) => <PostItem post={post} key={i} admin={admin} />)}</> : <></>;
}
function PostItem({ post, admin }: { post: any, admin: boolean }) {
// Naive method to calc word count and read time
const wordCount = post?.content.trim().split(/\s+/g).length;
const minutesToRead = (wordCount / 100 + 1).toFixed(0);
return (
<div className="card">
<Link passHref href={`/${post.username}`}>
<strong>By @{post.username}</strong>
</Link>
<Link passHref href={`/${post.username}/${post.slug}`}>
<h2>
{post.title}
</h2>
</Link>
<footer>
<span>
{wordCount} words. {minutesToRead} min read
</span>
<span className="push-left">💗 {post.heartCount || 0} Hearts</span>
</footer>
{/* If admin view, show extra controls for user */}
{admin && (
<>
<Link passHref href={`/admin/${post.slug}`}>
<h3>
<button className="btn-blue">Edit</button>
</h3>
</Link>
{post.published ? <p className="text-success">Live</p> : <p className="text-danger">Unpublished</p>}
</>
)}
</div>
);
}