Show Reddit post age in conversations
This commit is contained in:
1 parent
7db60e8839
commit
50f9f8a6aa
14 files changed
+65
-11
No files matched your search
@@ -5,6 +5,32 @@ export interface OriginPost {
|
||||
url: string;
|
||||
imageUrl?: string | null;
|
||||
subreddit?: string;
|
||||
postedAt?: number;
|
||||
}
|
||||
|
||||
export function formatPostAge(postedAt: number, now = Date.now()): string {
|
||||
const elapsedSeconds = Math.max(0, Math.floor((now - postedAt) / 1000));
|
||||
if (elapsedSeconds < 60) return 'now';
|
||||
const elapsedMinutes = Math.floor(elapsedSeconds / 60);
|
||||
if (elapsedMinutes < 60) return `${elapsedMinutes}m ago`;
|
||||
const elapsedHours = Math.floor(elapsedMinutes / 60);
|
||||
if (elapsedHours < 24) return `${elapsedHours}h ago`;
|
||||
const elapsedDays = Math.floor(elapsedHours / 24);
|
||||
if (elapsedDays < 30) return `${elapsedDays}d ago`;
|
||||
const elapsedMonths = Math.floor(elapsedDays / 30);
|
||||
if (elapsedMonths < 12) return `${elapsedMonths}mo ago`;
|
||||
return `${Math.floor(elapsedMonths / 12)}y ago`;
|
||||
}
|
||||
|
||||
function PostOriginMeta({ post }: { post: OriginPost }) {
|
||||
return (
|
||||
<small>
|
||||
Started from {post.subreddit ? `r/${post.subreddit}` : 'Reddit'}
|
||||
{post.postedAt ? (
|
||||
<> · <time dateTime={new Date(post.postedAt).toISOString()} title={new Date(post.postedAt).toLocaleString()}>{formatPostAge(post.postedAt)}</time></>
|
||||
) : null}
|
||||
</small>
|
||||
);
|
||||
}
|
||||
|
||||
export function OriginPostCard({ post }: { post: OriginPost }) {
|
||||
@@ -21,7 +47,7 @@ export function OriginPostCard({ post }: { post: OriginPost }) {
|
||||
</div>
|
||||
) : null}
|
||||
<a aria-label={post.title} className="origin-post-copy" href={post.url} rel="noreferrer" target="_blank">
|
||||
<small>Started from {post.subreddit ? `r/${post.subreddit}` : 'Reddit'}</small>
|
||||
<PostOriginMeta post={post} />
|
||||
<strong>{post.title}</strong>
|
||||
<span>Open original post</span>
|
||||
</a>
|
||||
@@ -69,7 +95,7 @@ export function OriginPostButton({ children, post }: { children: ReactNode; post
|
||||
</div>
|
||||
{post.imageUrl ? <img alt={`Source post: ${post.title}`} loading="lazy" src={post.imageUrl} /> : null}
|
||||
<div className="origin-post-dialog-copy">
|
||||
{post.subreddit ? <small>r/{post.subreddit}</small> : null}
|
||||
<PostOriginMeta post={post} />
|
||||
<h2 id="origin-post-dialog-title">{post.title}</h2>
|
||||
<a className="button primary" href={post.url} rel="noreferrer" target="_blank">Open Reddit post</a>
|
||||
</div>
|
||||
|
||||
@@ -229,6 +229,7 @@ export function ConversationDetail({ conversationId, onClose }: ConversationDeta
|
||||
url: conversation.triggeringPost.url,
|
||||
imageUrl: conversation.triggeringPost.imageUrl,
|
||||
subreddit: conversation.triggeringPost.subreddit,
|
||||
postedAt: conversation.triggeringPost.postedAt,
|
||||
}} />
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
Reference in new issue
Block a user