Skip to content

Commit f4ca4a4

Browse files
committed
Decouple input disable logic from amount type support
1 parent 1e877a7 commit f4ca4a4

File tree

1 file changed

+38
-33
lines changed

1 file changed

+38
-33
lines changed

src/components/scenes/RampCreateScene.tsx

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,6 @@ type Props = (
7979
direction: 'buy' | 'sell'
8080
}
8181

82-
// Helper function to determine which input types should be disabled
83-
interface AmountTypeSupport {
84-
fiatInputDisabled: boolean
85-
cryptoInputDisabled: boolean
86-
}
87-
8882
export const RampCreateScene: React.FC<Props> = (props: Props) => {
8983
const { direction, navigation, route } = props
9084
const { regionCode: initialRegionCode, forcedWalletResult } =
@@ -220,8 +214,7 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
220214
}, [selectedFiatCurrencyCode])
221215

222216
// Determine which input types should be disabled
223-
const { fiatInputDisabled, cryptoInputDisabled } =
224-
getAmountTypeSupport(supportedPlugins)
217+
const amountTypeSupport = getAmountTypeSupport(supportedPlugins)
225218

226219
const { data: fiatUsdRate } = useQuery({
227220
queryKey: ['fiatUsdRate', selectedFiatCurrencyCode],
@@ -247,7 +240,7 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
247240
// Don't override if the user has started typing or fiat input is disabled
248241
if (
249242
hasAppliedInitialAmount.current ||
250-
fiatInputDisabled ||
243+
amountTypeSupport.onlyCrypto ||
251244
'empty' in exchangeAmount ||
252245
lastUsedInput != null ||
253246
shouldShowRegionSelect
@@ -276,7 +269,7 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
276269
abort = true
277270
}
278271
}, [
279-
fiatInputDisabled,
272+
amountTypeSupport.onlyCrypto,
280273
isLightAccount,
281274
lastUsedInput,
282275
selectedWallet,
@@ -301,8 +294,8 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
301294

302295
// Guard against creating request with disabled input type
303296
if (
304-
(lastUsedInput === 'fiat' && fiatInputDisabled) ||
305-
(lastUsedInput === 'crypto' && cryptoInputDisabled)
297+
(lastUsedInput === 'fiat' && amountTypeSupport.onlyCrypto) ||
298+
(lastUsedInput === 'crypto' && amountTypeSupport.onlyFiat)
306299
) {
307300
return null
308301
}
@@ -330,8 +323,8 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
330323
lastUsedInput,
331324
countryCode,
332325
stateProvinceCode,
333-
fiatInputDisabled,
334-
cryptoInputDisabled,
326+
amountTypeSupport.onlyCrypto,
327+
amountTypeSupport.onlyFiat,
335328
direction
336329
])
337330

