import { Avatar, ListItemAvatar } from '@mui/material'; import { toUserFriendlyAddress } from '@tonconnect/ui'; import { ListItemButton } from '../../components/ListItemButton.js'; import { ListItemText } from '../../components/ListItemText.js'; import { useNavigateBack } from '../../hooks/useNavigateBack.js'; import { useWidgetEvents } from '../../hooks/useWidgetEvents.js'; import { ChainId } from '../../types/base.js'; import { WidgetEvent } from '../../types/events.js'; import { ChainType } from '../../utils/chainType.js'; import type { TVMWallet } from '../../types/wallet.js'; import { useTon } from '../../providers/WalletProvider/TVM/TVMBaseProvider.js'; import { useEffect, useRef } from 'react'; import { useChainSelect } from '../../components/ChainSelect/useChainSelect.js'; interface TVMListItemButtonProps { wallet: TVMWallet; } export const TVMListItemButton = ({ wallet }: TVMListItemButtonProps) => { const { navigateBack } = useNavigateBack(); const emitter = useWidgetEvents(); const { tonConnectUI } = useTon(); const { setCurrentChain } = useChainSelect('from'); const effectRan = useRef(false); const userFriendlyAddress = tonConnectUI?.account?.address ? toUserFriendlyAddress(tonConnectUI.account.address, false) : ''; const handleNavigateBack = () => { emitter.emit(WidgetEvent.WalletConnected, { address: userFriendlyAddress, chainId: ChainId.TON, chainType: ChainType.TVM, }); setCurrentChain(ChainId.TON); navigateBack(); }; useEffect(() => { if (!effectRan.current) { tonConnectUI?.onModalStateChange((state) => { if (state.closeReason === 'wallet-selected') { handleNavigateBack(); } }); } return () => { effectRan.current = true; tonConnectUI?.onModalStateChange(() => {}); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const connect = async () => { if (tonConnectUI?.connected) { handleNavigateBack(); return; } tonConnectUI?.openModal(); }; return ( {wallet.name[0]} ); };