'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 (
{seg.value.replace(/\n$/, '')}
)
}
return (
{linkifyText(seg.value, { linkClassName: 'text-[#A78BFA] hover:underline' })}
)
})}