Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
47 lines (40 sloc)
1.25 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Link from 'next/link'; | |
export default function PostFeed({ posts, admin }) { | |
return posts ? posts.map((post) => <PostItem post={post} key={post.slug} admin={admin} />) : null; | |
} | |
function PostItem({ post, admin = false }) { | |
// 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 href={`/${post.username}`}> | |
<a> | |
<strong>By @{post.username}</strong> | |
</a> | |
</Link> | |
<Link href={`/${post.username}/${post.slug}`}> | |
<h2> | |
<a>{post.title}</a> | |
</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 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> | |
); | |
} |