import { Input, type InputProps, Modal } from "@/components"; import { useMoonAccount } from "@/hooks"; import { shortenAddress } from "@/utils/shortenAddress"; import React, { useEffect, useMemo } from "react"; /** * Wallet Selector Modal * Reusable component to select a wallet from a list of moon wallets associated with the moon account. * Default behavior will set the selected moon wallet when a wallet is selected. * controlSelectedWallet prop can be set to false to disable this behavior. */ type WalletSelectorProps = { title?: string; headerProps?: React.HTMLAttributes; buttonProps?: React.ButtonHTMLAttributes; inputProps?: InputProps; listProps?: React.HTMLAttributes; listItemProps?: React.HTMLAttributes; modalProps?: React.HTMLAttributes; customButtonContent?: React.ReactNode; onChange?: (e: any) => void; controlSelectedWallet?: boolean; }; export const WalletSelectorModal = ({ title, headerProps, buttonProps, inputProps, listProps, listItemProps, modalProps, customButtonContent, onChange, controlSelectedWallet = true, }: WalletSelectorProps) => { const { accounts: wallets, setAccount: setWallet, account: wallet, createAccount: createWallet, listAccounts: listWallets, } = useMoonAccount(); const [isOpen, setIsOpen] = React.useState(false); const [searchTerm, setSearchTerm] = React.useState(""); const [selectedWallet, setSelectedWallet] = React.useState( null, ); useEffect(() => { if (!selectedWallet && wallet) setSelectedWallet(wallet); }, [wallet, selectedWallet]); useEffect(() => { if (!wallet && wallets.length > 0 && controlSelectedWallet) setWallet(wallets[0]); }, [wallet, wallets, setWallet]); const filteredWallets = useMemo(() => { return wallets.filter((wallet: string) => { if (!wallet) return false; return wallet.toLowerCase().includes(searchTerm.toLowerCase()); }); }, [wallets, searchTerm]); const toggleModal = (e?: any) => { e && e.preventDefault(); // Prevent form submission setIsOpen(!isOpen); }; const handleWalletSelect = (wallet: string) => { if (wallet && controlSelectedWallet) setWallet(wallet); setSelectedWallet(wallet); toggleModal(); const fakeEvent = { target: { value: wallet } }; onChange && onChange(fakeEvent); }; const createAndRefreshWallet = async () => { await createWallet({}); await listWallets(); }; return ( <> {customButtonContent ? ( ) : ( )} {title && (

{title}

)} setSearchTerm(e.target.value)} className="w-full p-2 mb-4 border border-gray-300 rounded" {...inputProps} />
{filteredWallets.map((wallet: string) => (
handleWalletSelect(wallet)} className="p-2 cursor-pointer hover:bg-accent-color rounded-sm" {...listItemProps} > {wallet}
))}
); };