import type { ReactNode } from 'react' import type { Post } from '../types' import { PostMedia } from './post-media' const URL = /(https?:\/\/[^\s]+)/g const dateFormatter = new Intl.DateTimeFormat('ja-JP', { dateStyle: 'medium', timeStyle: 'short', timeZone: 'Asia/Tokyo', }) function linkedText(text: string): ReactNode[] { const parts: ReactNode[] = [] let cursor = 0 for (const match of text.matchAll(URL)) { const href = match[0] const start = match.index if (start > cursor) parts.push(text.slice(cursor, start)) parts.push( {href} , ) cursor = start + href.length } if (cursor < text.length) parts.push(text.slice(cursor)) return parts } function formattedTime(createdAt?: string): string | undefined { if (!createdAt) return undefined const date = new Date(createdAt) return Number.isNaN(date.getTime()) ? undefined : dateFormatter.format(date) } export function PostCard({ post, quoted = false, }: { post: Post quoted?: boolean }) { const time = formattedTime(post.createdAt) const original = `https://x.com/${encodeURIComponent(post.author.username)}/status/${encodeURIComponent(post.id)}` return (
{post.author.profileImageUrl ? ( ) : null}
{post.author.name} @{post.author.username}
{time && post.createdAt ? ( ) : null}

{linkedText(post.text)}

{post.media ? : null} {post.article ? (
{post.article.title} {post.article.previewText ?

{post.article.previewText}

: null}
) : null} {post.quotedTweet && !quoted ? ( ) : null} {!quoted ? ( ) : null}
) }