import { useSnapshot } from 'valtio'; import { useCallback, useEffect, useState } from 'react'; import { Platform, ScrollView } from 'react-native'; import { RouterController, ApiController, AssetUtil, WcController, CoreHelperUtil, OptionsController, EventsController, ConstantsUtil, AssetController, LogController, ConnectionsController } from '@reown/appkit-core-react-native'; import { Button, FlexView, LoadingThumbnail, WalletImage, Link, IconBox, useCustomDimensions } from '@reown/appkit-ui-react-native'; import { StoreLink } from './components/StoreLink'; import { ConnectingBody, getMessage, type BodyErrorType } from '../w3m-connecting-body'; import styles from './styles'; interface Props { onRetry: () => void; onCopyUri: (uri?: string) => void; isInstalled?: boolean; } export function ConnectingMobile({ onRetry, onCopyUri, isInstalled }: Props) { const { data } = RouterController.state; const { padding } = useCustomDimensions(); const { wcUri, wcError } = useSnapshot(WcController.state); const { walletImages } = useSnapshot(AssetController.state); const [errorType, setErrorType] = useState(); const showCopy = OptionsController.isClipboardAvailable() && errorType !== 'not_installed' && !CoreHelperUtil.isLinkModeURL(wcUri); const showRetry = errorType !== 'not_installed'; const bodyMessage = getMessage({ walletName: data?.wallet?.name, errorType, declined: wcError }); const storeUrl = Platform.select({ ios: data?.wallet?.app_store, android: data?.wallet?.play_store }); const onRetryPress = () => { setErrorType(undefined); WcController.setWcError(false); onRetry?.(); }; const onStorePress = () => { if (storeUrl) { CoreHelperUtil.openLink(storeUrl); EventsController.sendEvent({ type: 'track', event: 'GET_WALLET', properties: { name: data?.wallet?.name ?? 'Unknown', link: storeUrl, linkType: Platform.select({ ios: 'appstore', android: 'playstore', default: undefined }), explorerId: data?.wallet?.id, walletRank: data?.wallet?.order } }); } }; const onConnect = useCallback(async () => { try { const { name, mobile_link } = data?.wallet ?? {}; if (name && mobile_link && wcUri) { const { redirect, href } = CoreHelperUtil.formatNativeUrl(mobile_link, wcUri); const wcLinking = { name, href }; WcController.setWcLinking(wcLinking); WcController.setPressedWallet(data?.wallet); await CoreHelperUtil.openLink(redirect); await WcController.state.wcPromise; WcController.setConnectedWallet(wcLinking, data?.wallet); const address = ConnectionsController.state.activeAddress; const caipNetworkId = ConnectionsController.state.activeNetwork?.caipNetworkId; EventsController.sendEvent({ type: 'track', event: 'CONNECT_SUCCESS', address: CoreHelperUtil.getPlainAddress(address), properties: { method: 'mobile', name: data?.wallet?.name ?? 'Unknown', explorerId: data?.wallet?.id, caipNetworkId } }); } } catch (error: any) { LogController.sendError(error, 'ConnectingMobile.tsx', 'onConnect'); if (error.message.includes(ConstantsUtil.LINKING_ERROR)) { setErrorType('not_installed'); } else { setErrorType('default'); } } }, [wcUri, data]); useEffect(() => { if (wcUri) { onConnect(); } }, [wcUri, onConnect]); return ( {wcError ? ( ) : null} {showRetry ? ( ) : null} {showCopy ? ( onCopyUri(wcUri)} > Copy link ) : null} ); }