nomadro/src/components/social-follow.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

198 lines
6.3 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 { isChinaUser } from "is-china-user";
import { useLocale } from "~/components/locale-provider";
import styles from "./social-follow.module.css";
const DISMISS_KEY = "nomadro-social-dismiss";
const DISMISS_MS = 1000 * 60 * 60 * 24 * 7; // 7 days
const SHOW_DELAY_MS = 4500;
const X_URL = "https://x.com/nomadro";
/** Public WeChat OA QR — replace asset when brand QR updates */
export const WECHAT_OA_QR = "/wechat-oa-qr.png";
type Mode = "x" | "wechat";
function shouldShow(): boolean {
try {
const raw = localStorage.getItem(DISMISS_KEY);
if (!raw) return true;
const ts = Number(raw);
if (!Number.isFinite(ts)) return true;
return Date.now() - ts > DISMISS_MS;
} catch {
return true;
}
}
/** 受控:加群 / 关注公众号二维码弹窗 */
export function WechatOaModal({
open,
onClose,
}: {
open: boolean;
onClose: () => void;
}) {
if (!open) return null;
return (
<div className={styles.root} role="dialog" aria-modal="true" aria-labelledby="wechat-oa-title">
<button type="button" className={styles.backdrop} aria-label="关闭" onClick={onClose} />
<div className={styles.card}>
<div className={styles.glow} aria-hidden />
<button type="button" className={styles.close} onClick={onClose} aria-label="关闭">
✕
</button>
<p className={styles.eyebrow}>保持连接</p>
<h2 id="wechat-oa-title" className={styles.title}>
关注 <span className={styles.accent}>公众号</span>
</h2>
<p className={styles.lead}>获取出海导航、游民社群与会员更新,扫码即达。</p>
<div className={styles.qrWrap}>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
className={styles.qr}
src={WECHAT_OA_QR}
alt="微信公众号二维码"
width={180}
height={180}
onError={(e) => {
(e.currentTarget as HTMLImageElement).style.display = "none";
const fallback = e.currentTarget.nextElementSibling;
if (fallback instanceof HTMLElement) fallback.hidden = false;
}}
/>
<div className={styles.qrFallback} hidden>
<span aria-hidden>📱</span>
<p>请搜索公众号「nomadro」</p>
</div>
</div>
<button type="button" className={styles.later} onClick={onClose}>
稍后再说
</button>
</div>
</div>
);
}
/**
* 自动引导弹窗(默认关闭;远程 flags.socialFollowAuto === true 才延迟弹出)
* 社区「加群」请用 WechatOaModal
*/
export function SocialFollowPrompt() {
const { locale, ready } = useLocale();
const [open, setOpen] = useState(false);
const [mode, setMode] = useState<Mode>("wechat");
useEffect(() => {
if (!ready) return;
if (!shouldShow()) return;
let cancelled = false;
let timer = 0;
const china = Boolean(isChinaUser()) || locale === "zh";
setMode(china ? "wechat" : "x");
void (async () => {
let allow = false;
try {
const res = await fetch("/api/config", { cache: "no-store" });
const data = await res.json().catch(() => ({}));
const flags = data?.config?.flags;
// 双开关:总开关关 → 永不弹;自动弹须显式 true(默认 false,现网不变)
allow =
flags?.socialFollow !== false && flags?.socialFollowAuto === true;
} catch {
allow = false;
}
if (cancelled || !allow) return;
timer = window.setTimeout(() => setOpen(true), SHOW_DELAY_MS);
})();
return () => {
cancelled = true;
if (timer) window.clearTimeout(timer);
};
}, [ready, locale]);
const dismiss = useCallback(() => {
setOpen(false);
try {
localStorage.setItem(DISMISS_KEY, String(Date.now()));
} catch {
/* ignore */
}
}, []);
if (!open) return null;
const isZh = mode === "wechat";
return (
<div className={styles.root} role="dialog" aria-modal="true" aria-labelledby="social-follow-title">
<button type="button" className={styles.backdrop} aria-label="关闭" onClick={dismiss} />
<div className={styles.card}>
<div className={styles.glow} aria-hidden />
<button type="button" className={styles.close} onClick={dismiss} aria-label="关闭">
✕
</button>
<p className={styles.eyebrow}>{isZh ? "保持连接" : "Stay in the loop"}</p>
<h2 id="social-follow-title" className={styles.title}>
{isZh ? (
<>
关注 <span className={styles.accent}>公众号</span>
</>
) : (
<>
Follow us on <span className={styles.accent}>X</span>
</>
)}
</h2>
<p className={styles.lead}>
{isZh
? "获取出海导航、游民社群与会员更新,扫码即达。"
: "Nomad nav, community drops, and VIP updates — one follow away."}
</p>
{isZh ? (
<div className={styles.qrWrap}>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
className={styles.qr}
src={WECHAT_OA_QR}
alt="微信公众号二维码"
width={180}
height={180}
onError={(e) => {
(e.currentTarget as HTMLImageElement).style.display = "none";
const fallback = e.currentTarget.nextElementSibling;
if (fallback instanceof HTMLElement) fallback.hidden = false;
}}
/>
<div className={styles.qrFallback} hidden>
<span aria-hidden>📱</span>
<p>请搜索公众号「nomadro」</p>
</div>
</div>
) : (
<a
className={styles.cta}
href={X_URL}
target="_blank"
rel="noopener noreferrer"
onClick={dismiss}
>
<span aria-hidden>𝕏</span> Follow @nomadro
</a>
)}
<button type="button" className={styles.later} onClick={dismiss}>
{isZh ? "稍后再说" : "Maybe later"}
</button>
</div>
</div>
);
}