import { useSnapshot } from 'valtio';
import { memo, useCallback, useEffect, useState } from 'react';
import { ScrollView } from 'react-native';
import {
OnRampController,
ThemeController,
RouterController,
type OnRampControllerState,
SnackController,
ConstantsUtil,
ConnectionsController,
AssetController,
AssetUtil
} from '@reown/appkit-core-react-native';
import {
Button,
FlexView,
Image,
Text,
TokenButton,
useTheme
} from '@reown/appkit-ui-react-native';
import {
NumberUtil,
StringUtil,
type OnRampCryptoCurrency
} from '@reown/appkit-common-react-native';
import { SelectorModal } from '../../partials/w3m-selector-modal';
import { Currency, ITEM_HEIGHT as CURRENCY_ITEM_HEIGHT } from './components/Currency';
import { getPurchaseCurrencies, getQuotesDebounced } from './utils';
import { CurrencyInput } from './components/CurrencyInput';
import { SelectPaymentModal } from './components/SelectPaymentModal';
import { Header } from './components/Header';
import { LoadingView } from './components/LoadingView';
import PaymentButton from './components/PaymentButton';
import styles from './styles';
const MemoizedCurrency = memo(Currency);
export function OnRampView() {
const { themeMode } = useSnapshot(ThemeController.state);
const Theme = useTheme();
const {
purchaseCurrency,
paymentCurrency,
selectedPaymentMethod,
paymentAmount,
quotesLoading,
quotes,
selectedQuote,
error,
loading,
initialLoading
} = useSnapshot(OnRampController.state) as OnRampControllerState;
const { activeNetwork } = useSnapshot(ConnectionsController.state);
const { networkImages } = useSnapshot(AssetController.state);
const [searchValue, setSearchValue] = useState('');
const [isCurrencyModalVisible, setIsCurrencyModalVisible] = useState(false);
const [isPaymentMethodModalVisible, setIsPaymentMethodModalVisible] = useState(false);
const purchaseCurrencyCode =
purchaseCurrency?.currencyCode?.split('_')[0] ?? purchaseCurrency?.currencyCode;
const networkImage = AssetUtil.getNetworkImage(activeNetwork, networkImages);
const isContinueDisabled = quotesLoading || loading || !selectedQuote;
const getQuotes = useCallback(() => {
if (OnRampController.canGenerateQuote()) {
OnRampController.getQuotes();
}
}, []);
const getPaymentButtonTitle = () => {
if (selectedPaymentMethod) {
return selectedPaymentMethod.name;
}
if (quotesLoading) {
return 'Loading quotes';
}
if (!paymentAmount || quotes?.length === 0) {
return 'Enter a valid amount';
}
return '';
};
const getPaymentButtonSubtitle = () => {
if (selectedQuote) {
return StringUtil.capitalize(selectedQuote?.serviceProvider);
}
if (selectedPaymentMethod) {
if (quotesLoading) {
return 'Loading quotes';
}
if (!paymentAmount || quotes?.length === 0) {
return 'Enter a valid amount';
}
}
return undefined;
};
const onValueChange = (value: number) => {
if (!value) {
OnRampController.abortGetQuotes();
OnRampController.setPaymentAmount(0);
OnRampController.setSelectedQuote(undefined);
OnRampController.clearError();
return;
}
OnRampController.setPaymentAmount(value);
getQuotesDebounced();
};
const handleSearch = (value: string) => {
setSearchValue(value);
};
const handleContinue = async () => {
if (OnRampController.state.selectedQuote) {
RouterController.push('OnRampCheckout');
}
};
const renderCurrencyItem = ({ item }: { item: OnRampCryptoCurrency }) => {
return (
);
};
const onPressPurchaseCurrency = (item: any) => {
setIsCurrencyModalVisible(false);
setIsPaymentMethodModalVisible(false);
setSearchValue('');
OnRampController.setPurchaseCurrency(item as OnRampCryptoCurrency);
getQuotes();
};
const onModalClose = () => {
setSearchValue('');
setIsCurrencyModalVisible(false);
setIsPaymentMethodModalVisible(false);
};
useEffect(() => {
if (error?.type === ConstantsUtil.ONRAMP_ERROR_TYPES.FAILED_TO_LOAD) {
SnackController.showInternalError({
shortMessage: 'Failed to load data. Please try again later.',
longMessage: error?.message
});
RouterController.goBack();
}
}, [error]);
useEffect(() => {
if (OnRampController.state.countries.length === 0) {
OnRampController.loadOnRampData();
}
}, []);
if (initialLoading || OnRampController.state.countries.length === 0) {
return ;
}
return (
<>
RouterController.push('OnRampSettings')} />
You Buy
setIsCurrencyModalVisible(true)}
testID="currency-selector"
chevron
renderClip={
networkImage ? (
) : null
}
/>
setIsPaymentMethodModalVisible(true)}
testID="payment-method-button"
/>
item.currencyCode}
title="Select Token"
itemHeight={CURRENCY_ITEM_HEIGHT}
showNetwork
/>
>
);
}