- 
  Notifications
 You must be signed in to change notification settings 
- Fork 275
Add a loading state for rampLastCryptoSelection #5782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -15,6 +15,7 @@ import { | |
| import { FLAG_LOGO_URL } from '../../constants/CdnConstants' | ||
| import { COUNTRY_CODES, FIAT_COUNTRY } from '../../constants/CountryConstants' | ||
| import { useHandler } from '../../hooks/useHandler' | ||
| import { useRampLastCryptoSelection } from '../../hooks/useRampLastCryptoSelection' | ||
| import { useRampPlugins } from '../../hooks/useRampPlugins' | ||
| import { useRampQuotes } from '../../hooks/useRampQuotes' | ||
| import { | ||
|  | @@ -95,9 +96,6 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => { | |
| const rampLastFiatCurrencyCode = useSelector( | ||
| state => state.ui.settings.rampLastFiatCurrencyCode | ||
| ) | ||
| const rampLastCryptoSelection = useSelector( | ||
| state => state.ui.settings.rampLastCryptoSelection | ||
| ) | ||
|  | ||
| // State for trade form | ||
| const [userInput, setUserInput] = useState('') | ||
|  | @@ -112,23 +110,12 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => { | |
| const defaultFiat = useSelector(state => getDefaultFiat(state)) | ||
| const selectedFiatCurrencyCode = rampLastFiatCurrencyCode ?? defaultFiat | ||
|  | ||
| const persistedCryptoSelection = React.useMemo< | ||
| WalletListWalletResult | undefined | ||
| >(() => { | ||
| if ( | ||
| rampLastCryptoSelection == null || | ||
| currencyWallets[rampLastCryptoSelection.walletId] == null | ||
| ) { | ||
| return undefined | ||
| } | ||
| return { | ||
| type: 'wallet', | ||
| walletId: rampLastCryptoSelection.walletId, | ||
| tokenId: rampLastCryptoSelection.tokenId | ||
| } | ||
| }, [currencyWallets, rampLastCryptoSelection]) | ||
| const { | ||
| selection: rampLastCryptoSelection, | ||
| isLoading: isLoadingPersistedCryptoSelection | ||
| } = useRampLastCryptoSelection() | ||
|  | ||
| const selectedCrypto = forcedWalletResult ?? persistedCryptoSelection | ||
| const selectedCrypto = forcedWalletResult ?? rampLastCryptoSelection | ||
|  | ||
| const [selectedWallet, selectedCryptoCurrencyCode] = | ||
| selectedCrypto != null | ||
|  | @@ -783,7 +770,8 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => { | |
|  | ||
| {/* Bottom Input (Crypto by design) */} | ||
| <View style={styles.inputRowView}> | ||
| {selectedCryptoCurrencyCode == null ? ( | ||
| {selectedCryptoCurrencyCode == null && | ||
| !isLoadingPersistedCryptoSelection ? ( | ||
| <EdgeButton | ||
| type="secondary" | ||
| onPress={handleCryptDropdown} | ||
|  | @@ -796,7 +784,10 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => { | |
| ) : ( | ||
| <> | ||
| <DropdownInputButton onPress={handleCryptDropdown}> | ||
| {selectedCrypto == null || selectedWallet == null ? null : ( | ||
| {isLoadingPersistedCryptoSelection ? ( | ||
| <ActivityIndicator /> | ||
| ) : selectedCrypto == null || | ||
| selectedWallet == null ? null : ( | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Crypto Amount Input Renders PrematurelyWhen the crypto selection is loading, the crypto amount input renders even if  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see Cursor's point, but it shouldn't happen because  If we want to be paranoid, we could put an extra  | ||
| <CryptoIcon | ||
| sizeRem={1.5} | ||
| pluginId={selectedWallet?.currencyInfo.pluginId ?? ''} | ||
|  | @@ -817,7 +808,11 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => { | |
| maxDecimals={6} | ||
| returnKeyType="done" | ||
| showSpinner={isFetchingQuotes && lastUsedInput === 'fiat'} | ||
| disabled={isMaxAmount || cryptoInputDisabled} | ||
| disabled={ | ||
| isLoadingPersistedCryptoSelection || | ||
| isMaxAmount || | ||
| cryptoInputDisabled | ||
| } | ||
| expand | ||
| /> | ||
| </> | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { useQuery } from '@tanstack/react-query' | ||
|  | ||
| import type { WalletListWalletResult } from '../components/modals/WalletListModal' | ||
| import { useSelector } from '../types/reactRedux' | ||
| import { useWatch } from './useWatch' | ||
|  | ||
| interface UseRampsPersistedCryptoSelectionResult { | ||
| selection?: WalletListWalletResult | ||
| isLoading: boolean | ||
| } | ||
|  | ||
| export const useRampLastCryptoSelection = | ||
| (): UseRampsPersistedCryptoSelectionResult => { | ||
| const account = useSelector(state => state.core.account) | ||
| const currencyWallets = useWatch(account, 'currencyWallets') | ||
|  | ||
| const rampLastCryptoSelection = useSelector( | ||
| state => state.ui.settings.rampLastCryptoSelection | ||
| ) | ||
|  | ||
| const { data: allWalletsReady = false } = useQuery<boolean>({ | ||
| queryKey: ['waitForAllWallets', account?.id], | ||
| queryFn: async () => { | ||
| if (account == null) return false | ||
| await account.waitForAllWallets() | ||
| return true | ||
| }, | ||
| enabled: account != null, | ||
| staleTime: Infinity, | ||
| gcTime: 300000, | ||
| refetchOnWindowFocus: false | ||
| }) | ||
|  | ||
| if (rampLastCryptoSelection == null) { | ||
| return { selection: undefined, isLoading: false } | ||
| } | ||
|  | ||
| const { walletId, tokenId } = rampLastCryptoSelection | ||
|  | ||
| if (currencyWallets[walletId] == null) { | ||
| // Before we know wallet readiness, keep loading state true | ||
| if (!allWalletsReady) return { selection: undefined, isLoading: true } | ||
| // Otherwise we know there is not wallet for the selection (invalid | ||
| // selection state). | ||
| return { selection: undefined, isLoading: false } | ||
| } | ||
|  | ||
| return { | ||
| selection: { | ||
| type: 'wallet', | ||
| walletId, | ||
| tokenId | ||
| }, | ||
| isLoading: false | ||
| } | ||
| } |