Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jorbuedo committed Dec 3, 2024
1 parent 6468fd4 commit 154d7ab
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 156 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
import {FlashList} from '@shopify/flash-list'
import {sortTokenInfos} from '@yoroi/portfolio'
import {useSwap, useSwapTokensOnlyVerified} from '@yoroi/swap'
import {useTheme} from '@yoroi/theme'
import {Portfolio} from '@yoroi/types'
import React from 'react'
import {ErrorBoundary} from 'react-error-boundary'
import {StyleSheet, TouchableOpacity, View} from 'react-native'
import {SafeAreaView} from 'react-native-safe-area-context'

import {Boundary} from '../../../../../../../components/Boundary/Boundary'
import {Icon} from '../../../../../../../components/Icon'
import {Spacer} from '../../../../../../../components/Spacer/Spacer'
import {Text} from '../../../../../../../components/Text'
import {useMetrics} from '../../../../../../../kernel/metrics/metricsManager'
import {YoroiWallet} from '../../../../../../../yoroi-wallets/cardano/types'
import {usePortfolioBalances} from '../../../../../../Portfolio/common/hooks/usePortfolioBalances'
import {
AmountItemPlaceholder,
TokenAmountItem,
} from '../../../../../../Portfolio/common/TokenAmountItem/TokenAmountItem'
import {useSearch, useSearchOnNavBar} from '../../../../../../Search/SearchContext'
import {NoAssetFoundImage} from '../../../../../../Send/common/NoAssetFoundImage'
import {useSelectedWallet} from '../../../../../../WalletManager/common/hooks/useSelectedWallet'
import {Counter} from '../../../../../common/Counter/Counter'
import {filterBySearch} from '../../../../../common/filterBySearch'
import {useNavigateTo} from '../../../../../common/navigation'
import {ServiceUnavailable} from '../../../../../common/ServiceUnavailable/ServiceUnavailable'
import {useStrings} from '../../../../../common/strings'
import {useSwapForm} from '../../../../../common/SwapFormProvider'
import {Boundary} from '../../../../components/Boundary/Boundary'
import {Icon} from '../../../../components/Icon'
import {Spacer} from '../../../../components/Spacer/Spacer'
import {Text} from '../../../../components/Text'
import {useMetrics} from '../../../../kernel/metrics/metricsManager'
import {YoroiWallet} from '../../../../yoroi-wallets/cardano/types'
import {usePortfolioBalances} from '../../../Portfolio/common/hooks/usePortfolioBalances'
import {AmountItemPlaceholder, TokenAmountItem} from '../../../Portfolio/common/TokenAmountItem/TokenAmountItem'
import {useSearch, useSearchOnNavBar} from '../../../Search/SearchContext'
import {NoAssetFoundImage} from '../../../Send/common/NoAssetFoundImage'
import {useSelectedWallet} from '../../../WalletManager/common/hooks/useSelectedWallet'
import {Counter} from '../../common/Counter/Counter'
import {filterBySearch} from '../../common/filterBySearch'
import {useNavigateTo} from '../../common/navigation'
import {ServiceUnavailable} from '../../common/ServiceUnavailable/ServiceUnavailable'
import {useStrings} from '../../common/strings'
import {useSwap} from '../../common/SwapProvider'

