import { IconArrowLeft, IconWallet } from "@/assets/icons"; import { useConnectToMoon } from "@/hooks"; import { useRef } from "react"; import { useAccount, useConnect, useSignMessage } from "wagmi"; /** * Props for the WalletConnectorList component. * * @interface WalletConnectorsListProps * @property {Function} onBack - Callback function to handle the back action. */ interface WalletConnectorsListProps { onBack: any; } /** * WalletConnectorsList component renders a list of wallet connectors and allows the user to connect to a wallet. * * @param {WalletConnectorsListProps} props - The properties for the WalletConnectorsList component. * @param {function} props.onBack - Callback function to handle the back button click event. * * @returns {JSX.Element} The rendered WalletConnectorsList component. * * @example * console.log('Back button clicked')} /> * * @remarks * This component uses the `useConnect`, `useAccount`, `useSignMessage`, and `useConnectToMoon` hooks to manage wallet connections. * It filters out duplicate connectors and displays a list of unique connectors. * When a connector is clicked, it triggers the `handleConnect` function to initiate the connection process. * If the user is already connected, it automatically calls `connectToMoonSiwe`. */ export const WalletConnectorsList = ({ onBack }: WalletConnectorsListProps) => { const { connectors, connect } = useConnect(); const { address, isConnected } = useAccount(); const { signMessageAsync } = useSignMessage(); const walletAddress = address ? address : ""; const { connectToMoonSiwe } = useConnectToMoon({ address: walletAddress, signMessageAsync, }); const hasAttemptedConnection = useRef(false); // useEffect(() => { // if (isConnected && address && !hasAttemptedConnection.current) { // // connectToMoonSiwe(); // hasAttemptedConnection.current = true; // } // }, [isConnected, address, connectToMoonSiwe]); const handleConnect = async (connector: any) => { if (isConnected) { if (!hasAttemptedConnection.current) { connectToMoonSiwe(); hasAttemptedConnection.current = true; } return; } connect({ connector }); }; //remove duplicate connectors const uniqueConnectors = connectors.filter( (v: any, i: any, a: any) => a.findIndex((t: any) => t.name === v.name) === i, ); return (
Back

Connect to a wallet

{uniqueConnectors && uniqueConnectors.map((connector: any) => (
))}
); };