import type { Metadata } from "next"; import Link from "next/link"; import { notFound } from "next/navigation"; import messages from "../../../messages/en/index"; import communityStructure from "../../../messages/en/create/community/communityStructure.json"; import { routeMetadata } from "../../../lib/siteMetadata"; import { getRuleAdminEmails, isDatabaseConfigured } from "../../../lib/server/env"; import { prisma } from "../../../lib/server/db"; import { isRuleAdminEmail } from "../../../lib/server/ruleAdmins"; import { aggregateRuleLaunchCounts } from "../../../lib/server/ruleLaunchCounts"; import { getSessionUser } from "../../../lib/server/session"; export const metadata: Metadata = routeMetadata("/rule-counts", { title: messages.metadata.ruleCounts.title, robots: { index: false, follow: false }, }); const ORG_LABELS = Object.fromEntries( communityStructure.organizationTypes.map((row) => [row.id, row.label]), ); function typeLabel(orgType: string | null, folded: boolean, copy: typeof messages.pages.ruleCounts): string { if (folded) return copy.folded; if (!orgType) return copy.notGiven; return ORG_LABELS[orgType] ?? orgType; } function placeLabel(place: string | null, folded: boolean, copy: typeof messages.pages.ruleCounts): string { if (folded) return copy.folded; if (!place) return copy.notGiven; return place; } export default async function RuleCountsPage() { if (getRuleAdminEmails().length === 0) notFound(); const copy = messages.pages.ruleCounts; const user = await getSessionUser(); if (!user) { return (

{copy.title}

{copy.signIn}

{copy.signInButton}
); } if (!isRuleAdminEmail(user.email)) { return (

{copy.title}

{copy.forbidden}

); } const rows = isDatabaseConfigured() ? await prisma.publishedRule.findMany({ select: { orgType: true, communityLocation: true }, }) : []; const counts = aggregateRuleLaunchCounts( rows.map((row) => ({ orgType: row.orgType, place: row.communityLocation, })), ); return (

{copy.title}

{copy.intro}

{copy.finished}: {counts.finished}

{counts.cells.length === 0 ? (

{copy.empty}

) : ( {counts.cells.map((cell) => ( ))}
{copy.columns.type} {copy.columns.place} {copy.columns.count}
{typeLabel(cell.orgType, cell.folded, copy)} {placeLabel(cell.place, cell.folded, copy)} {cell.count}
)}
); }