nomadro/components/social-follow.tsx
eric 9f8fec1723 Ship portal PC/H5 acceptance, VIP ScrollReader, admin analytics and ops anchors.
Adds /api/health, ntfy notify on config save, /admin/analytics charts, and deploy smoke for miniprogram config/API.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 23:52:00 -05:00

142 lines
4.3 KiB
TypeScript
Raw Permalink 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 */
const WECHAT_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 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 = true;
try {
const res = await fetch("/api/config", { cache: "no-store" });
const data = await res.json().catch(() => ({}));
if (data?.config?.flags?.socialFollow === false) allow = false;
} catch {
/* offline → show */
}
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}>
{/* Prefer local public asset; fallback emoji if missing */}
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
className={styles.qr}
src={WECHAT_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>
);
}