-
-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0eaa0df
commit e50d2c0
Showing
6 changed files
with
81 additions
and
51 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
packages/suite/src/hooks/wallet/coinmarket/form/common/useCoinmarketExchangeQuotesFilter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { useEffect, useMemo } from 'react'; | ||
import type { UseFormSetValue } from 'react-hook-form'; | ||
|
||
import { ExchangeTrade } from 'invity-api'; | ||
|
||
import { | ||
FORM_EXCHANGE_CEX, | ||
FORM_EXCHANGE_DEX, | ||
FORM_EXCHANGE_TYPE, | ||
} from 'src/constants/wallet/coinmarket/form'; | ||
import type { | ||
CoinmarketExchangeFormProps, | ||
ExchangeType, | ||
RateType, | ||
} from 'src/types/coinmarket/coinmarketForm'; | ||
import { getCexQuotesByRateType } from 'src/utils/wallet/coinmarket/exchangeUtils'; | ||
|
||
interface CoinmarketExchangeQuotesFilterProps { | ||
quotes: ExchangeTrade[] | undefined; | ||
exchangeType: ExchangeType; | ||
rateType: RateType; | ||
exchangeInfo: any; | ||
setValue: UseFormSetValue<CoinmarketExchangeFormProps>; | ||
} | ||
|
||
export const useCoinmarketExchangeQuotesFilter = ({ | ||
exchangeType, | ||
rateType, | ||
quotes, | ||
exchangeInfo, | ||
setValue, | ||
}: CoinmarketExchangeQuotesFilterProps) => { | ||
const dexQuotes = useMemo(() => quotes?.filter(quote => quote.isDex), [quotes]); | ||
const cexQuotes = useMemo( | ||
() => getCexQuotesByRateType(rateType, quotes, exchangeInfo), | ||
[rateType, quotes, exchangeInfo], | ||
); | ||
|
||
// handle edge case when there are no longer quotes of selected exchange type | ||
useEffect(() => { | ||
const isSelectedDexButFoundOnlyCex = | ||
exchangeType === FORM_EXCHANGE_DEX && !dexQuotes?.length && cexQuotes?.length; | ||
const isSelectedCexButFoundOnlyDex = | ||
exchangeType === FORM_EXCHANGE_CEX && dexQuotes?.length && !cexQuotes?.length; | ||
const isSelectedDexButNotFoundAny = | ||
exchangeType === FORM_EXCHANGE_DEX && !dexQuotes?.length && !cexQuotes?.length; | ||
|
||
if (isSelectedDexButFoundOnlyCex) { | ||
setValue(FORM_EXCHANGE_TYPE, FORM_EXCHANGE_CEX); | ||
} else if (isSelectedCexButFoundOnlyDex) { | ||
setValue(FORM_EXCHANGE_TYPE, FORM_EXCHANGE_DEX); | ||
} else if (isSelectedDexButNotFoundAny) { | ||
setValue(FORM_EXCHANGE_TYPE, FORM_EXCHANGE_CEX); | ||
} | ||
}, [dexQuotes, exchangeType, cexQuotes, setValue]); | ||
|
||
return { | ||
dexQuotes, | ||
cexQuotes, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters