'use client'; import { useState } from 'react'; import Link from 'next/link'; import CodeBlock from './CodeBlock'; // Language badge color map — dark-theme friendly const LANGUAGE_COLORS: Record = { javascript: '#f7df1e', js: '#f7df1e', typescript: '#3178c6', ts: '#3178c6', tsx: '#3178c6', jsx: '#61dafb', python: '#3572a5', py: '#3572a5', go: '#00acd7', golang: '#00acd7', rust: '#dea584', rs: '#dea584', java: '#b07219', c: '#555555', cpp: '#f34b7d', 'c++': '#f34b7d', csharp: '#178600', 'c#': '#178600', cs: '#178600', ruby: '#701516', rb: '#701516', php: '#4f5d95', swift: '#f05138', kotlin: '#7f52ff', kt: '#7f52ff', bash: '#89e051', sh: '#89e051', shell: '#89e051', json: '#292929', yaml: '#cb171e', yml: '#cb171e', sql: '#e38c00', css: '#563d7c', scss: '#c6538c', html: '#e34c26', xml: '#0060ac', markdown: '#083fa1', md: '#083fa1', }; const LINE_PREVIEW_COUNT = 30; interface SnippetAuthor { username: string; avatarUrl?: string; } interface Snippet { id: string; title: string; language: string; code: string; author: SnippetAuthor; likes: number; comments: number; createdAt: string; isLiked?: boolean; } interface CodeSnippetCardProps { snippet: Snippet; onLike?: (id: string) => void; } function formatRelativeTime(dateStr: string): string { const date = new Date(dateStr); const now = new Date(); const diffMs = now.getTime() - date.getTime(); const diffSec = Math.floor(diffMs / 1000); if (diffSec < 60) return `${diffSec}s ago`; const diffMin = Math.floor(diffSec / 60); if (diffMin < 60) return `${diffMin}m ago`; const diffHr = Math.floor(diffMin / 60); if (diffHr < 24) return `${diffHr}h ago`; const diffDay = Math.floor(diffHr / 24); if (diffDay < 30) return `${diffDay}d ago`; const diffMo = Math.floor(diffDay / 30); if (diffMo < 12) return `${diffMo}mo ago`; return `${Math.floor(diffMo / 12)}y ago`; } function LanguageBadge({ language }: { language: string }) { const key = language.toLowerCase(); const color = LANGUAGE_COLORS[key] ?? '#6e7681'; // Decide text color based on lightness of badge background const lightColors = new Set(['#f7df1e', '#89e051', '#27c93f']); const textColor = lightColors.has(color) ? '#1a1a2e' : '#e6edf3'; return ( {language} ); } function AuthorAvatar({ username, avatarUrl }: SnippetAuthor) { const initial = username.charAt(0).toUpperCase(); if (avatarUrl) { return ( // eslint-disable-next-line @next/next/no-img-element {`${username} ); } return (
{initial}
); } export default function CodeSnippetCard({ snippet, onLike }: CodeSnippetCardProps) { const [copied, setCopied] = useState(false); const [expanded, setExpanded] = useState(false); const lines = snippet.code.split('\n'); const isTruncated = lines.length > LINE_PREVIEW_COUNT; const visibleCode = isTruncated && !expanded ? lines.slice(0, LINE_PREVIEW_COUNT).join('\n') : snippet.code; const handleCopy = async () => { try { await navigator.clipboard.writeText(snippet.code); setCopied(true); setTimeout(() => setCopied(false), 2000); } catch (err) { console.error('Failed to copy snippet code:', err); } }; const handleLike = () => { onLike?.(snippet.id); }; return (
{/* ── Header ── */}
@{snippet.author.username}
{/* Copy button in header area */}
{/* ── Title + language badge ── */}

{snippet.title}

{/* ── Code block ── */}
{/* CodeBlock already handles syntax highlighting, line numbers, and its own copy button. We pass maxHeight="none" so the card controls truncation via the "Show more" toggle rather than letting CodeBlock scroll internally. */}
{/* Show more / Show less toggle */} {isTruncated && ( )}
{/* ── Footer ── */}
{/* Left: like + comment counts */}
{/* Like button */} {/* Comment count (display only) */}
{snippet.comments.toLocaleString()}
{/* Right: View Full link */} View Full →
); }