import { useSnapshot } from 'valtio';
import { useState } from 'react';
import { Linking, ScrollView } from 'react-native';
import {
ApiController,
CoreHelperUtil,
EventsController,
ModalController,
OptionsController,
RouterController,
SnackController,
ConstantsUtil,
SwapController,
OnRampController,
ConnectionsController,
AssetController,
AssetUtil,
LogController
} from '@reown/appkit-core-react-native';
// import { ConstantsUtil as CommonConstantsUtil } from '@reown/appkit-common-react-native';
import {
Avatar,
Button,
FlexView,
IconLink,
Text,
UiUtil,
Spacing,
ListItem,
useCustomDimensions
} from '@reown/appkit-ui-react-native';
import { useInternalAppKit } from '../../AppKitContext';
import { AuthButtons } from './components/auth-buttons';
import styles from './styles';
export function AccountDefaultView() {
const { switchAccountType, disconnect } = useInternalAppKit();
const { loading } = useSnapshot(ModalController.state);
const {
activeAddress: address,
activeBalance: balance,
activeNetwork,
activeNamespace,
connection,
accountType,
identity
} = useSnapshot(ConnectionsController.state);
const account = address?.split(':')[2];
const [disconnecting, setDisconnecting] = useState(false);
const { features, isOnRampEnabled } = useSnapshot(OptionsController.state);
const { history } = useSnapshot(RouterController.state);
const { networkImages } = useSnapshot(AssetController.state);
const networkImage = AssetUtil.getNetworkImage(activeNetwork, networkImages);
const showCopy = OptionsController.isClipboardAvailable();
const isAuth = !!connection?.properties?.provider;
const showBalance = balance && !isAuth;
const showExplorer = Object.keys(activeNetwork?.blockExplorers ?? {}).length > 0 && !isAuth;
const showBack = history.length > 1;
const showSwitchAccountType = isAuth && activeNamespace === 'eip155';
const showActivity =
!isAuth &&
activeNetwork?.caipNetworkId &&
ConstantsUtil.ACTIVITY_SUPPORTED_CHAINS.includes(activeNetwork.caipNetworkId);
const showSwaps =
!isAuth &&
features?.swaps &&
activeNetwork?.caipNetworkId &&
ConstantsUtil.SWAP_SUPPORTED_NETWORKS.includes(activeNetwork.caipNetworkId);
const showSend =
!isAuth && activeNamespace && ConstantsUtil.SEND_SUPPORTED_NAMESPACES.includes(activeNamespace);
const showBuy =
!isAuth &&
isOnRampEnabled &&
activeNamespace &&
ConstantsUtil.ONRAMP_SUPPORTED_NAMESPACES.includes(activeNamespace);
const { padding } = useCustomDimensions();
async function onDisconnect() {
setDisconnecting(true);
await disconnect(ConnectionsController.state.activeNamespace);
setDisconnecting(false);
}
const onSwitchAccountType = async () => {
try {
const namespace = ConnectionsController.state.activeNamespace;
const network = ConnectionsController.state.activeNetwork;
if (isAuth && namespace && network) {
const newType = ConnectionsController.state.accountType === 'eoa' ? 'smartAccount' : 'eoa';
switchAccountType(namespace, newType, network);
}
} catch (error) {
LogController.sendError(error, 'AccountDefaultView.tsx', 'onSwitchAccountType');
SnackController.showError('Error switching account type');
}
};
const onExplorerPress = () => {
if (showExplorer && ConnectionsController.state.activeNetwork?.blockExplorers?.default?.url) {
Linking.openURL(ConnectionsController.state.activeNetwork?.blockExplorers?.default?.url);
}
};
const onCopyAddress = () => {
if (OptionsController.isClipboardAvailable() && ConnectionsController.state.activeAddress) {
const _address = CoreHelperUtil.getPlainAddress(ConnectionsController.state.activeAddress);
if (_address) {
OptionsController.copyToClipboard(_address);
SnackController.showSuccess('Address copied');
}
}
};
const onSwapPress = () => {
SwapController.clearTokens();
EventsController.sendEvent({
type: 'track',
event: 'OPEN_SWAP',
properties: {
network: ConnectionsController.state.activeNetwork?.caipNetworkId,
isSmartAccount: false
}
});
RouterController.push('Swap');
};
const onBuyPress = () => {
EventsController.sendEvent({
type: 'track',
event: 'SELECT_BUY_CRYPTO'
});
OnRampController.resetState();
RouterController.push('OnRamp');
};
const onActivityPress = () => {
RouterController.push('Transactions');
};
const onNetworkPress = () => {
RouterController.push('Networks');
EventsController.sendEvent({
type: 'track',
event: 'CLICK_NETWORKS'
});
};
const onUpgradePress = () => {
EventsController.sendEvent({ type: 'track', event: 'EMAIL_UPGRADE_FROM_MODAL' });
RouterController.push('UpgradeEmailWallet');
};
const onSendPress = () => {
const network = ConnectionsController.state.activeNetwork?.caipNetworkId;
const isSmartAccount = ConnectionsController.state.accountType === 'smartAccount';
EventsController.sendEvent({
type: 'track',
event: 'OPEN_SEND',
properties: { network, isSmartAccount }
});
RouterController.push('WalletSend');
};
const onEmailPress = async () => {
// TODO: Uncomment when email update is enabled
// const email = ConnectionsController.state.connection?.properties?.email;
// const provider = ConnectionsController.state.connection?.properties?.provider;
// if (provider !== 'email' || !email) return;
// const sessionTopic = ConnectionsController.state.connection?.properties?.sessionTopic;
// if (!sessionTopic) {
// throw new Error('Session topic not found');
// }
// const link = `${CommonConstantsUtil.WEB_WALLET_URL}/emailUpdate/${sessionTopic}`;
// await CoreHelperUtil.openLink(link);
// Subscribe to email update event
};
return (
<>
{showBack ? (
) : null}
{identity?.name
? UiUtil.getTruncateString({
string: identity?.name,
charsStart: 20,
charsEnd: 0,
truncate: 'end'
})
: UiUtil.getTruncateString({
string: account ?? '',
charsStart: 4,
charsEnd: 6,
truncate: 'middle'
})}
{showCopy ? (
) : null}
{showBalance ? (
{CoreHelperUtil.formatBalance(balance.amount, balance.symbol, 6)}
) : null}
{showExplorer ? (
) : null}
{isAuth ? (
) : null}
{activeNetwork?.name}
{showBuy ? (
Buy Crypto
) : null}
{showSend ? (
Send
) : null}
{showSwaps ? (
Swap
) : null}
{showActivity ? (
Activity
) : null}
{showSwitchAccountType ? (
{`Switch to your ${
accountType === 'eoa' ? 'Smart Account' : 'EOA'
}`}
) : null}
Disconnect
>
);
}