1- import React , { memo , ReactElement , useEffect , useMemo } from 'react' ;
1+ import React , { memo , ReactElement , useEffect } from 'react' ;
22import { Trans , useTranslation } from 'react-i18next' ;
33
44import BottomSheetScreen from '../../components/BottomSheetScreen' ;
@@ -7,97 +7,90 @@ import { __E2E__ } from '../../constants/env';
77import { useSnapPoints } from '../../hooks/bottomSheet' ;
88import { useAppDispatch , useAppSelector } from '../../hooks/redux' ;
99import { useBalance } from '../../hooks/wallet' ;
10- import { viewControllersSelector } from '../../store/reselect/ui' ;
1110import {
1211 ignoreHighBalanceCountSelector ,
1312 ignoreHighBalanceTimestampSelector ,
1413} from '../../store/reselect/user' ;
1514import { exchangeRatesSelector } from '../../store/reselect/wallet' ;
1615import { MAX_WARNINGS , ignoreHighBalance } from '../../store/slices/user' ;
17- import { closeSheet , showBottomSheet } from '../../store/utils/ui' ;
1816import { BodyMB , Display } from '../../styles/text' ;
1917import { getFiatDisplayValues } from '../../utils/displayValues' ;
2018import { openURL } from '../../utils/helpers' ;
21- import { objectKeys } from '../../utils/objectKeys ' ;
19+ import { useAllSheetRefs , useSheetRef } from './SheetRefsProvider ' ;
2220
2321const imageSrc = require ( '../../assets/illustrations/exclamation-mark.png' ) ;
2422
2523const BALANCE_THRESHOLD_USD = 500 ; // how high the balance must be to show this warning to the user (in USD)
2624const BALANCE_THRESHOLD_SATS = 700000 ; // how high the balance must be to show this warning to the user (in Sats)
2725const ASK_INTERVAL = 1000 * 60 * 60 * 24 ; // 1 day - how long this prompt will be hidden if user taps Later
28- const CHECK_DELAY = 3000 ; // how long user needs to stay on Wallets screen before he will see this prompt
26+ const CHECK_DELAY = 2500 ; // how long user needs to stay on the home screen before he will see this prompt
27+
28+ const sheetId = 'highBalance' ;
2929
3030const HighBalanceWarning = ( ) : ReactElement => {
3131 const { t } = useTranslation ( 'other' ) ;
3232 const { totalBalance } = useBalance ( ) ;
3333 const snapPoints = useSnapPoints ( 'large' ) ;
3434 const dispatch = useAppDispatch ( ) ;
35+ const sheetRefs = useAllSheetRefs ( ) ;
36+ const sheetRef = useSheetRef ( sheetId ) ;
3537 const count = useAppSelector ( ignoreHighBalanceCountSelector ) ;
3638 const exchangeRates = useAppSelector ( exchangeRatesSelector ) ;
37- const viewControllers = useAppSelector ( viewControllersSelector ) ;
3839 const ignoreTimestamp = useAppSelector ( ignoreHighBalanceTimestampSelector ) ;
3940
40- const anyBottomSheetIsOpen = useMemo ( ( ) => {
41- const viewControllerKeys = objectKeys ( viewControllers ) ;
42- return viewControllerKeys
43- . filter ( ( view ) => view !== 'highBalance' )
44- . some ( ( view ) => viewControllers [ view ] . isOpen ) ;
45- } , [ viewControllers ] ) ;
46-
4741 const { fiatValue } = getFiatDisplayValues ( {
4842 satoshis : totalBalance ,
4943 currency : 'USD' ,
5044 exchangeRates,
5145 } ) ;
5246
53- // if balance over BALANCE_THRESHOLD
54- // and not more than MAX_WARNINGS times
55- // and user has not seen this prompt for ASK_INTERVAL
56- // and no other bottom-sheets are shown
57- // and user on "Wallets" screen for CHECK_DELAY
58- const shouldShowBottomSheet = useMemo ( ( ) => {
59- const thresholdReached =
60- // fallback in case exchange rates are not available
61- fiatValue !== 0
62- ? fiatValue > BALANCE_THRESHOLD_USD
63- : totalBalance > BALANCE_THRESHOLD_SATS ;
64- const belowMaxWarnings = count < MAX_WARNINGS ;
65- const isTimeoutOver = Number ( new Date ( ) ) - ignoreTimestamp > ASK_INTERVAL ;
66- return (
67- ! __E2E__ &&
68- thresholdReached &&
69- belowMaxWarnings &&
70- isTimeoutOver &&
71- ! anyBottomSheetIsOpen
72- ) ;
73- } , [ fiatValue , totalBalance , count , ignoreTimestamp , anyBottomSheetIsOpen ] ) ;
74-
47+ // biome-ignore lint/correctness/useExhaustiveDependencies: sheetRefs don't change
7548 useEffect ( ( ) => {
76- if ( ! shouldShowBottomSheet ) {
77- return ;
78- }
49+ // if balance over BALANCE_THRESHOLD
50+ // and not more than MAX_WARNINGS times
51+ // and user has not seen this prompt for ASK_INTERVAL
52+ // and no other bottom-sheets are shown
53+ // and user on home screen for CHECK_DELAY
54+ const shouldShow = ( ) => {
55+ const isTimeoutOver = Number ( new Date ( ) ) - ignoreTimestamp > ASK_INTERVAL ;
56+ const isAnySheetOpen = sheetRefs . some ( ( { ref } ) => ref . current ?. isOpen ( ) ) ;
57+ const belowMaxWarnings = count < MAX_WARNINGS ;
58+ const thresholdReached =
59+ // fallback in case exchange rates are not available
60+ fiatValue !== 0
61+ ? fiatValue > BALANCE_THRESHOLD_USD
62+ : totalBalance > BALANCE_THRESHOLD_SATS ;
63+
64+ return (
65+ ! __E2E__ &&
66+ ! isAnySheetOpen &&
67+ isTimeoutOver &&
68+ thresholdReached &&
69+ belowMaxWarnings
70+ ) ;
71+ } ;
7972
8073 const timer = setTimeout ( ( ) => {
81- showBottomSheet ( 'highBalance' ) ;
74+ if ( shouldShow ( ) ) {
75+ sheetRef . current ?. present ( ) ;
76+ }
8277 } , CHECK_DELAY ) ;
8378
84- return ( ) : void => {
85- clearTimeout ( timer ) ;
86- } ;
87- } , [ shouldShowBottomSheet ] ) ;
79+ return ( ) => clearTimeout ( timer ) ;
80+ } , [ ignoreTimestamp , fiatValue , totalBalance , count ] ) ;
8881
8982 const onMore = ( ) : void => {
9083 openURL ( 'https://en.bitcoin.it/wiki/Storing_bitcoins' ) ;
9184 } ;
9285
9386 const onDismiss = ( ) : void => {
9487 dispatch ( ignoreHighBalance ( true ) ) ;
95- closeSheet ( 'highBalance' ) ;
88+ sheetRef . current ?. close ( ) ;
9689 } ;
9790
9891 return (
9992 < BottomSheetWrapper
100- view = "highBalance"
93+ view = { sheetId }
10194 snapPoints = { snapPoints }
10295 onClose = { ( ) : void => {
10396 dispatch ( ignoreHighBalance ( false ) ) ;
0 commit comments