nomadro/app/admin/config/page.tsx
eric 9f8fec1723 Ship portal PC/H5 acceptance, VIP ScrollReader, admin analytics and ops anchors.
Adds /api/health, ntfy notify on config save, /admin/analytics charts, and deploy smoke for miniprogram config/API.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 23:52:00 -05:00

200 lines
6.3 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import { useCallback, useEffect, useState } from "react";
import type { NomadroRemoteConfig } from "~/lib/remote-config";
import styles from "../admin.module.css";
export default function AdminConfigPage() {
const [config, setConfig] = useState<NomadroRemoteConfig | null>(null);
const [error, setError] = useState("");
const [saved, setSaved] = useState("");
const [busy, setBusy] = useState(false);
const load = useCallback(async () => {
setError("");
const res = await fetch("/api/admin/config", { cache: "no-store" });
const data = await res.json().catch(() => ({}));
if (!res.ok) {
setError(data.error || "加载失败");
return;
}
setConfig(data.config);
}, []);
useEffect(() => {
void load();
}, [load]);
async function save() {
if (!config) return;
setBusy(true);
setSaved("");
setError("");
try {
const res = await fetch("/api/admin/config", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ config }),
});
const data = await res.json().catch(() => ({}));
if (!res.ok) {
setError(data.error || "保存失败");
return;
}
setConfig(data.config);
setSaved(`已保存 · ${data.config.updatedAt}`);
} finally {
setBusy(false);
}
}
if (!config) {
return (
<>
<h1 className={styles.pageTitle}>⚙️ 远程配置</h1>
<p className={styles.muted}>{error || "加载中…"}</p>
</>
);
}
const moduleKeys = Object.keys(config.modules) as (keyof typeof config.modules)[];
return (
<>
<h1 className={styles.pageTitle}>⚙️ 远程配置</h1>
<p className={styles.pageLead}>
前端模块开关 / 文案 / VIP 价格由后端下发;客户端无网用包内默认配置兜底。
持久化:<code>data/remote-config.json</code>
</p>
{error ? <p className={styles.error}>{error}</p> : null}
{saved ? <p className={styles.muted}>{saved}</p> : null}
<div className={styles.panel} style={{ marginBottom: 12 }}>
<h3>模块开关</h3>
<div className={styles.grid2}>
{moduleKeys.map((key) => {
const m = config.modules[key];
return (
<label key={key} className={styles.panel} style={{ cursor: "pointer" }}>
<div style={{ display: "flex", justifyContent: "space-between", gap: 8 }}>
<strong>{m.title || key}</strong>
<input
type="checkbox"
checked={m.enabled}
onChange={(e) =>
setConfig({
...config,
modules: {
...config.modules,
[key]: { ...m, enabled: e.target.checked },
},
})
}
/>
</div>
<input
className={styles.input}
style={{ marginTop: 8, width: "100%" }}
value={m.title}
onChange={(e) =>
setConfig({
...config,
modules: {
...config.modules,
[key]: { ...m, title: e.target.value },
},
})
}
/>
{m.tabLabel !== undefined ? (
<input
className={styles.input}
style={{ marginTop: 8, width: "100%" }}
value={m.tabLabel || ""}
placeholder="Tab 文案"
onChange={(e) =>
setConfig({
...config,
modules: {
...config.modules,
[key]: { ...m, tabLabel: e.target.value },
},
})
}
/>
) : null}
</label>
);
})}
</div>
</div>
<div className={styles.panel} style={{ marginBottom: 12 }}>
<h3>功能 Flag</h3>
{(Object.keys(config.flags) as (keyof typeof config.flags)[]).map((k) => (
<label key={k} style={{ display: "flex", gap: 10, marginBottom: 8, cursor: "pointer" }}>
<input
type="checkbox"
checked={config.flags[k]}
onChange={(e) =>
setConfig({
...config,
flags: { ...config.flags, [k]: e.target.checked },
})
}
/>
<span>{k}</span>
</label>
))}
</div>
<div className={styles.panel} style={{ marginBottom: 12 }}>
<h3>文案 / VIP</h3>
{(
[
["homeBrand", "品牌"],
["homeTagline", "副标题"],
["homeLead", "导语"],
["vipProductId", "VIP productId"],
["vipCta", "VIP 按钮"],
] as const
).map(([key, label]) => (
<label key={key} style={{ display: "block", marginBottom: 10 }}>
<span className={styles.muted}>{label}</span>
<input
className={styles.input}
style={{ width: "100%", marginTop: 4 }}
value={String(config.copy[key])}
onChange={(e) =>
setConfig({
...config,
copy: { ...config.copy, [key]: e.target.value },
})
}
/>
</label>
))}
<label style={{ display: "block", marginBottom: 10 }}>
<span className={styles.muted}>VIP 价格(分)</span>
<input
className={styles.input}
style={{ width: "100%", marginTop: 4 }}
type="number"
value={config.copy.vipPriceFen}
onChange={(e) =>
setConfig({
...config,
copy: { ...config.copy, vipPriceFen: Number(e.target.value) || 0 },
})
}
/>
</label>
</div>
<button type="button" className={styles.primaryBtn} disabled={busy} onClick={save}>
{busy ? "保存中…" : "保存到后端"}
</button>
</>
);
}