@@ -390,7 +383,7 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
390383
// Derived state for display values
391384
const displayFiatAmount = React.useMemo(() => {
392385
// Don't show any value if fiat input is disabled
393-
if (fiatInputDisabled) return ''
386+
if (amountTypeSupport.onlyCrypto) return ''
394387
if ('empty' in exchangeAmount) return ''
395388

396389
if ('max' in exchangeAmount) {
@@ -416,7 +409,7 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
416409
return ''
417410
}
418411
}, [
419-
fiatInputDisabled,
412+
amountTypeSupport.onlyCrypto,
420413
maxQuoteForMaxFlow,
421414
exchangeAmount,
422415
lastUsedInput,
@@ -425,7 +418,7 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
425418

426419
const displayCryptoAmount = React.useMemo(() => {
427420
// Don't show any value if crypto input is disabled
428-
if (cryptoInputDisabled) return ''
421+
if (amountTypeSupport.onlyFiat) return ''
429422
if ('empty' in exchangeAmount || lastUsedInput === null) return ''
430423

431424
if ('max' in exchangeAmount) {
@@ -451,7 +444,7 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
451444
return ''
452445
}
453446
}, [
454-
cryptoInputDisabled,
447+
amountTypeSupport.onlyFiat,
455448
maxQuoteForMaxFlow,
456449
exchangeAmount,
457450
lastUsedInput,
@@ -596,11 +589,14 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
596589
!isLoadingQuotes
597590
) {
598591
// Persist the chosen amount so it remains after returning
599-
if (!fiatInputDisabled && maxQuoteForMaxFlow.fiatAmount != null) {
592+
if (
593+
!amountTypeSupport.onlyCrypto &&
594+
maxQuoteForMaxFlow.fiatAmount != null
595+
) {
600596
setLastUsedInput('fiat')
601597
setExchangeAmount({ amount: maxQuoteForMaxFlow.fiatAmount })
602598
} else if (
603-
!cryptoInputDisabled &&
599+
!amountTypeSupport.onlyFiat &&
604600
maxQuoteForMaxFlow.cryptoAmount != null
605601
) {
606602
setLastUsedInput('crypto')
@@ -619,8 +615,8 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
619615
isLoadingQuotes,
620616
rampQuoteRequest,
621617
navigation,
622-
fiatInputDisabled,
623-
cryptoInputDisabled,
618+
amountTypeSupport.onlyCrypto,
619+
amountTypeSupport.onlyFiat,
624620
exchangeAmount
625621
])
626622

@@ -641,6 +637,13 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
641637
)
642638
}
643639

640+
const fiatInputDisabled =
641+
'max' in exchangeAmount || amountTypeSupport.onlyCrypto
642+
const cryptoInputDisabled =
643+
isLoadingPersistedCryptoSelection ||
644+
'max' in exchangeAmount ||
645+
amountTypeSupport.onlyFiat
646+
644647
// Render trade form view
645648
return (
646649
<>
@@ -697,7 +700,7 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
697700
maxDecimals={2}
698701
returnKeyType="done"
699702
showSpinner={isFetchingQuotes && lastUsedInput === 'crypto'}
700-
disabled={'max' in exchangeAmount || fiatInputDisabled}
703+
disabled={fiatInputDisabled}
701704
expand
702705
/>
703706
</View>
@@ -742,11 +745,7 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
742745
maxDecimals={6}
743746
returnKeyType="done"
744747
showSpinner={isFetchingQuotes && lastUsedInput === 'fiat'}
745-
disabled={
746-
isLoadingPersistedCryptoSelection ||
747-
'max' in exchangeAmount ||
748-
cryptoInputDisabled
749-
}
748+
disabled={cryptoInputDisabled}
750749
expand
751750
/>
752751
</>
@@ -861,8 +860,8 @@ export const RampCreateScene: React.FC<Props> = (props: Props) => {
861860
lastUsedInput === null ||
862861
supportedPlugins.length === 0 ||
863862
sortedQuotes.length === 0 ||
864-
(lastUsedInput === 'fiat' && fiatInputDisabled) ||
865-
(lastUsedInput === 'crypto' && cryptoInputDisabled)
863+
(lastUsedInput === 'fiat' && amountTypeSupport.onlyCrypto) ||
864+
(lastUsedInput === 'crypto' && amountTypeSupport.onlyFiat)
866865
}
867866
/>
868867
</>
@@ -943,11 +942,17 @@ const getStyles = cacheStyles((theme: ReturnType<typeof useTheme>) => ({
943942
}
944943
}))
945944

945+
// Helper function to determine which input types should be disabled
946+
interface AmountTypeSupport {
947+
onlyCrypto: boolean
948+
onlyFiat: boolean
949+
}
950+
946951
function getAmountTypeSupport(
947952
supportedPlugins: SupportedPluginResult[]
948953
): AmountTypeSupport {
949954
if (supportedPlugins.length === 0) {
950-
return { fiatInputDisabled: false, cryptoInputDisabled: false }
955+
return { onlyCrypto: false, onlyFiat: false }
951956
}
952957

953958
// Collect all supported amount types from all plugins
@@ -973,8 +978,8 @@ function getAmountTypeSupport(
973978
allSupportedTypes.has('crypto') && !allSupportedTypes.has('fiat')
974979

975980
return {
976-
fiatInputDisabled: onlyCrypto,
977-
cryptoInputDisabled: onlyFiat
981+
onlyCrypto,
982+
onlyFiat
978983
}
979984
}
980985

0 commit comments

Comments
 (0)