Ampire
import React, { useState } from "react"; import CommentSection from "./CommentSection"; function timeAgo(dateStr) { const seconds = Math.floor((Date.now() - new Date(dateStr).getTime()) / 1000); const units = [
import React, { useState } from "react";
import CommentSection from "./CommentSection";
function timeAgo(dateStr) {
const seconds = Math.floor((Date.now() - new Date(dateStr).getTime()) / 1000);
const units = [
["year", 31536000], ["month", 2592000], ["day", 86400],
["hour", 3600], ["minute", 60],
];
for (const [name, secs] of units) {
const value = Math.floor(seconds / secs);
if (value >= 1) return `${value} ${name}${value > 1 ? "s" : ""} ago`;
}
return "just now";
}
export default function PostCard({ post }) {
const [showComments, setShowComments] = useState(false);
const downloadUrl = `/api/posts/download?postId=${post.id}`;
return (
<article className="post-card">
<header className="post-card-header">
{post.profilePicUrl ? (
<img src={post.profilePicUrl} alt={post.username} className="post-avatar" />
) : (
<div className="post-avatar-fallback">{post.username?.[0]?.toUpperCase()}</div>
)}
<div>
<strong>{post.username}</strong>
<p className="muted small">{timeAgo(post.createdAt)}</p>
</div>
</header>
<div className="post-media">
{post.mediaType === "VIDEO" ? (
<video src={post.mediaUrl} controls />
) : (
<img src={post.mediaUrl} alt={post.caption || "post media"} />
)}
</div>
{post.caption && <p className="post-caption">{post.caption}</p>}
<div className="post-card-actions">
<button onClick={() => setShowComments((s) => !s)}>
{showComments ? "Hide" : "Show"} Comments ({post.commentCount})
</button>
<a href={downloadUrl} download className="download-link">
Download
</a>
</div>
{showComments && <CommentSection postId={post.id} />}
</article>
);
}
📰 Read the original article on Dev.to WebDev
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.