Files
PolyWeather/frontend/app/sources/[slug]/page.tsx
T

123 lines
3.1 KiB
TypeScript
Raw Normal View History

2026-06-24 16:41:13 +08:00
import type { Metadata } from "next";
2026-06-24 18:53:22 +08:00
import { cookies, headers } from "next/headers";
2026-06-24 16:41:13 +08:00
import { notFound } from "next/navigation";
import { SourceDetailPageView } from "@/components/public-content/PublicContentPages";
2026-06-24 18:53:22 +08:00
import {
LANDING_LOCALE_COOKIE,
LANDING_LOCALE_QUERY_PARAM,
pickLandingLocale,
type LandingLocale,
} from "@/components/landing/landingLocale";
2026-06-24 16:41:13 +08:00
import {
SOURCE_PAGES,
absolutePublicUrl,
getSourcePage,
2026-06-24 18:53:22 +08:00
localizeSourcePage,
2026-06-24 16:41:13 +08:00
sourcePath,
} from "@/content/public-content";
type SourcePageParams = {
slug: string;
};
2026-06-24 18:53:22 +08:00
type SourceSearchParams = Promise<Record<string, string | string[] | undefined>>;
async function resolvePublicContentLocale(searchParams: SourceSearchParams): Promise<LandingLocale> {
const params = await searchParams;
const rawLocale = params[LANDING_LOCALE_QUERY_PARAM];
const queryLocale = Array.isArray(rawLocale) ? rawLocale[0] : rawLocale;
const [cookieStore, headerStore] = await Promise.all([cookies(), headers()]);
return pickLandingLocale(
queryLocale,
cookieStore.get(LANDING_LOCALE_COOKIE)?.value,
headerStore.get("accept-language"),
);
}
2026-06-24 16:41:13 +08:00
export function generateStaticParams() {
return SOURCE_PAGES.map((source) => ({ slug: source.slug }));
}
export async function generateMetadata({
params,
2026-06-24 18:53:22 +08:00
searchParams,
2026-06-24 16:41:13 +08:00
}: {
params: Promise<SourcePageParams>;
2026-06-24 18:53:22 +08:00
searchParams: SourceSearchParams;
2026-06-24 16:41:13 +08:00
}): Promise<Metadata> {
2026-06-24 18:53:22 +08:00
const [{ slug }, locale] = await Promise.all([
params,
resolvePublicContentLocale(searchParams),
]);
2026-06-24 16:41:13 +08:00
const source = getSourcePage(slug);
if (!source) {
return {
title: "Source not found",
};
}
2026-06-24 18:53:22 +08:00
const localizedSource = localizeSourcePage(source, locale);
2026-06-24 16:41:13 +08:00
return {
2026-06-24 18:53:22 +08:00
title: localizedSource.title,
description: localizedSource.description,
2026-06-24 16:41:13 +08:00
alternates: {
canonical: sourcePath(source),
},
openGraph: {
type: "article",
2026-06-24 18:53:22 +08:00
title: localizedSource.title,
description: localizedSource.description,
2026-06-24 16:41:13 +08:00
url: sourcePath(source),
modifiedTime: source.updatedAt,
},
};
}
export default async function SourceDetailPage({
params,
2026-06-24 18:53:22 +08:00
searchParams,
2026-06-24 16:41:13 +08:00
}: {
params: Promise<SourcePageParams>;
2026-06-24 18:53:22 +08:00
searchParams: SourceSearchParams;
2026-06-24 16:41:13 +08:00
}) {
2026-06-24 18:53:22 +08:00
const [{ slug }, locale] = await Promise.all([
params,
resolvePublicContentLocale(searchParams),
]);
2026-06-24 16:41:13 +08:00
const source = getSourcePage(slug);
if (!source) {
notFound();
}
2026-06-24 18:53:22 +08:00
const localizedSource = localizeSourcePage(source, locale);
2026-06-24 16:41:13 +08:00
const pathname = sourcePath(source);
const jsonLd = {
"@context": "https://schema.org",
"@type": "Dataset",
2026-06-24 18:53:22 +08:00
name: localizedSource.title,
description: localizedSource.description,
2026-06-24 16:41:13 +08:00
url: absolutePublicUrl(pathname),
dateModified: source.updatedAt,
creator: {
"@type": "Organization",
name: source.operator,
},
includedInDataCatalog: {
"@type": "DataCatalog",
name: "PolyWeather source notes",
url: absolutePublicUrl("/sources"),
},
};
return (
<>
<script
type="application/ld+json"
dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
/>
2026-06-24 18:53:22 +08:00
<SourceDetailPageView source={source} locale={locale} />
2026-06-24 16:41:13 +08:00
</>
);
}