nomadro/app/admin/tt/page.tsx
eric 5d442b6783 Isolate Douyin admin from WeChat: drop shared tt-default-book seed.
Keep platforms.tt Douyin-only; weapp offline book and top-level modules unchanged.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-30 20:45:18 +08:00

380 lines
12 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

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 {
DEFAULT_TT_BOOK_CONTENT,
DEFAULT_TT_PLATFORM,
type NomadroRemoteConfig,
type TtBookChapter,
type TtPlatformConfig,
} from "~/lib/remote-config";
import styles from "../admin.module.css";
type TtKey = keyof TtPlatformConfig["modules"];
const MODULE_LABELS: Record<TtKey, string> = {
nav: "出海导航",
book: "数字游民小册",
community: "旅居社群",
};
function ensureTt(config: NomadroRemoteConfig): TtPlatformConfig {
return {
modules: {
nav: { ...DEFAULT_TT_PLATFORM.modules.nav, ...config.platforms?.tt?.modules?.nav },
book: { ...DEFAULT_TT_PLATFORM.modules.book, ...config.platforms?.tt?.modules?.book },
community: {
...DEFAULT_TT_PLATFORM.modules.community,
...config.platforms?.tt?.modules?.community,
},
},
bookContent: {
...DEFAULT_TT_BOOK_CONTENT,
...config.platforms?.tt?.bookContent,
chapters:
config.platforms?.tt?.bookContent?.chapters?.length
? config.platforms.tt.bookContent.chapters
: DEFAULT_TT_BOOK_CONTENT.chapters,
},
};
}
export default function AdminTtPage() {
const [config, setConfig] = useState<NomadroRemoteConfig | null>(null);
const [tt, setTt] = useState<TtPlatformConfig | null>(null);
const [activeIdx, setActiveIdx] = useState(0);
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;
}
const cfg = data.config as NomadroRemoteConfig;
setConfig(cfg);
setTt(ensureTt(cfg));
}, []);
useEffect(() => {
void load();
}, [load]);
function patchTt(next: TtPlatformConfig) {
setTt(next);
}
function setModuleEnabled(key: TtKey, enabled: boolean) {
if (!tt) return;
patchTt({
...tt,
modules: {
...tt.modules,
[key]: { ...tt.modules[key], enabled },
},
});
}
function setBookMeta(field: keyof TtPlatformConfig["bookContent"], value: string) {
if (!tt) return;
patchTt({
...tt,
bookContent: { ...tt.bookContent, [field]: value },
});
}
function setChapter(idx: number, patch: Partial<TtBookChapter>) {
if (!tt) return;
const chapters = tt.bookContent.chapters.map((c, i) =>
i === idx ? { ...c, ...patch } : c,
);
patchTt({
...tt,
bookContent: { ...tt.bookContent, chapters },
});
}
function addChapter() {
if (!tt) return;
const n = tt.bookContent.chapters.length + 1;
patchTt({
...tt,
bookContent: {
...tt.bookContent,
chapters: [
...tt.bookContent.chapters,
{
id: `ch-${Date.now()}`,
title: `第 ${n} 章`,
emoji: "📖",
content: `# 第 ${n} 章\n\n在此编辑 Markdown…\n`,
},
],
},
});
setActiveIdx(n - 1);
}
function removeChapter(idx: number) {
if (!tt || tt.bookContent.chapters.length <= 1) return;
const chapters = tt.bookContent.chapters.filter((_, i) => i !== idx);
patchTt({
...tt,
bookContent: { ...tt.bookContent, chapters },
});
setActiveIdx(Math.max(0, Math.min(activeIdx, chapters.length - 1)));
}
function resetBook() {
if (!tt) return;
if (!window.confirm("恢复为默认 7 章「数字游民」正文?当前远程正文会被覆盖。")) return;
patchTt({
...tt,
bookContent: structuredClone(DEFAULT_TT_BOOK_CONTENT),
});
setActiveIdx(0);
}
async function save() {
if (!config || !tt) return;
setBusy(true);
setSaved("");
setError("");
try {
const next: NomadroRemoteConfig = {
...config,
platforms: {
...config.platforms,
tt: {
...tt,
bookContent: {
...tt.bookContent,
updatedAt: new Date().toISOString(),
},
},
},
};
const res = await fetch("/api/admin/config", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ config: next }),
});
const data = await res.json().catch(() => ({}));
if (!res.ok) {
setError(data.error || "保存失败");
return;
}
setConfig(data.config);
setTt(ensureTt(data.config));
setSaved(`已保存 · ${data.config.updatedAt}`);
} finally {
setBusy(false);
}
}
if (!tt) {
return (
<>
<h1 className={styles.pageTitle}>🎵 抖音小程序</h1>
<p className={styles.muted}>{error || "加载中…"}</p>
</>
);
}
const chapter = tt.bookContent.chapters[activeIdx];
return (
<>
<h1 className={styles.pageTitle}>🎵 抖音小程序</h1>
<p className={styles.pageLead}>
<strong>仅控制抖音小程序</strong>(<code>platforms.tt</code>)。微信小程序 / 门户顶层{" "}
<code>modules</code> 与正文数据互不影响,请到「远程配置」改微信侧开关。
客户端无网用抖音包内 7 章「数字游民」兜底。
</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>
<p className={styles.muted} style={{ marginBottom: 12 }}>
默认:导航关 · 小册开 · 社群关。勾选即在抖音首页显示对应入口。
</p>
<div className={styles.grid2}>
{(Object.keys(MODULE_LABELS) as TtKey[]).map((key) => {
const m = tt.modules[key];
return (
<label key={key} className={styles.panel} style={{ cursor: "pointer" }}>
<div style={{ display: "flex", justifyContent: "space-between", gap: 8 }}>
<strong>
{MODULE_LABELS[key]}{" "}
<span className={styles.muted} style={{ fontWeight: 500 }}>
({key})
</span>
</strong>
<input
type="checkbox"
checked={!!m.enabled}
onChange={(e) => setModuleEnabled(key, e.target.checked)}
/>
</div>
<p className={styles.muted} style={{ marginTop: 8, marginBottom: 0 }}>
{m.note || (m.enabled ? "首页显示" : "首页隐藏")}
</p>
</label>
);
})}
</div>
</div>
<div className={styles.panel} style={{ marginBottom: 12 }}>
<h3>小册元信息</h3>
<div className={styles.grid2}>
{(
[
["title", "书名"],
["subtitle", "副标题"],
["author", "作者"],
["coverEmoji", "封面 emoji"],
] as const
).map(([field, label]) => (
<label key={field} style={{ display: "block" }}>
<span className={styles.muted}>{label}</span>
<input
className={styles.input}
style={{ width: "100%", marginTop: 6 }}
value={String(tt.bookContent[field] || "")}
onChange={(e) => setBookMeta(field, e.target.value)}
/>
</label>
))}
</div>
<label style={{ display: "block", marginTop: 12 }}>
<span className={styles.muted}>简介</span>
<textarea
className={styles.input}
style={{ width: "100%", marginTop: 6, minHeight: 72 }}
value={tt.bookContent.description || ""}
onChange={(e) => setBookMeta("description", e.target.value)}
/>
</label>
<label style={{ display: "block", marginTop: 12 }}>
<span className={styles.muted}>金句 / tagline</span>
<input
className={styles.input}
style={{ width: "100%", marginTop: 6 }}
value={tt.bookContent.tagline || ""}
onChange={(e) => setBookMeta("tagline", e.target.value)}
/>
</label>
</div>
<div className={styles.panel} style={{ marginBottom: 12 }}>
<div style={{ display: "flex", justifyContent: "space-between", gap: 12, flexWrap: "wrap" }}>
<h3 style={{ margin: 0 }}>章节 Markdown({tt.bookContent.chapters.length})</h3>
<div style={{ display: "flex", gap: 8 }}>
<button type="button" className={styles.chip} onClick={addChapter}>
+ 加章
</button>
<button type="button" className={styles.chip} onClick={resetBook}>
恢复 7 章默认
</button>
</div>
</div>
<div style={{ display: "flex", gap: 8, flexWrap: "wrap", margin: "12px 0" }}>
{tt.bookContent.chapters.map((c, i) => (
<button
key={c.id || i}
type="button"
className={styles.chip}
style={{
opacity: i === activeIdx ? 1 : 0.65,
borderColor: i === activeIdx ? "var(--admin-accent)" : undefined,
}}
onClick={() => setActiveIdx(i)}
>
{c.emoji || "📖"} {i + 1}. {c.title || c.id}
</button>
))}
</div>
{chapter ? (
<>
<div className={styles.grid2}>
<label style={{ display: "block" }}>
<span className={styles.muted}>章标题</span>
<input
className={styles.input}
style={{ width: "100%", marginTop: 6 }}
value={chapter.title}
onChange={(e) => setChapter(activeIdx, { title: e.target.value })}
/>
</label>
<label style={{ display: "block" }}>
<span className={styles.muted}>章 id</span>
<input
className={styles.input}
style={{ width: "100%", marginTop: 6 }}
value={chapter.id}
onChange={(e) => setChapter(activeIdx, { id: e.target.value })}
/>
</label>
<label style={{ display: "block" }}>
<span className={styles.muted}>emoji</span>
<input
className={styles.input}
style={{ width: "100%", marginTop: 6 }}
value={chapter.emoji || ""}
onChange={(e) => setChapter(activeIdx, { emoji: e.target.value })}
/>
</label>
<label style={{ display: "block" }}>
<span className={styles.muted}>hook(封面打字机)</span>
<input
className={styles.input}
style={{ width: "100%", marginTop: 6 }}
value={chapter.hook || ""}
onChange={(e) => setChapter(activeIdx, { hook: e.target.value })}
/>
</label>
</div>
<label style={{ display: "block", marginTop: 12 }}>
<span className={styles.muted}>正文 Markdown</span>
<textarea
className={styles.input}
style={{
width: "100%",
marginTop: 6,
minHeight: 320,
fontFamily: "ui-monospace, SFMono-Regular, Menlo, Consolas, monospace",
fontSize: "0.85rem",
lineHeight: 1.5,
}}
value={chapter.content}
onChange={(e) => setChapter(activeIdx, { content: e.target.value })}
/>
</label>
<button
type="button"
className={styles.chip}
style={{ marginTop: 10, color: "var(--admin-coral)" }}
onClick={() => removeChapter(activeIdx)}
disabled={tt.bookContent.chapters.length <= 1}
>
删除本章
</button>
</>
) : null}
</div>
<button type="button" className={styles.primaryBtn} disabled={busy} onClick={() => void save()}>
{busy ? "保存中…" : "保存抖音配置"}
</button>
</>
);
}