import Link from "next/link"; import { CodeXml, Lock, Copy, Eye, MessageSquare, GitFork } from "lucide-react"; import { fetchSnippetById } from "@/lib/api"; import { postViewerCookieHeader } from "@/lib/cookies"; import { getPublicSettingsCached } from "@/lib/serverData"; import { cookies } from "next/headers"; import { snippetPath } from "@/lib/urlStyle"; import Avatar from "@/components/Avatar"; /** * [snippet id=N] 嵌入卡片(SSR)。 * 私有/已删除/不存在时渲染降级占位,不暴露内容。 */ export default async function MdSnippet({ id }: { id: number }) { const cookieHeader = postViewerCookieHeader(await cookies()); const [data, settings] = await Promise.all([ fetchSnippetById(id, cookieHeader).catch(() => null), getPublicSettingsCached(), ]); const urlStyle = settings.url_style; // 降级占位:不存在 / 私有且无权限 / 已删除 if (!data) { return (

代码片段 #{id}

片段不存在或无权查看

); } const s = data.snippet; const ownerName = s.user ? s.user.nickname || s.user.username : ""; const previewLines = s.code.split("\n").slice(0, 5); const href = snippetPath(urlStyle, s.id); return (
{/* 头部:语言 + 标题 + 作者 */}
{s.language}

{s.title}

{s.user && ( {ownerName} )}
{/* 代码预览 */}
            {previewLines.map((line, i) => (
              
{i + 1} {line || " "}
))}
{/* 底部统计 */}
{s.copy_count} {s.view_count} {s.comment_count} {s.fork_count}
); }