nomadro/src/app/admin/config/page.tsx
hackrobot 05b9ab3e33 Add admin toggle for WeChat/X follow popup with safe config writes.
Expose socialFollowAuto in /admin/config, default off so live behavior stays unchanged; backup and preserve platforms.tt on save; protect remote-config.json during deploy.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-02 04:51:02 +08:00

238 lines
7.6 KiB
TypeScript
Raw 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>
{(
[
{
key: "payEnabled" as const,
label: "支付总开关",
hint: "关闭后前端隐藏/拒绝支付入口(不影响已有订单数据)",
},
{
key: "socialFollow" as const,
label: "关注引导总开关",
hint: "关闭后不自动弹出「关注公众号 / X」弹窗(含稍后再说)",
},
{
key: "socialFollowAuto" as const,
label: "自动弹出关注公众号(稍后再说)",
hint: "开启后首页延迟约 4.5s 弹出;需同时打开「关注引导总开关」。默认关闭。",
},
{
key: "offlineBadge" as const,
label: "离线模式角标",
hint: "网络失败走本地兜底时是否显示离线提示",
},
] as const
).map(({ key, label, hint }) => (
<label
key={key}
style={{
display: "flex",
gap: 10,
marginBottom: 12,
cursor: "pointer",
alignItems: "flex-start",
}}
>
<input
type="checkbox"
checked={Boolean(config.flags[key])}
onChange={(e) =>
setConfig({
...config,
flags: { ...config.flags, [key]: e.target.checked },
})
}
style={{ marginTop: 3 }}
/>
<span>
<strong>{label}</strong>
<span className={styles.muted} style={{ display: "block", fontSize: 12 }}>
{hint} · <code>{key}</code>
</span>
</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>
</>
);
}