/** * Nomad Blog 运营台 — 文章显隐/置顶 · AdSense · 模块开关 * 内容 SoT 仍是仓库 Markdown;本页管线上运营与广告展示。 */ import { useCallback, useEffect, useMemo, useState } from 'react' import { api } from '../api/client' import { useConfigLive } from '../lib/useConfigLive' import { useDraft } from '../lib/useDraft' import UnsavedChanges from '../components/UnsavedChanges' type Overview = { ok?: boolean portal?: string liveUrl?: string aboutUrl?: string privacyUrl?: string adsTxtUrl?: string rssUrl?: string sitemapUrl?: string counts?: { posts?: number; visible?: number; hidden?: number } ops?: { featuredSlug?: string hiddenSlugs?: string[] note?: string heroLead?: string } moduleEnabled?: boolean adsenseEnabled?: boolean adsenseClientId?: string adsensePublisherId?: string adsenseSlotBlogMid?: string adsenseSlotBlogEnd?: string version?: number updatedAt?: string } type Post = { slug: string title: string description?: string date?: string tags?: string[] wordCount?: number readingMinutes?: number url?: string hidden?: boolean featured?: boolean } type TabKey = 'posts' | 'adsense' | 'module' const TABS: Array<{ key: TabKey; label: string }> = [ { key: 'posts', label: '文章' }, { key: 'adsense', label: 'AdSense' }, { key: 'module', label: '模块 / 文案' }, ] function draftFrom(overview: Overview) { return { adsenseClientId: overview.adsenseClientId || '', adsensePublisherId: overview.adsensePublisherId || '', adsenseSlotBlogMid: overview.adsenseSlotBlogMid || '', adsenseSlotBlogEnd: overview.adsenseSlotBlogEnd || '', heroLead: overview.ops?.heroLead || '', note: overview.ops?.note || '', } } export default function Blog() { const live = useConfigLive() const [tab, setTab] = useState('posts') const [ov, setOv] = useState(null) const [posts, setPosts] = useState([]) const [q, setQ] = useState('') const [loading, setLoading] = useState(true) const [busy, setBusy] = useState('') const [msg, setMsg] = useState('') const [err, setErr] = useState('') const editor = useDraft({ adsenseClientId: '', adsensePublisherId: '', adsenseSlotBlogMid: '', adsenseSlotBlogEnd: '', heroLead: '', note: '', }) const { draft, setDraft } = editor const stale = ov?.version != null && live.meta != null && live.meta.version > ov.version const load = useCallback(async () => { setLoading(true) setErr('') try { const [overview, list] = await Promise.all([ api('/api/admin/social/blog/overview'), api<{ items?: Post[] }>('/api/admin/social/blog/posts'), ]) setOv(overview) setPosts(Array.isArray(list.items) ? list.items : []) editor.reset(draftFrom(overview)) } catch (e: unknown) { setErr(e instanceof Error ? e.message : '加载失败') } finally { setLoading(false) } }, []) useEffect(() => { void load() }, [load]) useEffect(() => { if (stale && !editor.dirty && !busy) void load() }, [stale, editor.dirty, busy, load]) async function patch(body: Record, okHint: string) { if (!ov || loading || busy) return if (stale) { setErr('配置已更新,请加载最新配置后重试;当前草稿已保留'); return } for (const [key, pattern, label] of [ ['adsenseClientId', /^(ca-pub-[0-9]+)?$/, 'clientId 应为 ca-pub- 加数字'], ['adsensePublisherId', /^(pub-[0-9]+)?$/, 'publisher 应为 pub- 加数字'], ['adsenseSlotBlogMid', /^[0-9]*$/, '文中 slot 只能填写数字'], ['adsenseSlotBlogEnd', /^[0-9]*$/, '文末 slot 只能填写数字'], ] as const) { if (key in body && !pattern.test(String(body[key]))) { setErr(label); return } } setBusy(okHint) setMsg('') setErr('') try { const next = await api('/api/admin/social/blog/ops', { method: 'PATCH', body: JSON.stringify({ ...body, expectedVersion: ov.version }), }) setOv(next) editor.accept(draftFrom(next), Object.keys(body) as Array) setMsg(okHint) try { const list = await api<{ items?: Post[] }>('/api/admin/social/blog/posts') setPosts(Array.isArray(list.items) ? list.items : []) } catch { setErr('修改已保存,文章列表刷新失败,请点刷新重试') } } catch (e: unknown) { setErr(e instanceof Error ? e.message : '保存失败') } finally { setBusy('') } } const filtered = useMemo(() => { const needle = q.trim().toLowerCase() if (!needle) return posts return posts.filter((p) => `${p.title} ${p.slug} ${(p.tags || []).join(' ')}`.toLowerCase().includes(needle), ) }, [posts, q]) const adsReady = Boolean(ov?.adsenseClientId) && (Boolean(ov?.adsenseSlotBlogMid) || Boolean(ov?.adsenseSlotBlogEnd)) return (

Nomad Blog

管理 /social/blog:文章显隐、置顶、AdSense 与模块开关。正文仍在仓库 Markdown(Tina 式 Git SoT)。

模块 {ov?.moduleEnabled ? '开' : '关'} AdSense {ov?.adsenseEnabled ? '展示中' : '关闭'} {ov?.version != null ? v{ov.version} : null}
{ov?.liveUrl ? ( 打开博客 ↗ ) : null}
{msg ?
{msg}
: null} {err ?
{err}
: null} {stale &&
线上配置已更新,当前草稿已保留。请使用刷新按钮加载最新配置后再保存。
}
文章 {ov?.counts?.posts ?? '—'}
可见 {ov?.counts?.visible ?? '—'}
隐藏 {ov?.counts?.hidden ?? '—'}
广告就绪 {adsReady ? '是' : '否'} {adsReady ? 'client + slot' : '先填 clientId / slot'}
{( [ ['线上博客', ov?.liveUrl], ['About', ov?.aboutUrl], ['Privacy', ov?.privacyUrl], ['ads.txt', ov?.adsTxtUrl], ['RSS', ov?.rssUrl], ['Sitemap', ov?.sitemapUrl], ] as const ).map(([label, href]) => href ? ( {label} ) : null, )}
{tab === 'posts' ? (

文章列表

置顶 / 隐藏写入 remote-config;改标题正文请在仓库 Markdown。

setQ(e.target.value)} /> {filtered.length} 篇
{loading && !posts.length ? (

加载中…

) : !filtered.length ? (

暂无文章(确认门户 /api/public/v1/social/blog 可访问)

) : (
{filtered.map((p) => (
{p.featured ? 置顶 : null} {p.hidden ? 已隐藏 : null} {p.date || '—'}
预览

{p.title}

{p.description || p.slug}

{p.slug} {p.readingMinutes ? {p.readingMinutes} min : null} {p.wordCount ? {p.wordCount} words : null}
{p.hidden ? ( ) : ( )}
))}
)}
) : null} {tab === 'adsense' ? (

Google AdSense

审过前保持关闭。开启后仅英文长文页插位。

{( [ ['adsenseClientId', 'clientId (ca-pub-…)'], ['adsensePublisherId', 'ads.txt publisher (pub-…)'], ['adsenseSlotBlogMid', '文中 slot'], ['adsenseSlotBlogEnd', '文末 slot'], ] as const ).map(([key, label]) => ( ))}
  • 申请期:模块开、广告关、政策页齐全、持续发英文长文
  • 过审后:填 client / slot → 开展示 → 更新根目录 ads.txt
) : null} {tab === 'module' ? (

模块与运营文案

关闭 modules.socialBlog 可隐藏入口(需门户读配置)。

) : null}
) }