/** * 单受众模块运营台:/cn 或 /intl * 入口级区分中文用户 vs 国际(英语为主),再进开关 / 埋点等。 */ import { useCallback, useEffect, useState } from 'react' import { Link } from 'react-router-dom' import { api } from '../api/client' import { useDraft } from '../lib/useDraft' import { useConfigLive } from '../lib/useConfigLive' import UnsavedChanges from '../components/UnsavedChanges' export type LaneKey = 'cn' | 'intl' type Lane = { payEnabled: boolean socialFollow: boolean followChannel: 'wechat' | 'x' | 'none' vipCta: string homeLead: string note?: string } type AudienceCfg = { defaultLane: 'cn' | 'intl' cn: Lane intl: Lane version?: number updatedAt?: string } const META: Record< LaneKey, { title: string titleEn: string eyebrow: string detect: string pay: string follow: string accent: string } > = { cn: { title: '中文用户', titleEn: 'Chinese', eyebrow: '特殊对待 · Special lane', detect: '微信内置 MicroMessenger / is-china-user → 本模块', pay: 'zpay · 微信内 xpay', follow: '公众号二维码', accent: 'cn', }, intl: { title: '国际用户', titleEn: 'International', eyebrow: '.com 默认 · English-first', detect: '非中文信号 → 本模块(日/越/泰可切语言)', pay: 'Creem · NOWPayments', follow: 'X / Twitter', accent: 'intl', }, } function Switch({ checked, onChange, label, disabled, }: { checked: boolean onChange: (v: boolean) => void label: string disabled?: boolean }) { return ( ) } export default function LaneHub({ lane }: { lane: LaneKey }) { const meta = META[lane] const [cfg, setCfg] = useState(null) const [busy, setBusy] = useState(false) const [msg, setMsg] = useState('') const [err, setErr] = useState('') const live = useConfigLive() const editor = useDraft({ vipCta: '', homeLead: '' }) const { vipCta, homeLead } = editor.draft const setVipCta = (value: string) => editor.setDraft(d => ({ ...d, vipCta: value })) const setHomeLead = (value: string) => editor.setDraft(d => ({ ...d, homeLead: value })) const stale = cfg?.version != null && live.meta != null && live.meta.version > cfg.version const load = useCallback(async () => { setErr('') const data = await api('/api/admin/audience') setCfg(data) const L = data[lane] editor.reset({ vipCta: L.vipCta, homeLead: L.homeLead }) }, [lane]) useEffect(() => { load().catch((e: Error) => setErr(e.message || '加载失败')) }, [load]) useEffect(() => { if (stale && !editor.dirty && !busy) void load().catch(e => setErr(String(e))) }, [stale, editor.dirty, busy, load]) async function patchLane(partial: Partial, okHint: string) { if (!cfg || busy) return if (stale) { setErr('配置已更新,请加载最新配置后重试;当前草稿已保留'); return } setBusy(true) setMsg('') setErr('') try { const data = await api('/api/admin/audience', { method: 'PUT', body: JSON.stringify({ [lane]: partial, expectedVersion: cfg.version }), }) setCfg(data) editor.accept({ vipCta: data[lane].vipCta, homeLead: data[lane].homeLead }, Object.keys(partial) as Array<'vipCta' | 'homeLead'>) setMsg(okHint) } catch (e: unknown) { setErr(e instanceof Error ? e.message : '保存失败') } finally { setBusy(false) } } const L = cfg?.[lane] const other: LaneKey = lane === 'cn' ? 'intl' : 'cn' const [stats, setStats] = useState | null>(null) const [socialCounts, setSocialCounts] = useState | null>(null) useEffect(() => { api>('/api/admin/stats') .then(setStats) .catch(() => setStats(null)) api<{ counts?: Record }>('/api/admin/social/ugc/overview') .then((d) => setSocialCounts(d.counts || null)) .catch(() => setSocialCounts(null)) }, [lane]) const tools = [ { to: `/${lane}/social/overview`, ico: '◈', title: '指挥台', desc: '加入 / 自拍 / 举报优先' }, { to: `/${lane}/pricing`, ico: '¥', title: '动态价格', desc: '免费 / 付费一键切换 · 体验次数' }, { to: `/${lane}/switches`, ico: '开', title: '页面开关', desc: '模块显隐 · VIP 入口 · 文案' }, { to: `/${lane}/social/locale-geo`, ico: '语', title: '语言国家', desc: '多语言开关 · 国家目录 · 优先城市' }, { to: `/${lane}/blog`, ico: '✎', title: 'Nomad Blog', desc: '文章显隐 / 置顶' }, { to: `/${lane}/orders`, ico: '订', title: '订单', desc: '支付与发货' }, { to: `/${lane}/users`, ico: '◎', title: '用户', desc: '解锁与干预' }, { to: `/${lane}/events`, ico: '◉', title: '用户分析', desc: '访客 · DAU · 漏斗 · 事件流' }, { to: `/${lane}/ntfy`, ico: '♪', title: '通知', desc: 'ntfy 试推' }, ] return (
{meta.eyebrow}

受众设置 · {meta.title}{' '} {meta.titleEn}

{meta.detect}
支付 {meta.pay} · 关注 {meta.follow}

/{lane} {cfg?.defaultLane === lane ? 识别失败时默认 : null} {cfg?.version != null ? v{cfg.version} : null}
看{META[other].title}设置
{msg ?
{msg}
: null} {err ?
{err}
: null} {(stale || (!cfg && err)) &&
{stale ? '线上配置已更新,当前草稿已保留。' : '配置加载失败。'}
} {(stats || socialCounts) && (
已付订单 {stats?.paid_orders ?? '—'}
用户 {stats?.users ?? '—'}
社群聊天 {socialCounts?.messages ?? '—'}
花名册 {socialCounts?.roster ?? '—'}
)}
{tools.map((t) => ( {t.ico} {t.title} {t.desc} ))}
{!L ? (
加载本轨配置…
) : (

本轨开关与文案

{L.note || '写入 remote-config.audience.' + lane}

支付 CTA

{meta.pay}

void patchLane({ payEnabled: v }, `支付已${v ? '开' : '关'}`)} />
关注引导

{meta.follow}

void patchLane({ socialFollow: v }, `关注已${v ? '开' : '关'}`)} />
关注渠道

本轨默认渠道