返回 CodeWhale
page.tsx
根目录 / web / app / [locale] / admin / page.tsx
1 import type { Metadata } from "next";
2 import { cookies } from "next/headers";
3 import { ConnectionBanner } from "@/components/connection-banner";
4 import { EmptyState, ErrorState } from "@/components/surface-state";
5 import { WhalePose } from "@/components/whale-pose";
6 import { getAgentEnv, listDrafts, validateSession, type AgentDraft } from "@/lib/community-agent";
7 import { MAINTAINER_TOKEN_UNAVAILABLE, maintainerTokenUsable } from "@/lib/maintainer-token";
8 import { AdminClient } from "./admin-client";
9
10 export const dynamic = "force-dynamic";
11
12 // Maintainer-only surface: keep it out of search indexes (robots.ts also
13 // disallows /*/admin).
14 export const metadata: Metadata = {
15 title: "Maintainer panel · Codewhale",
16 robots: { index: false, follow: false },
17 alternates: {},
18 openGraph: { title: "Maintainer panel · Codewhale" },
19 };
20
21 const TYPE_LABELS: Record<string, { en: string; zh: string }> = {
22 digest: { en: "Weekly Digest", zh: "每周摘要" },
23 linkcheck: { en: "Broken Link", zh: "失效链接" },
24 "semantic-drift": { en: "Content Drift", zh: "内容漂移" },
25 };
26
27 function LoginForm({ locale, error }: { locale: string; error: boolean }) {
28 const isZh = locale === "zh";
29 return (
30 <div className="account-entry">
31 <ConnectionBanner locale={locale} />
32 <WhalePose pose="listen" className="account-entry-pose" />
33 <h1 className="page-title">
34 {isZh ? "维护者登录" : "Maintainer login"}
35 </h1>
36 <form method="POST" action={`/api/admin/login?locale=${locale}`} autoComplete="off" className="account-form">
37 <input type="hidden" name="locale" value={locale} />
38 <label className="field">
39 <span className="field-label">{isZh ? "令牌" : "Token"}</span>
40 <input
41 type="password"
42 name="token"
43 required
44 autoFocus
45 autoComplete="off"
46 spellCheck={false}
47 aria-invalid={error || undefined}
48 aria-describedby={error ? "admin-token-error" : undefined}
49 className="field-input font-mono"
50 />
51 </label>
52 <button
53 type="submit"
54 className="btn btn-primary btn-lg"
55 >
56 {isZh ? "登录" : "Sign in"}
57 </button>
58 {error && (
59 <p id="admin-token-error" className="field-hint field-hint-error" role="alert">
60 {isZh ? "令牌错误。" : "Invalid token."}
61 </p>
62 )}
63 </form>
64 </div>
65 );
66 }
67
68 export default async function AdminPage({
69 params,
70 searchParams,
71 }: {
72 params: Promise<{ locale: string }>;
73 searchParams: Promise<{ err?: string }>;
74 }) {
75 const { locale } = await params;
76 const { err } = await searchParams;
77 const isZh = locale === "zh";
78
79 const env = await getAgentEnv();
80
81 if (!maintainerTokenUsable(env.MAINTAINER_TOKEN)) {
82 if (env.MAINTAINER_TOKEN) console.warn(MAINTAINER_TOKEN_UNAVAILABLE);
83 return (
84 <div className="route-state">
85 <ConnectionBanner locale={locale} />
86 <div className="mt-6" />
87 <ErrorState
88 locale={locale}
89 title={isZh ? "未配置" : "Not configured"}
90 titleAs="h1"
91 body={
92 isZh
93 ? "此页面当前不可用。"
94 : "This page is not available right now."
95 }
96 />
97 </div>
98 );
99 }
100
101 const cookieStore = await cookies();
102 const sid = cookieStore.get("mt_sid")?.value;
103 const authed = await validateSession(env.CURATED_KV, sid, env.MAINTAINER_TOKEN);
104
105 if (!authed) {
106 return <LoginForm locale={locale} error={err === "1"} />;
107 }
108
109 let drafts: AgentDraft[] = [];
110 try {
111 drafts = await listDrafts(env.CURATED_KV);
112 } catch (e) {
113 console.error("failed to list drafts", e);
114 }
115
116 const pending = drafts.filter((d) => !d.posted);
117 const posted = drafts.filter((d) => d.posted);
118
119 return (
120 <section className="page-body admin-page">
121 {/* The signed-in shell: typed offline / reconnect state with a real
122 server probe, so a paused action is never mistaken for a posted one. */}
123 <ConnectionBanner locale={locale} />
124 <div className="admin-head">
125 <div>
126 <h1 className="page-title">
127 {isZh ? "社区助理草稿" : "Community Assistant Drafts"}
128 </h1>
129 <p className="page-meta tabular">
130 {pending.length} pending · {posted.length} posted
131 </p>
132 </div>
133 <form method="POST" action={`/api/admin/logout?locale=${locale}`}>
134 <button
135 type="submit"
136 className="btn btn-ghost btn-sm"
137 >
138 {isZh ? "退出" : "Sign out"}
139 </button>
140 </form>
141 </div>
142
143 {pending.length === 0 && posted.length === 0 && (
144 <EmptyState
145 locale={locale}
146 title={isZh ? "暂无草稿" : "No drafts yet"}
147 body={
148 isZh
149 ? "有新草稿时会显示在这里。"
150 : "New drafts appear here when there are any."
151 }
152 />
153 )}
154
155 <AdminClient
156 drafts={pending}
157 posted={posted}
158 isZh={isZh}
159 typeLabels={TYPE_LABELS}
160 />
161 </section>
162 );
163 }
164
164 lines Plain Text