douyinWEB/frontend/src/components/PwaInstallBanner.tsx
eric 565081678b fix(pwa): guide China users to Add to Home Screen (WebAPK needs Google)
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-20 08:00:46 -05:00

155 lines
5.5 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 { useEffect, useState } from "react";
import { Download, X } from "lucide-react";
type BeforeInstallPromptEvent = Event & {
prompt: () => Promise<void>;
userChoice: Promise<{ outcome: "accepted" | "dismissed" }>;
};
const DISMISS_KEY = "dylive_pwa_install_dismissed";
function isStandalone(): boolean {
if (typeof window === "undefined") return false;
if (window.matchMedia("(display-mode: standalone)").matches) return true;
return Boolean((navigator as Navigator & { standalone?: boolean }).standalone);
}
export function PwaInstallBanner() {
const [deferred, setDeferred] = useState<BeforeInstallPromptEvent | null>(null);
const [visible, setVisible] = useState(false);
const [iosHint, setIosHint] = useState(false);
const [androidHint, setAndroidHint] = useState(false);
const [status, setStatus] = useState("");
const [busy, setBusy] = useState(false);
useEffect(() => {
if (typeof window === "undefined") return;
if (isStandalone()) return;
if (localStorage.getItem(DISMISS_KEY) === "1") return;
const ua = navigator.userAgent || "";
const isIos =
/iPad|iPhone|iPod/.test(ua) || (navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1);
const isSafari = /Safari/.test(ua) && !/CriOS|FxiOS|EdgiOS|Chrome/.test(ua);
const isAndroid = /Android/i.test(ua);
const isChrome = /Chrome|CriOS/i.test(ua) && !/Edg/i.test(ua);
const onBip = (e: Event) => {
e.preventDefault();
setDeferred(e as BeforeInstallPromptEvent);
setVisible(true);
setIosHint(false);
setAndroidHint(isAndroid);
setStatus("");
};
window.addEventListener("beforeinstallprompt", onBip);
const onInstalled = () => {
setStatus("已添加。请看桌面是否出现 LiveRadar(可能带 Chrome 角标)。");
setVisible(true);
setDeferred(null);
};
window.addEventListener("appinstalled", onInstalled);
if (isIos && isSafari) {
setIosHint(true);
setVisible(true);
} else if (isAndroid) {
setAndroidHint(true);
setVisible(true);
}
return () => {
window.removeEventListener("beforeinstallprompt", onBip);
window.removeEventListener("appinstalled", onInstalled);
};
}, []);
if (!visible) return null;
const dismiss = () => {
localStorage.setItem(DISMISS_KEY, "1");
setVisible(false);
setDeferred(null);
};
const install = async () => {
if (!deferred || busy) return;
setBusy(true);
setStatus("");
try {
await deferred.prompt();
const choice = await deferred.userChoice;
if (choice.outcome === "accepted") {
setStatus(
"若桌面/应用列表仍没有:国内无 Google 时「安装应用」常会静默失败。请改用 Chrome ⋮ →「添加到主屏幕」。",
);
setAndroidHint(true);
} else {
setStatus("已取消。请用 Chrome 右上角 ⋮ →「添加到主屏幕」(不要点「安装应用」)。");
setAndroidHint(true);
}
} catch {
setStatus("安装弹窗失败。请用 Chrome ⋮ →「添加到主屏幕」。");
setAndroidHint(true);
} finally {
setDeferred(null);
setBusy(false);
}
};
return (
<div className="pointer-events-none fixed inset-x-0 bottom-[calc(4.25rem+env(safe-area-inset-bottom))] z-30 px-3 md:bottom-4 md:left-auto md:right-4 md:max-w-sm">
<div className="pointer-events-auto rounded-2xl border border-teal-400/30 bg-[#0b1520]/95 p-3 shadow-xl backdrop-blur-md">
<div className="flex items-start gap-2">
<div className="mt-0.5 flex h-8 w-8 shrink-0 items-center justify-center rounded-xl bg-teal-500/15 text-teal-200">
<Download className="h-4 w-4" />
</div>
<div className="min-w-0 flex-1">
<p className="text-sm font-medium text-slate-100">添加到主屏幕</p>
<p className="mt-0.5 text-[11px] leading-relaxed text-slate-400">
{iosHint
? "Safari:底部分享 →「添加到主屏幕」。"
: androidHint
? "国内网络请用:Chrome 右上角 ⋮ →「添加到主屏幕」。不要用「安装应用」(要连 Google,没翻墙常会装不上、桌面和应用列表都没有)。"
: "可选添加到主屏幕,方便手机快速打开。"}
</p>
{status ? (
<p className="mt-1.5 text-[11px] leading-relaxed text-amber-100/90">{status}</p>
) : null}
<div className="mt-2 flex flex-wrap gap-2">
{deferred ? (
<button
type="button"
disabled={busy}
onClick={() => void install()}
className="rounded-lg bg-teal-500/20 px-3 py-1.5 text-xs text-teal-100 hover:bg-teal-500/30 disabled:opacity-50"
>
{busy ? "弹出中…" : "尝试安装"}
</button>
) : null}
<button
type="button"
onClick={dismiss}
className="rounded-lg border border-white/10 px-3 py-1.5 text-xs text-slate-400"
>
知道了
</button>
</div>
</div>
<button
type="button"
onClick={dismiss}
className="rounded-lg p-1 text-slate-500 hover:text-slate-200"
aria-label="关闭"
>
<X className="h-4 w-4" />
</button>
</div>
</div>
</div>
);
}