Document the 2026-11-05 UI/UX upgrade and keep roster/live gates aligned with face-check and presence. Co-authored-by: Cursor <cursoragent@cursor.com>
475 lines
17 KiB
TypeScript
475 lines
17 KiB
TypeScript
/**
|
||
* 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<TabKey>('posts')
|
||
const [ov, setOv] = useState<Overview | null>(null)
|
||
const [posts, setPosts] = useState<Post[]>([])
|
||
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<Overview>('/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<string, unknown>, 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<Overview>('/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<keyof typeof draft>)
|
||
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 (
|
||
<div className="com-page com-blog-admin">
|
||
<header className="com-page-head com-page-head-row">
|
||
<div>
|
||
<h1>Nomad Blog</h1>
|
||
<p>
|
||
管理 <code>/social/blog</code>:文章显隐、置顶、AdSense 与模块开关。正文仍在仓库 Markdown(Tina
|
||
式 Git SoT)。
|
||
</p>
|
||
<div className="com-chip-row">
|
||
<span className={`com-chip${ov?.moduleEnabled ? ' com-chip-ok' : ''}`}>
|
||
模块 {ov?.moduleEnabled ? '开' : '关'}
|
||
</span>
|
||
<span className={`com-chip${ov?.adsenseEnabled ? ' com-chip-ok' : ''}`}>
|
||
AdSense {ov?.adsenseEnabled ? '展示中' : '关闭'}
|
||
</span>
|
||
{ov?.version != null ? <span className="com-chip">v{ov.version}</span> : null}
|
||
</div>
|
||
</div>
|
||
<div className="com-blog-head-actions">
|
||
{ov?.liveUrl ? (
|
||
<a className="com-btn com-btn-ghost" href={ov.liveUrl} target="_blank" rel="noreferrer">
|
||
打开博客 ↗
|
||
</a>
|
||
) : null}
|
||
<button type="button" className="com-btn com-btn-ghost" onClick={() => { if (!editor.dirty || window.confirm('刷新将放弃未保存修改,继续?')) void load() }} disabled={!!busy || loading}>
|
||
{loading ? '刷新中…' : '刷新'}
|
||
</button>
|
||
</div>
|
||
</header>
|
||
|
||
<UnsavedChanges dirty={editor.dirty} />
|
||
{msg ? <div className="com-toast com-toast-ok" role="status">{msg}</div> : null}
|
||
{err ? <div className="com-toast com-toast-err" role="alert">{err}</div> : null}
|
||
{stale && <div className="com-toast" role="status">线上配置已更新,当前草稿已保留。请使用刷新按钮加载最新配置后再保存。</div>}
|
||
|
||
<div className="com-stat-grid com-stat-grid-4">
|
||
<article className="com-stat">
|
||
<span className="com-stat-label">文章</span>
|
||
<strong>{ov?.counts?.posts ?? '—'}</strong>
|
||
</article>
|
||
<article className="com-stat">
|
||
<span className="com-stat-label">可见</span>
|
||
<strong>{ov?.counts?.visible ?? '—'}</strong>
|
||
</article>
|
||
<article className="com-stat">
|
||
<span className="com-stat-label">隐藏</span>
|
||
<strong>{ov?.counts?.hidden ?? '—'}</strong>
|
||
</article>
|
||
<article className="com-stat">
|
||
<span className="com-stat-label">广告就绪</span>
|
||
<strong>{adsReady ? '是' : '否'}</strong>
|
||
<span className="com-stat-note">{adsReady ? 'client + slot' : '先填 clientId / slot'}</span>
|
||
</article>
|
||
</div>
|
||
|
||
<div className="com-blog-quicklinks">
|
||
{(
|
||
[
|
||
['线上博客', ov?.liveUrl],
|
||
['About', ov?.aboutUrl],
|
||
['Privacy', ov?.privacyUrl],
|
||
['ads.txt', ov?.adsTxtUrl],
|
||
['RSS', ov?.rssUrl],
|
||
['Sitemap', ov?.sitemapUrl],
|
||
] as const
|
||
).map(([label, href]) =>
|
||
href ? (
|
||
<a key={label} href={href} target="_blank" rel="noreferrer" className="com-blog-quick">
|
||
{label}
|
||
</a>
|
||
) : null,
|
||
)}
|
||
</div>
|
||
|
||
<nav className="com-tabs" aria-label="博客运营">
|
||
{TABS.map((t) => (
|
||
<button
|
||
key={t.key}
|
||
type="button"
|
||
className={`com-tab${tab === t.key ? ' is-on' : ''}`}
|
||
onClick={() => setTab(t.key)}
|
||
>
|
||
{t.label}
|
||
</button>
|
||
))}
|
||
</nav>
|
||
|
||
{tab === 'posts' ? (
|
||
<section className="com-panel">
|
||
<div className="com-panel-head">
|
||
<div>
|
||
<h2>文章列表</h2>
|
||
<p>置顶 / 隐藏写入 remote-config;改标题正文请在仓库 Markdown。</p>
|
||
</div>
|
||
</div>
|
||
<div className="com-toolbar">
|
||
<input
|
||
className="com-input"
|
||
type="search"
|
||
aria-label="搜索博客文章"
|
||
placeholder="搜标题 / slug / 标签"
|
||
value={q}
|
||
onChange={(e) => setQ(e.target.value)}
|
||
/>
|
||
<span className="com-muted">{filtered.length} 篇</span>
|
||
</div>
|
||
{loading && !posts.length ? (
|
||
<p className="com-muted">加载中…</p>
|
||
) : !filtered.length ? (
|
||
<p className="com-muted">暂无文章(确认门户 /api/public/v1/social/blog 可访问)</p>
|
||
) : (
|
||
<div className="com-blog-cards">
|
||
{filtered.map((p) => (
|
||
<article
|
||
key={p.slug}
|
||
className={`com-blog-card${p.featured ? ' is-featured' : ''}${p.hidden ? ' is-hidden' : ''}`}
|
||
>
|
||
<div className="com-blog-card-top">
|
||
<div className="com-chip-row">
|
||
{p.featured ? <span className="com-chip com-chip-ok">置顶</span> : null}
|
||
{p.hidden ? <span className="com-chip">已隐藏</span> : null}
|
||
<span className="com-chip">{p.date || '—'}</span>
|
||
</div>
|
||
<a href={p.url || '#'} target="_blank" rel="noreferrer" className="com-blog-card-link">
|
||
预览
|
||
</a>
|
||
</div>
|
||
<h3>{p.title}</h3>
|
||
<p>{p.description || p.slug}</p>
|
||
<div className="com-blog-card-meta">
|
||
<code>{p.slug}</code>
|
||
{p.readingMinutes ? <span>{p.readingMinutes} min</span> : null}
|
||
{p.wordCount ? <span>{p.wordCount} words</span> : null}
|
||
</div>
|
||
<div className="com-blog-card-actions">
|
||
<button
|
||
type="button"
|
||
className="com-btn com-btn-ghost com-btn-sm"
|
||
disabled={!!busy || p.featured}
|
||
onClick={() => void patch({ featuredSlug: p.slug }, `已置顶 ${p.slug}`)}
|
||
>
|
||
置顶
|
||
</button>
|
||
{p.hidden ? (
|
||
<button
|
||
type="button"
|
||
className="com-btn com-btn-ghost com-btn-sm"
|
||
disabled={!!busy}
|
||
onClick={() => void patch({ showSlug: p.slug }, `已显示 ${p.slug}`)}
|
||
>
|
||
显示
|
||
</button>
|
||
) : (
|
||
<button
|
||
type="button"
|
||
className="com-btn com-btn-danger com-btn-sm"
|
||
disabled={!!busy}
|
||
onClick={() => void patch({ hideSlug: p.slug }, `已隐藏 ${p.slug}`)}
|
||
>
|
||
隐藏
|
||
</button>
|
||
)}
|
||
</div>
|
||
</article>
|
||
))}
|
||
</div>
|
||
)}
|
||
</section>
|
||
) : null}
|
||
|
||
{tab === 'adsense' ? (
|
||
<section className="com-panel">
|
||
<div className="com-panel-head">
|
||
<div>
|
||
<h2>Google AdSense</h2>
|
||
<p>审过前保持关闭。开启后仅英文长文页插位。</p>
|
||
</div>
|
||
<label className="com-switch" title="AdSense">
|
||
<input
|
||
type="checkbox"
|
||
aria-label="AdSense 展示开关"
|
||
checked={Boolean(ov?.adsenseEnabled)}
|
||
disabled={!!busy}
|
||
onChange={(e) =>
|
||
void patch(
|
||
{ adsenseEnabled: e.target.checked },
|
||
e.target.checked ? 'AdSense 已开启' : 'AdSense 已关闭',
|
||
)
|
||
}
|
||
/>
|
||
<span className="com-switch-track">
|
||
<span className="com-switch-thumb" />
|
||
</span>
|
||
</label>
|
||
</div>
|
||
<div className="com-form-grid">
|
||
{(
|
||
[
|
||
['adsenseClientId', 'clientId (ca-pub-…)'],
|
||
['adsensePublisherId', 'ads.txt publisher (pub-…)'],
|
||
['adsenseSlotBlogMid', '文中 slot'],
|
||
['adsenseSlotBlogEnd', '文末 slot'],
|
||
] as const
|
||
).map(([key, label]) => (
|
||
<label key={key} className="com-field">
|
||
<span>{label}</span>
|
||
<input
|
||
value={draft[key]}
|
||
disabled={!!busy}
|
||
onChange={(e) => setDraft((d) => ({ ...d, [key]: e.target.value }))}
|
||
/>
|
||
</label>
|
||
))}
|
||
</div>
|
||
<div className="com-actions">
|
||
<button
|
||
type="button"
|
||
className="com-btn"
|
||
disabled={!!busy}
|
||
onClick={() =>
|
||
void patch(
|
||
{
|
||
adsenseClientId: draft.adsenseClientId.trim(),
|
||
adsensePublisherId: draft.adsensePublisherId.trim(),
|
||
adsenseSlotBlogMid: draft.adsenseSlotBlogMid.trim(),
|
||
adsenseSlotBlogEnd: draft.adsenseSlotBlogEnd.trim(),
|
||
},
|
||
'AdSense 配置已保存',
|
||
)
|
||
}
|
||
>
|
||
保存 AdSense
|
||
</button>
|
||
</div>
|
||
<ul className="com-list" style={{ marginTop: 16 }}>
|
||
<li>申请期:模块开、广告关、政策页齐全、持续发英文长文</li>
|
||
<li>
|
||
过审后:填 client / slot → 开展示 → 更新根目录 <code>ads.txt</code>
|
||
</li>
|
||
</ul>
|
||
</section>
|
||
) : null}
|
||
|
||
{tab === 'module' ? (
|
||
<section className="com-panel">
|
||
<div className="com-panel-head">
|
||
<div>
|
||
<h2>模块与运营文案</h2>
|
||
<p>关闭 modules.socialBlog 可隐藏入口(需门户读配置)。</p>
|
||
</div>
|
||
<label className="com-switch" title="socialBlog">
|
||
<input
|
||
type="checkbox"
|
||
aria-label="博客模块开关"
|
||
checked={ov?.moduleEnabled !== false}
|
||
disabled={!!busy}
|
||
onChange={(e) =>
|
||
void patch(
|
||
{ moduleEnabled: e.target.checked },
|
||
e.target.checked ? 'Blog 模块已开' : 'Blog 模块已关',
|
||
)
|
||
}
|
||
/>
|
||
<span className="com-switch-track">
|
||
<span className="com-switch-thumb" />
|
||
</span>
|
||
</label>
|
||
</div>
|
||
<div className="com-form-grid">
|
||
<label className="com-field">
|
||
<span>运营备注(仅后台)</span>
|
||
<input
|
||
value={draft.note}
|
||
maxLength={500}
|
||
disabled={!!busy}
|
||
onChange={(e) => setDraft((d) => ({ ...d, note: e.target.value }))}
|
||
/>
|
||
</label>
|
||
<label className="com-field">
|
||
<span>Hero 导语覆盖(可选)</span>
|
||
<input
|
||
value={draft.heroLead}
|
||
maxLength={400}
|
||
disabled={!!busy}
|
||
onChange={(e) => setDraft((d) => ({ ...d, heroLead: e.target.value }))}
|
||
/>
|
||
</label>
|
||
</div>
|
||
<div className="com-actions">
|
||
<button
|
||
type="button"
|
||
className="com-btn"
|
||
disabled={!!busy}
|
||
onClick={() =>
|
||
void patch(
|
||
{ note: draft.note, heroLead: draft.heroLead },
|
||
'文案已保存',
|
||
)
|
||
}
|
||
>
|
||
保存文案
|
||
</button>
|
||
<button
|
||
type="button"
|
||
className="com-btn com-btn-ghost"
|
||
disabled={!!busy || !ov?.ops?.featuredSlug}
|
||
onClick={() => void patch({ featuredSlug: '' }, '已取消置顶')}
|
||
>
|
||
清除置顶
|
||
</button>
|
||
</div>
|
||
</section>
|
||
) : null}
|
||
</div>
|
||
)
|
||
}
|