| 1 | import type { Metadata } from "next"; |
| 2 | import { cookies } from "next/headers"; |
| 3 | import { getAgentEnv, listDrafts, validateSession, type AgentDraft } from "@/lib/community-agent"; |
| 4 | import { AdminClient } from "./admin-client"; |
| 5 | |
| 6 | export const dynamic = "force-dynamic"; |
| 7 | |
| 8 | // Maintainer-only surface: keep it out of search indexes (robots.ts also |
| 9 | // disallows /*/admin). |
| 10 | export const metadata: Metadata = { |
| 11 | robots: { index: false, follow: false }, |
| 12 | }; |
| 13 | |
| 14 | const TYPE_LABELS: Record<string, { en: string; zh: string }> = { |
| 15 | triage: { en: "Issue Triage", zh: "议题分类" }, |
| 16 | "pr-review": { en: "PR Review", zh: "PR 审阅" }, |
| 17 | stale: { en: "Stale Nudge", zh: "过期提醒" }, |
| 18 | dupes: { en: "Duplicate", zh: "重复检测" }, |
| 19 | digest: { en: "Weekly Digest", zh: "每周摘要" }, |
| 20 | linkcheck: { en: "Broken Link", zh: "失效链接" }, |
| 21 | "semantic-drift": { en: "Content Drift", zh: "内容漂移" }, |
| 22 | }; |
| 23 | |
| 24 | function LoginForm({ locale, error }: { locale: string; error: boolean }) { |
| 25 | const isZh = locale === "zh"; |
| 26 | return ( |
| 27 | <div className="mx-auto max-w-md px-6 py-20"> |
| 28 | <h1 className="font-display text-3xl mb-6"> |
| 29 | {isZh ? "维护者登录" : "Maintainer login"} |
| 30 | </h1> |
| 31 | <form method="POST" action={`/api/admin/login?locale=${locale}`} autoComplete="off" className="space-y-4"> |
| 32 | <input type="hidden" name="locale" value={locale} /> |
| 33 | <label className="block"> |
| 34 | <span className="eyebrow block mb-2">{isZh ? "令牌" : "Token"}</span> |
| 35 | <input |
| 36 | type="password" |
| 37 | name="token" |
| 38 | required |
| 39 | autoFocus |
| 40 | autoComplete="off" |
| 41 | spellCheck={false} |
| 42 | className="w-full px-3 py-2 hairline-t hairline-b hairline-l hairline-r bg-paper font-mono text-sm focus:outline-none focus:border-indigo" |
| 43 | /> |
| 44 | </label> |
| 45 | <button |
| 46 | type="submit" |
| 47 | className="w-full px-5 py-3 bg-ink text-paper font-mono text-sm uppercase tracking-wider hover:bg-indigo transition-colors" |
| 48 | > |
| 49 | {isZh ? "登录 →" : "Sign in →"} |
| 50 | </button> |
| 51 | {error && ( |
| 52 | <p className="text-sm text-indigo font-mono"> |
| 53 | {isZh ? "令牌错误。" : "Invalid token."} |
| 54 | </p> |
| 55 | )} |
| 56 | </form> |
| 57 | </div> |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | export default async function AdminPage({ |
| 62 | params, |
| 63 | searchParams, |
| 64 | }: { |
| 65 | params: Promise<{ locale: string }>; |
| 66 | searchParams: Promise<{ err?: string }>; |
| 67 | }) { |
| 68 | const { locale } = await params; |
| 69 | const { err } = await searchParams; |
| 70 | const isZh = locale === "zh"; |
| 71 | |
| 72 | const env = await getAgentEnv(); |
| 73 | |
| 74 | if (!env.MAINTAINER_TOKEN) { |
| 75 | return ( |
| 76 | <div className="mx-auto max-w-[1400px] px-6 py-20 text-center"> |
| 77 | <h1 className="font-display text-3xl mb-4">{isZh ? "未配置" : "Not configured"}</h1> |
| 78 | <p className="text-ink-soft"> |
| 79 | {isZh |
| 80 | ? "MAINTAINER_TOKEN 未设置。请在部署前配置此环境变量。" |
| 81 | : "MAINTAINER_TOKEN is not set. Configure this secret before deployment."} |
| 82 | </p> |
| 83 | </div> |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | const cookieStore = await cookies(); |
| 88 | const sid = cookieStore.get("mt_sid")?.value; |
| 89 | const authed = await validateSession(env.CURATED_KV, sid); |
| 90 | |
| 91 | if (!authed) { |
| 92 | return <LoginForm locale={locale} error={err === "1"} />; |
| 93 | } |
| 94 | |
| 95 | let drafts: AgentDraft[] = []; |
| 96 | try { |
| 97 | drafts = await listDrafts(env.CURATED_KV); |
| 98 | } catch (e) { |
| 99 | console.error("failed to list drafts", e); |
| 100 | } |
| 101 | |
| 102 | const pending = drafts.filter((d) => !d.posted); |
| 103 | const posted = drafts.filter((d) => d.posted); |
| 104 | |
| 105 | return ( |
| 106 | <section className="mx-auto max-w-[1400px] px-6 pt-12 pb-20"> |
| 107 | <div className="flex items-baseline justify-between mb-8 hairline-b pb-4"> |
| 108 | <div> |
| 109 | <h1 className="font-display tracking-crisp text-3xl"> |
| 110 | {isZh ? "社区助理草稿" : "Community Assistant Drafts"} |
| 111 | </h1> |
| 112 | <p className="mt-2 text-sm text-ink-mute font-mono"> |
| 113 | {pending.length} pending · {posted.length} posted |
| 114 | </p> |
| 115 | </div> |
| 116 | <form method="POST" action={`/api/admin/logout?locale=${locale}`}> |
| 117 | <button |
| 118 | type="submit" |
| 119 | className="font-mono text-xs text-ink-mute hover:text-indigo uppercase tracking-wider" |
| 120 | > |
| 121 | {isZh ? "退出 →" : "Sign out →"} |
| 122 | </button> |
| 123 | </form> |
| 124 | </div> |
| 125 | |
| 126 | {pending.length === 0 && posted.length === 0 && ( |
| 127 | <div className="hairline-t hairline-b py-16 text-center"> |
| 128 | <div className="font-cjk text-indigo text-2xl mb-3">暂无草稿</div> |
| 129 | <p className="text-ink-soft"> |
| 130 | {isZh |
| 131 | ? "草稿将在 cron 运行后出现。可在 wrangler.jsonc 中配置触发时间。" |
| 132 | : "Drafts will appear here after cron runs. Configure triggers in wrangler.jsonc."} |
| 133 | </p> |
| 134 | </div> |
| 135 | )} |
| 136 | |
| 137 | <AdminClient |
| 138 | drafts={pending} |
| 139 | posted={posted} |
| 140 | isZh={isZh} |
| 141 | typeLabels={TYPE_LABELS} |
| 142 | /> |
| 143 | </section> |
| 144 | ); |
| 145 | } |
| 146 |