"use client";
import { useQuery } from "@tanstack/react-query";
import { useCallback, useEffect, useRef, useState } from "react";
import type { Chain } from "../../../../../chains/types.js";
import type { ThirdwebClient } from "../../../../../client/client.js";
import type { Wallet } from "../../../../../wallets/interfaces/wallet.js";
import type { SmartWalletOptions } from "../../../../../wallets/smart/types.js";
import type { WalletInfo } from "../../../../../wallets/wallet-info.js";
import { useConnectionManager } from "../../../../core/providers/connection-manager.js";
import { useWalletInfo } from "../../../../core/utils/wallet.js";
import { LoadingScreen } from "../../../wallets/shared/LoadingScreen.js";
import { getSmartWalletLocale } from "../../../wallets/smartWallet/locale/getSmartWalletLocale.js";
import type { SmartWalletLocale } from "../../../wallets/smartWallet/locale/types.js";
import { Container } from "../../components/basic.js";
import { Spacer } from "../../components/Spacer.js";
import { Spinner } from "../../components/Spinner.js";
import { Text } from "../../components/text.js";
import type { LocaleId } from "../../types.js";
import type { ConnectLocale } from "../locale/types.js";
import { AnyWalletConnectUI } from "./AnyWalletConnectUI.js";
/**
* @internal
*/
export function SmartConnectUI(props: {
personalWallet: Wallet;
done: (smartWallet: Wallet) => void;
onBack?: () => void;
accountAbstraction: SmartWalletOptions;
setModalVisibility: (value: boolean) => void;
meta: {
title?: string;
titleIconUrl?: string;
showThirdwebBranding?: boolean;
termsOfServiceUrl?: string;
privacyPolicyUrl?: string;
};
size: "compact" | "wide";
client: ThirdwebClient;
chain: Chain | undefined;
chains: Chain[] | undefined;
connectLocale: ConnectLocale;
walletConnect:
| {
projectId?: string;
}
| undefined;
}) {
const personalWalletInfo = useWalletInfo(props.personalWallet.id);
const [keyConnected, setKeyConnected] = useState(false);
if (!personalWalletInfo.data) {
return ;
}
// connect personal wallet
if (!keyConnected) {
return (
{
setKeyConnected(true);
}}
meta={props.meta}
onBack={props.onBack}
setModalVisibility={props.setModalVisibility}
size={props.size}
wallet={props.personalWallet}
walletConnect={props.walletConnect}
/>
);
}
return (
);
}
function SmartWalletConnecting(props: {
done: (smartWallet: Wallet) => void;
personalWallet: Wallet;
accountAbstraction: SmartWalletOptions;
onBack?: () => void;
personalWalletInfo: WalletInfo;
localeId: LocaleId;
size: "compact" | "wide";
client: ThirdwebClient;
}) {
const localeQuery = useQuery({
queryFn: () => getSmartWalletLocale(props.localeId),
queryKey: ["getSmartWalletLocale", props.localeId],
});
const { personalWallet } = props;
const { done } = props;
const [smartWalletConnectionStatus, setSmartWalletConnectionStatus] =
useState<"connecting" | "connect-error" | "idle">("idle");
const connectionManager = useConnectionManager();
const handleConnect = useCallback(async () => {
if (!personalWallet) {
throw new Error("No personal wallet");
}
setSmartWalletConnectionStatus("connecting");
try {
const connected = await connectionManager.handleConnection(
personalWallet,
{
accountAbstraction: props.accountAbstraction,
client: props.client,
},
);
done(connected);
setSmartWalletConnectionStatus("idle");
} catch (e) {
console.error(e);
setSmartWalletConnectionStatus("connect-error");
}
}, [
done,
personalWallet,
props.client,
props.accountAbstraction,
connectionManager,
]);
const connectStarted = useRef(false);
useEffect(() => {
if (!connectStarted.current) {
handleConnect();
connectStarted.current = true;
}
}, [handleConnect]);
if (!localeQuery.data) {
return ;
}
if (smartWalletConnectionStatus === "connect-error") {
return (
{localeQuery.data.failedToConnect}
);
}
return (
{localeQuery.data.connecting}
);
}