export const SelectBuyTokenFromListScreen = () => {
const strings = useStrings()
Expand Down Expand Up @@ -73,15 +69,15 @@ const TokenList = () => {
const strings = useStrings()
const {styles, colors} = useStyles()
const {wallet} = useSelectedWallet()
const tokenInfos = useSwapTokensOnlyVerified()
const {tokenInfos} = useSwap()
const {search: assetSearchTerm} = useSearch()
const balances = usePortfolioBalances({wallet})

const walletTokenIds = React.useMemo(() => balances.all.map(({info: {id}}) => id), [balances.all])

const [filteredTokenList, someInWallet] = React.useMemo(() => {
const list = sortTokenInfos({
secondaryTokenInfos: tokenInfos.filter(filterBySearch(assetSearchTerm)),
secondaryTokenInfos: Array.from(tokenInfos.values()).filter(filterBySearch(assetSearchTerm)),
primaryTokenInfo: wallet.portfolioPrimaryTokenInfo,
})
const set = new Set(list.map(({id}) => id))
Expand Down Expand Up @@ -153,19 +149,14 @@ const SelectableToken = ({wallet, tokenInfo, walletTokenIds}: SelectableTokenPro
// NOTE: no need to subscribe to the balance
const balanceAvailable = wallet.balances.records.get(id)?.quantity ?? 0n
const {closeSearch} = useSearch()
const {buyTokenInfoChanged, orderData, resetQuantities} = useSwap()
const {
sellQuantity: {isTouched: isSellTouched},
buyQuantity: {isTouched: isBuyTouched},
buyTouched,
switchTokens,
} = useSwapForm()
const swapForm = useSwap()

const navigateTo = useNavigateTo()
const {track} = useMetrics()

const inUserWallet = walletTokenIds.includes(tokenInfo.id)
const shouldUpdateToken = id !== orderData.amounts.buy?.info.id || !isBuyTouched
const shouldSwitchTokens = id === orderData.amounts.sell?.info.id && isSellTouched
const shouldUpdateToken = id !== swapForm.tokenOutInput.tokenId || !swapForm.tokenOutInput.isTouched
const shouldSwitchTokens = id === swapForm.tokenInInput.tokenId && swapForm.tokenInInput.isTouched

const handleOnTokenSelection = () => {
const [policyId] = id.split('.')
Expand All @@ -175,13 +166,13 @@ const SelectableToken = ({wallet, tokenInfo, walletTokenIds}: SelectableTokenPro

// useCase - switch tokens when selecting the same already selected token on the other side
if (shouldSwitchTokens) {
resetQuantities()
switchTokens()
swapForm.dispatch({type: 'ResetAmounts'})
swapForm.dispatch({type: 'SwitchTouched'})
}

if (shouldUpdateToken) {
buyTokenInfoChanged(tokenInfo)
buyTouched()
swapForm.dispatch({type: 'TokenInIdChanged', value: id})
swapForm.dispatch({type: 'TokenInInputTouched'})
}
navigateTo.startSwap()
closeSearch()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import {FlashList} from '@shopify/flash-list'
import {infoFilterByName} from '@yoroi/portfolio'
import {useSwap} from '@yoroi/swap'
import {useTheme} from '@yoroi/theme'
import {Portfolio} from '@yoroi/types'
import * as React from 'react'
import {StyleSheet, TouchableOpacity, View} from 'react-native'
import {SafeAreaView} from 'react-native-safe-area-context'

import {Spacer} from '../../../../../../../components/Spacer/Spacer'
import {Text} from '../../../../../../../components/Text'
import {useMetrics} from '../../../../../../../kernel/metrics/metricsManager'
import {getTokenIdParts} from '../../../../../../Portfolio/common/helpers/get-token-id-parts'
import {usePortfolioBalances} from '../../../../../../Portfolio/common/hooks/usePortfolioBalances'
import {TokenAmountItem} from '../../../../../../Portfolio/common/TokenAmountItem/TokenAmountItem'
import {useSearch, useSearchOnNavBar} from '../../../../../../Search/SearchContext'
import {NoAssetFoundImage} from '../../../../../../Send/common/NoAssetFoundImage'
import {useSelectedWallet} from '../../../../../../WalletManager/common/hooks/useSelectedWallet'
import {Counter} from '../../../../../common/Counter/Counter'
import {useNavigateTo} from '../../../../../common/navigation'
import {useStrings} from '../../../../../common/strings'
import {useSwapForm} from '../../../../../common/SwapFormProvider'
import {Spacer} from '../../../../components/Spacer/Spacer'
import {Text} from '../../../../components/Text'
import {useMetrics} from '../../../../kernel/metrics/metricsManager'
import {getTokenIdParts} from '../../../Portfolio/common/helpers/get-token-id-parts'
import {usePortfolioBalances} from '../../../Portfolio/common/hooks/usePortfolioBalances'
import {TokenAmountItem} from '../../../Portfolio/common/TokenAmountItem/TokenAmountItem'
import {useSearch, useSearchOnNavBar} from '../../../Search/SearchContext'
import {NoAssetFoundImage} from '../../../Send/common/NoAssetFoundImage'
import {useSelectedWallet} from '../../../WalletManager/common/hooks/useSelectedWallet'
import {Counter} from '../../common/Counter/Counter'
import {useNavigateTo} from '../../common/navigation'
import {useStrings} from '../../common/strings'
import {useSwap} from '../../common/SwapProvider'

export const SelectSellTokenFromListScreen = () => {
const strings = useStrings()
Expand Down Expand Up @@ -100,19 +99,14 @@ const TokenList = () => {
const SelectableToken = ({amount}: {amount: Portfolio.Token.Amount}) => {
const styles = useStyles()
const {closeSearch} = useSearch()
const {sellTokenInfoChanged, orderData, resetQuantities} = useSwap()
const {
buyQuantity: {isTouched: isBuyTouched},
sellQuantity: {isTouched: isSellTouched},
sellTouched,
switchTokens,
} = useSwapForm()
const swapForm = useSwap()

const navigateTo = useNavigateTo()
const {track} = useMetrics()
const {policyId} = getTokenIdParts(amount.info.id)

const shouldUpdateToken = amount.info.id !== orderData.amounts.sell?.info.id || !isSellTouched
const shouldSwitchTokens = amount.info.id === orderData.amounts.buy?.info.id && isBuyTouched
const shouldUpdateToken = amount.info.id !== swapForm.tokenInInput.tokenId || !swapForm.tokenInInput.isTouched
const shouldSwitchTokens = amount.info.id === swapForm.tokenOutInput.tokenId && swapForm.tokenOutInput.isTouched

const handleOnTokenSelection = () => {
track.swapAssetFromChanged({
Expand All @@ -121,13 +115,13 @@ const SelectableToken = ({amount}: {amount: Portfolio.Token.Amount}) => {

// useCase - switch tokens when selecting the same already selected token on the other side
if (shouldSwitchTokens) {
resetQuantities()
switchTokens()
swapForm.dispatch({type: 'ResetAmounts'})
swapForm.dispatch({type: 'SwitchTouched'})
}

if (shouldUpdateToken) {
sellTouched()
sellTokenInfoChanged(amount.info)
swapForm.dispatch({type: 'TokenInInputTouched'})
swapForm.dispatch({type: 'TokenInIdChanged', value: amount.info.id})
}

navigateTo.startSwap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {StartMultiTokenTxScreen} from '../Send/useCases/StartMultiTokenTx/StartM
import {NetworkTag} from '../Settings/useCases/changeAppSettings/ChangeNetwork/NetworkTag'
import {SwapTabNavigator} from '../Swap/SwapNavigator'
import {EditSlippageScreen} from '../Swap/useCases/CreateOrder/EditSlippageScreen'
import {SelectBuyTokenFromListScreen} from '../Swap/useCases/CreateOrder/SelectBuyTokenFromListScreen'
import {SelectSellTokenFromListScreen} from '../Swap/useCases/CreateOrder/SelectSellTokenFromListScreen'
// import {EditSlippageScreen, SelectPoolFromListScreen} from '../Swap/useCases'
import {ReviewSwap} from '../Swap/useCases/ReviewSwap/ReviewSwap'
import {ShowPreprodNoticeScreen} from '../Swap/useCases/ShowPreprodNoticeScreen/ShowPreprodNoticeScreen'
Expand Down Expand Up @@ -233,7 +235,7 @@ export const TxHistoryNavigator = () => {
}}
/>

{/* <Stack.Screen
<Stack.Screen
name="swap-select-sell-token"
component={SelectSellTokenFromListScreen}
options={{
Expand All @@ -251,6 +253,7 @@ export const TxHistoryNavigator = () => {
}}
/>

{/*
<Stack.Screen
name="swap-select-pool"
component={SelectPoolFromListScreen}
Expand Down
Loading

0 comments on commit 154d7ab

Please sign in to comment.