Commit 2aa4ec6a authored by Zach Pomerantz's avatar Zach Pomerantz Committed by GitHub

fix: clear or retain amounts on token select (#5161)

* fix: clear or retain amounts on token select

* lint: add curlies
parent a70ef432
...@@ -48,7 +48,7 @@ export function useSyncWidgetInputs({ ...@@ -48,7 +48,7 @@ export function useSyncWidgetInputs({
if (origin === 'max') { if (origin === 'max') {
sendAnalyticsEvent(EventName.SWAP_MAX_TOKEN_AMOUNT_SELECTED, { ...trace }) sendAnalyticsEvent(EventName.SWAP_MAX_TOKEN_AMOUNT_SELECTED, { ...trace })
} }
setType(toTradeType(field)) setType(field === Field.INPUT ? TradeType.EXACT_INPUT : TradeType.EXACT_OUTPUT)
setAmount(amount) setAmount(amount)
}, },
[trace] [trace]
...@@ -71,20 +71,34 @@ export function useSyncWidgetInputs({ ...@@ -71,20 +71,34 @@ export function useSyncWidgetInputs({
}, []) }, [])
const onTokenSelect = useCallback( const onTokenSelect = useCallback(
(token: Currency) => { (selectingToken: Currency) => {
if (selectingField === undefined) return if (selectingField === undefined) return
setType(toTradeType(selectingField))
const otherField = invertField(selectingField) const otherField = invertField(selectingField)
let otherToken = tokens[otherField] const isFlip = tokens[otherField]?.equals(selectingToken)
otherToken = otherToken?.equals(token) ? tokens[selectingField] : otherToken const update: SwapTokens = {
const update = { [selectingField]: selectingToken,
[selectingField]: token, [otherField]: isFlip ? tokens[selectingField] : tokens[otherField],
[otherField]: otherToken,
default: tokens.default, default: tokens.default,
} }
setType((type) => {
// If flipping the tokens, also flip the type/amount.
if (isFlip) {
return invertTradeType(type)
}
// Setting a new token should clear its amount, if it is set.
const activeField = type === TradeType.EXACT_INPUT ? Field.INPUT : Field.OUTPUT
if (selectingField === activeField) {
setAmount(() => EMPTY_AMOUNT)
}
return type
})
if (!includesDefaultToken(update)) { if (!includesDefaultToken(update)) {
onTokenChange?.(update[Field.OUTPUT] || update[Field.INPUT] || token) onTokenChange?.(update[Field.OUTPUT] || selectingToken)
} }
setTokens(update) setTokens(update)
}, },
...@@ -127,16 +141,6 @@ function invertField(field: Field) { ...@@ -127,16 +141,6 @@ function invertField(field: Field) {
} }
} }
// TODO(zzmp): Move to @uniswap/widgets.
function toTradeType(modifiedField: Field) {
switch (modifiedField) {
case Field.INPUT:
return TradeType.EXACT_INPUT
case Field.OUTPUT:
return TradeType.EXACT_OUTPUT
}
}
// TODO(zzmp): Include in @uniswap/sdk-core (on TradeType, if possible). // TODO(zzmp): Include in @uniswap/sdk-core (on TradeType, if possible).
function invertTradeType(tradeType: TradeType) { function invertTradeType(tradeType: TradeType) {
switch (tradeType) { switch (tradeType) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment