import { useEffect, useState } from "react"; import { useI18n } from "../i18n"; import { openWalletDeepLink } from "./deepLinks"; import { getInjectedWallet, type WalletKind } from "./injected"; import { useWallet } from "./WalletProvider"; type ModalState = "idle" | "signing"; type WalletOption = { kind: WalletKind; labelKey: string; }; const walletOptions: WalletOption[] = [ { kind: "tokenPocket", labelKey: "walletTokenPocket" }, { kind: "metaMask", labelKey: "walletMetaMask" }, { kind: "imToken", labelKey: "walletImToken" }, ]; function isMobileDevice(): boolean { if (typeof navigator === "undefined") return false; const ua = navigator.userAgent || ""; return ( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile/i.test( ua, ) || (/Macintosh/i.test(ua) && navigator.maxTouchPoints > 1) ); } export function WalletLoginModal() { const { t } = useI18n(); const { closeLoginModal, loginModalOpen, signInInjected } = useWallet(); const [state, setState] = useState("idle"); const [error, setError] = useState(""); const [mobileDevice, setMobileDevice] = useState(false); const [selectedWallet, setSelectedWallet] = useState(null); useEffect(() => { if (!loginModalOpen) return; setMobileDevice(isMobileDevice()); setSelectedWallet(null); setError(""); }, [loginModalOpen]); const close = () => { if (state === "signing") return; closeLoginModal(); setError(""); }; if (!loginModalOpen) return null; const walletName = (kind: WalletKind) => { if (kind === "tokenPocket") return t("walletTokenPocket"); if (kind === "metaMask") return t("walletMetaMask"); return t("walletImToken"); }; const chooseWallet = async (kind: WalletKind) => { setError(""); setSelectedWallet(kind); const injectedWallet = getInjectedWallet(kind); if (!injectedWallet) { if (mobileDevice) { openWalletDeepLink(kind); return; } setError( t("walletInstallSelected").replace("{wallet}", walletName(kind)), ); return; } setState("signing"); await signInInjected(kind) .catch((err) => { setError(err instanceof Error ? err.message : t("walletLoginFailed")); }) .finally(() => setState("idle")); }; return (

{t("walletLoginTitle")}

{t("walletLoginDesc")}

{mobileDevice ? t("walletChooseMobile") : t("walletChooseDesktop")}

{walletOptions.map((option) => { const signingThis = state === "signing" && selectedWallet === option.kind; return ( ); })}
{!mobileDevice ? (

{t("walletDesktopHint")}

) : null}
{error ? (

{error}

) : null}
); }