Skip to content
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

fix(cash-in-bottom-sheet): only show when home is in focus #3927

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions e2e/src/FiatConnectTransferOut.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ describe('FiatConnect Transfer Out', () => {
})

const platform = device.getPlatform()
// disable test for android until we fix the bottom sheet issue
if (platform === 'ios') {
describe('Non KYC', fiatConnectNonKycTransferOut)
}
describe('Non KYC', fiatConnectNonKycTransferOut)

// KYC test needs to be on iOS and needs Mock Provider info
if (platform == 'ios' && MOCK_PROVIDER_BASE_URL && MOCK_PROVIDER_API_KEY) {
Expand Down
67 changes: 38 additions & 29 deletions src/home/WalletHome.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useIsFocused } from '@react-navigation/native'
import _ from 'lodash'
import React, { useEffect, useRef } from 'react'
import React, { useEffect, useMemo, useRef } from 'react'
import { useTranslation } from 'react-i18next'
import { RefreshControl, RefreshControlProps, SectionList, StyleSheet } from 'react-native'
import Animated from 'react-native-reanimated'
Expand All @@ -8,6 +9,7 @@
import { showMessage } from 'src/alert/actions'
import { AppState } from 'src/app/actions'
import { appStateSelector, phoneNumberVerifiedSelector } from 'src/app/selectors'
import QrScanButton from 'src/components/QrScanButton'
import { HomeTokenBalance } from 'src/components/TokenBalance'
import {
ALERT_BANNER_DURATION,
Expand Down Expand Up @@ -37,12 +39,12 @@
import TransactionFeed from 'src/transactions/feed/TransactionFeed'
import { userInSanctionedCountrySelector } from 'src/utils/countryFeatures'
import { checkContactsPermission } from 'src/utils/permissions'
import QrScanButton from 'src/components/QrScanButton'

const AnimatedSectionList = Animated.createAnimatedComponent(SectionList)

function WalletHome() {
const { t } = useTranslation()
const isFocused = useIsFocused()

const appState = useSelector(appStateSelector)
const isLoading = useSelector((state) => state.home.loading)
Expand Down Expand Up @@ -114,32 +116,6 @@
dispatch(refreshAllBalances())
}

const shouldShowCashInBottomSheet = () => {
// If user is in a sanctioned country do not show the cash in bottom sheet
if (userInSanctionedCountry) {
return false
}
// If there are no core tokens then we are either still loading or loading failed.
if (!coreTokenBalances.length) {
return false
}
const hasStable = !!coreTokenBalances.find(
(token) => token.balance.gte(STABLE_TRANSACTION_MIN_AMOUNT) && token.address !== celoAddress
)

const hasCelo =
coreTokenBalances
.find((token) => token.address === celoAddress)
?.balance.isGreaterThan(CELO_TRANSACTION_MIN_AMOUNT) ?? false
const isAccountBalanceZero = hasStable === false && hasCelo === false

const { cashInBottomSheetEnabled } = getExperimentParams(
ExperimentConfigs[StatsigExperiments.CHOOSE_YOUR_ADVENTURE]
)

return cashInBottomSheetEnabled && isAccountBalanceZero
}

const keyExtractor = (_item: any, index: number) => {
return index.toString()
}
Expand Down Expand Up @@ -179,6 +155,39 @@
renderItem: () => <TransactionFeed key={'TransactionList'} />,
})

// Cash In Bottom Sheet
const { cashInBottomSheetEnabled } = getExperimentParams(
ExperimentConfigs[StatsigExperiments.CHOOSE_YOUR_ADVENTURE]
)
const hasStable = !!coreTokenBalances.find(
(token) => token.balance.gte(STABLE_TRANSACTION_MIN_AMOUNT) && token.address !== celoAddress
)
const hasCelo =
coreTokenBalances
.find((token) => token.address === celoAddress)
?.balance.isGreaterThan(CELO_TRANSACTION_MIN_AMOUNT) ?? false
const isAccountBalanceZero = hasStable === false && hasCelo === false

const WrappedCashInBottomSheet = useMemo(() => {
if (!isFocused) {
return null

Check warning on line 173 in src/home/WalletHome.tsx

View check run for this annotation

Codecov / codecov/patch

src/home/WalletHome.tsx#L173

Added line #L173 was not covered by tests
Comment on lines +171 to +173
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a bit too hacky, since isFocused is not in the list of dependencies for useMemo.

Could we instead keep the old implementation and track (via some state) whether we've already displayed the bottom sheet to avoid triggering it every time the screen is put in focus?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also want to ensure that the bottom sheet will only appear a single time per app session; that is, I think the semantics should be:

  • Show the bottom sheet once per app session, on the first occurrence of the following conditions:
    • Bottom sheet is enabled
    • User has zero balance
    • Home screen is focused

Namely, we want to avoid showing the bottom sheet multiple times in the situation where a user goes from zero balance -> nonzero balance -> zero balance, all within the same app session.

}

if (!coreTokenBalances.length) {
return null
}

if (userInSanctionedCountry) {
return null

Check warning on line 181 in src/home/WalletHome.tsx

View check run for this annotation

Codecov / codecov/patch

src/home/WalletHome.tsx#L181

Added line #L181 was not covered by tests
}

if (isAccountBalanceZero) {
return <CashInBottomSheet />
} else {
return null
}
}, [userInSanctionedCountry, cashInBottomSheetEnabled, isAccountBalanceZero, coreTokenBalances])

return (
<SafeAreaView
testID="WalletHome"
Expand Down Expand Up @@ -207,7 +216,7 @@
testID="WalletHome/SectionList"
/>
{showHomeNavBar && <SendOrRequestBar />}
{shouldShowCashInBottomSheet() && <CashInBottomSheet />}
{WrappedCashInBottomSheet}
{ConfirmOpenDappBottomSheet}
</SafeAreaView>
)
Expand Down
Loading