'use client' import { useState } from 'react' import Link from 'next/link' import { linkifyText } from '@/utils/linkify' import ImageCarousel from './ImageCarousel' import RichEmbeds from './RichEmbeds' import ContentHider from './moderation/ContentHider' import { parseSelfLabel } from '@/utils/moderation' export interface Post { id: string content: string author: { username: string displayName: string avatarUrl?: string } likes: number comments: number reposts: number createdAt: string tags?: string[] repoOwner?: string repoName?: string isLiked?: boolean /** Attached image URLs, in display order. */ images?: string[] /** Per-image alt text, parallel to `images` (accessibility). */ imageAlts?: string[] } export interface PostCardProps { post: Post onLike?: (id: string) => void } function formatRelativeTime(dateString: string): string { const date = new Date(dateString) const now = new Date() const diffMs = now.getTime() - date.getTime() const diffSec = Math.floor(diffMs / 1000) const diffMin = Math.floor(diffSec / 60) const diffHr = Math.floor(diffMin / 60) const diffDay = Math.floor(diffHr / 24) if (diffSec < 60) return `${diffSec}s` if (diffMin < 60) return `${diffMin}m` if (diffHr < 24) return `${diffHr}h` if (diffDay < 7) return `${diffDay}d` return date.toLocaleDateString(undefined, { month: 'short', day: 'numeric' }) } interface ContentSegment { type: 'text' | 'code' value: string language?: string } function parseContent(content: string): ContentSegment[] { const segments: ContentSegment[] = [] const fenceRegex = /```(\w*)\n?([\s\S]*?)```/g let lastIndex = 0 let match: RegExpExecArray | null while ((match = fenceRegex.exec(content)) !== null) { if (match.index > lastIndex) { segments.push({ type: 'text', value: content.slice(lastIndex, match.index) }) } segments.push({ type: 'code', language: match[1] || undefined, value: match[2] }) lastIndex = match.index + match[0].length } if (lastIndex < content.length) { segments.push({ type: 'text', value: content.slice(lastIndex) }) } return segments } function RenderedContent({ content }: { content: string }) { const segments = parseContent(content) return (
{segments.map((seg, i) => { if (seg.type === 'code') { return (
              
                {seg.value.replace(/\n$/, '')}
              
            
) } return ( {linkifyText(seg.value, { linkClassName: 'text-[#A78BFA] hover:underline' })} ) })}
) } function HeartIcon({ filled }: { filled: boolean }) { return ( ) } function CommentIcon() { return ( ) } function RepostIcon() { return ( ) } function ShareIcon() { return ( ) } export default function PostCard({ post, onLike }: PostCardProps) { const [liked, setLiked] = useState(post.isLiked ?? false) const [likeCount, setLikeCount] = useState(post.likes) const handleLike = () => { const nextLiked = !liked setLiked(nextLiked) setLikeCount((prev) => (nextLiked ? prev + 1 : prev - 1)) onLike?.(post.id) } const initials = post.author.displayName.trim().charAt(0).toUpperCase() const { label: selfLabel, cleanContent } = parseSelfLabel(post.content || '') return (
{/* Header */}
{/* Avatar */} {post.author.avatarUrl ? ( {post.author.displayName} ) : (
{initials}
)} {/* Author info */}
{post.author.displayName} @{post.author.username} ยท {formatRelativeTime(post.createdAt)}
{/* Repo badge */} {post.repoOwner && post.repoName && ( {post.repoOwner}/{post.repoName} )}
{/* Content + media (soft-hidden behind a warning when self-labelled) */}
{post.images && post.images.length > 0 && (
)}
{/* Tags */} {post.tags && post.tags.length > 0 && (
{post.tags.map((tag) => ( #{tag} ))}
)} {/* Actions */}
{/* Like */} {/* Comments */}
{post.comments}
{/* Reposts */}
{post.reposts}
{/* Share */}
) }