"use client"; import { useEffect, useRef, useState } from "react"; import type { ThirdwebClient } from "../../../../client/client.js"; import { webLocalStorage } from "../../../../utils/storage/webStorage.js"; import { isEcosystemWallet } from "../../../../wallets/ecosystem/is-ecosystem-wallet.js"; import type { Wallet } from "../../../../wallets/interfaces/wallet.js"; import { setLastAuthProvider } from "../../../core/utils/storage.js"; import type { ConnectLocale } from "../../ui/ConnectWallet/locale/types.js"; import { Container, ModalHeader } from "../../ui/components/basic.js"; import { Button } from "../../ui/components/buttons.js"; import { Spacer } from "../../ui/components/Spacer.js"; import { Spinner } from "../../ui/components/Spinner.js"; import { Text } from "../../ui/components/text.js"; import type { ConnectWalletSelectUIState } from "./ConnectWalletSocialOptions.js"; import type { InAppWalletLocale } from "./locale/types.js"; /** * @internal */ export function GuestLogin(props: { locale: InAppWalletLocale; wallet: Wallet; done: () => void; goBack?: () => void; state: ConnectWalletSelectUIState; size: "compact" | "wide"; client: ThirdwebClient; connectLocale: ConnectLocale; }) { const ewLocale = props.locale; const locale = ewLocale.socialLoginScreen; const [authError, setAuthError] = useState(undefined); const { done, wallet } = props; const [status, setStatus] = useState<"connecting" | "connected" | "error">( "connecting", ); const handleGuestLogin = async () => { const connectOptions = { client: props.client, ecosystem: isEcosystemWallet(wallet) ? { id: wallet.id, partnerId: wallet.getConfig()?.partnerId, } : undefined, strategy: "guest" as const, }; try { await wallet.connect(connectOptions); await setLastAuthProvider("guest", webLocalStorage); setStatus("connected"); done(); } catch (e) { setStatus("error"); // TODO this only happens on 'retry' button click, not on initial login // should pass auth error message to this component if ( e instanceof Error && e?.message?.includes("PAYMENT_METHOD_REQUIRED") ) { setAuthError(ewLocale.maxAccountsExceeded); } console.error("Error generating guest account", e); } }; const guestLogin = props.state?.guestLogin; const socialLoginStarted = useRef(false); useEffect(() => { if (socialLoginStarted.current) { return; } if (guestLogin) { socialLoginStarted.current = true; setStatus("connecting"); guestLogin.connectionPromise .then(() => { done(); setStatus("connected"); }) .catch((e) => { setAuthError(e.message); setStatus("error"); }); } }, [done, guestLogin]); return ( {props.goBack && ( )} {props.size === "compact" ? : null} {status !== "error" && ( Generating your guest account )} {status === "error" && ( {authError ? ( {authError} ) : ( {locale.failed} )} )} ); }