111 lines
3.2 KiB
TypeScript
111 lines
3.2 KiB
TypeScript
import type { Metadata } from "next";
|
|
import { headers } from "next/headers";
|
|
import { notFound } from "next/navigation";
|
|
import messages from "../../../../messages/en/index";
|
|
import { isShareSlug } from "../../../../lib/sharePaths";
|
|
import {
|
|
SHARE_LOOKUP_MISS_INTERVAL_MS,
|
|
getReadablePublishedRule,
|
|
} from "../../../../lib/server/publishedRules";
|
|
import { rateLimitKey } from "../../../../lib/server/rateLimit";
|
|
import { getSessionUser } from "../../../../lib/server/session";
|
|
import { parsePublishedDocumentForCommunityRuleDisplay } from "../../../../lib/create/publishedDocumentToDisplaySections";
|
|
import { NO_INDEX_ROBOTS, routeMetadata } from "../../../../lib/siteMetadata";
|
|
import CommunityRule from "../../../components/type/CommunityRule";
|
|
import HeaderLockup from "../../../components/type/HeaderLockup";
|
|
|
|
interface PageProps {
|
|
params: Promise<{ shareSlug: string }>;
|
|
}
|
|
|
|
async function clientIp(): Promise<string> {
|
|
const headerStore = await headers();
|
|
return (
|
|
headerStore.get("x-forwarded-for")?.split(",")[0]?.trim() ||
|
|
headerStore.get("x-real-ip") ||
|
|
"unknown"
|
|
);
|
|
}
|
|
|
|
async function loadSharedRule(shareSlug: string) {
|
|
const ip = await clientIp();
|
|
if (!isShareSlug(shareSlug)) {
|
|
const limited = rateLimitKey(
|
|
`share-lookup-miss:${ip}`,
|
|
SHARE_LOOKUP_MISS_INTERVAL_MS,
|
|
);
|
|
if (!limited.ok) return null;
|
|
return null;
|
|
}
|
|
const user = await getSessionUser();
|
|
return getReadablePublishedRule({
|
|
idOrSlug: shareSlug,
|
|
user,
|
|
viaShareSlug: true,
|
|
requestPath: `/r/${shareSlug}`,
|
|
failedLookupKey: ip,
|
|
});
|
|
}
|
|
|
|
export async function generateMetadata({
|
|
params,
|
|
}: PageProps): Promise<Metadata> {
|
|
const { shareSlug } = await params;
|
|
const resolved = await loadSharedRule(shareSlug);
|
|
if (!resolved) {
|
|
notFound();
|
|
}
|
|
const { rule } = resolved;
|
|
const description =
|
|
typeof rule.summary === "string" && rule.summary.trim().length > 0
|
|
? rule.summary
|
|
: undefined;
|
|
return routeMetadata(`/r/${shareSlug}`, {
|
|
title: rule.title,
|
|
description,
|
|
robots: NO_INDEX_ROBOTS,
|
|
openGraph: {
|
|
title: rule.title,
|
|
description,
|
|
type: "article",
|
|
url: `/r/${shareSlug}`,
|
|
siteName: messages.metadata.siteName,
|
|
},
|
|
twitter: {
|
|
card: "summary_large_image",
|
|
title: rule.title,
|
|
description,
|
|
},
|
|
});
|
|
}
|
|
|
|
export default async function SharedRuleDetailPage({ params }: PageProps) {
|
|
const { shareSlug } = await params;
|
|
const resolved = await loadSharedRule(shareSlug);
|
|
if (!resolved) {
|
|
notFound();
|
|
}
|
|
const { rule } = resolved;
|
|
|
|
const sections = parsePublishedDocumentForCommunityRuleDisplay(rule.document);
|
|
const description =
|
|
typeof rule.summary === "string" && rule.summary.trim().length > 0
|
|
? rule.summary
|
|
: undefined;
|
|
|
|
return (
|
|
<article className="min-h-screen bg-[var(--color-teal-teal50,#c9fef9)]">
|
|
<div className="mx-auto flex max-w-[1200px] flex-col gap-[var(--space-1200)] px-5 py-[var(--space-1200)] md:px-12 md:py-[var(--space-1800)]">
|
|
<HeaderLockup
|
|
title={rule.title}
|
|
description={description}
|
|
justification="left"
|
|
size="L"
|
|
palette="inverse"
|
|
/>
|
|
<CommunityRule sections={sections} />
|
|
</div>
|
|
</article>
|
|
);
|
|
}
|