50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
import type { Metadata } from "next";
|
|
import { fetchSnippets, fetchSnippetLanguages, fetchSnippetStats, type SnippetStats } from "@/lib/api";
|
|
import { getPublicSettingsCached } from "@/lib/serverData";
|
|
import { requestCanonical } from "@/lib/canonicalMeta";
|
|
import { snippetsPath } from "@/lib/urlStyle";
|
|
import SnippetExplorer from "@/components/SnippetExplorer";
|
|
|
|
export async function generateMetadata(): Promise<Metadata> {
|
|
const settings = await getPublicSettingsCached();
|
|
return {
|
|
title: "代码片段",
|
|
description: settings.site_description,
|
|
...(await requestCanonical(snippetsPath(settings.url_style))),
|
|
};
|
|
}
|
|
|
|
export default async function SnippetsPage() {
|
|
const settings = await getPublicSettingsCached();
|
|
|
|
let initialItems: Awaited<ReturnType<typeof fetchSnippets>>["items"] = [];
|
|
let initialTotal = 0;
|
|
let languages: string[] = [];
|
|
let stats: SnippetStats | null = null;
|
|
try {
|
|
const [listRes, langRes, statsRes] = await Promise.all([
|
|
fetchSnippets({ size: 12 }),
|
|
fetchSnippetLanguages(),
|
|
fetchSnippetStats(),
|
|
]);
|
|
initialItems = listRes.items;
|
|
initialTotal = listRes.total;
|
|
languages = langRes.languages;
|
|
stats = statsRes;
|
|
} catch {
|
|
/* 降级空态 */
|
|
}
|
|
|
|
return (
|
|
<div className="min-w-0 pt-5 sm:pt-7">
|
|
<SnippetExplorer
|
|
initialItems={initialItems}
|
|
initialTotal={initialTotal}
|
|
languages={languages}
|
|
urlStyle={settings.url_style}
|
|
stats={stats}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|