1
- import React , { memo , ReactElement , useEffect , useMemo } from 'react' ;
1
+ import React , { memo , ReactElement , useEffect } from 'react' ;
2
2
import { Trans , useTranslation } from 'react-i18next' ;
3
3
4
4
import BottomSheetScreen from '../../components/BottomSheetScreen' ;
@@ -7,97 +7,90 @@ import { __E2E__ } from '../../constants/env';
7
7
import { useSnapPoints } from '../../hooks/bottomSheet' ;
8
8
import { useAppDispatch , useAppSelector } from '../../hooks/redux' ;
9
9
import { useBalance } from '../../hooks/wallet' ;
10
- import { viewControllersSelector } from '../../store/reselect/ui' ;
11
10
import {
12
11
ignoreHighBalanceCountSelector ,
13
12
ignoreHighBalanceTimestampSelector ,
14
13
} from '../../store/reselect/user' ;
15
14
import { exchangeRatesSelector } from '../../store/reselect/wallet' ;
16
15
import { MAX_WARNINGS , ignoreHighBalance } from '../../store/slices/user' ;
17
- import { closeSheet , showBottomSheet } from '../../store/utils/ui' ;
18
16
import { BodyMB , Display } from '../../styles/text' ;
19
17
import { getFiatDisplayValues } from '../../utils/displayValues' ;
20
18
import { openURL } from '../../utils/helpers' ;
21
- import { objectKeys } from '../../utils/objectKeys ' ;
19
+ import { useAllSheetRefs , useSheetRef } from './SheetRefsProvider ' ;
22
20
23
21
const imageSrc = require ( '../../assets/illustrations/exclamation-mark.png' ) ;
24
22
25
23
const BALANCE_THRESHOLD_USD = 500 ; // how high the balance must be to show this warning to the user (in USD)
26
24
const BALANCE_THRESHOLD_SATS = 700000 ; // how high the balance must be to show this warning to the user (in Sats)
27
25
const 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' ;
29
29
30
30
const HighBalanceWarning = ( ) : ReactElement => {
31
31
const { t } = useTranslation ( 'other' ) ;
32
32
const { totalBalance } = useBalance ( ) ;
33
33
const snapPoints = useSnapPoints ( 'large' ) ;
34
34
const dispatch = useAppDispatch ( ) ;
35
+ const sheetRefs = useAllSheetRefs ( ) ;
36
+ const sheetRef = useSheetRef ( sheetId ) ;
35
37
const count = useAppSelector ( ignoreHighBalanceCountSelector ) ;
36
38
const exchangeRates = useAppSelector ( exchangeRatesSelector ) ;
37
- const viewControllers = useAppSelector ( viewControllersSelector ) ;
38
39
const ignoreTimestamp = useAppSelector ( ignoreHighBalanceTimestampSelector ) ;
39
40
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
-
47
41
const { fiatValue } = getFiatDisplayValues ( {
48
42
satoshis : totalBalance ,
49
43
currency : 'USD' ,
50
44
exchangeRates,
51
45
} ) ;
52
46
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
75
48
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
+ } ;
79
72
80
73
const timer = setTimeout ( ( ) => {
81
- showBottomSheet ( 'highBalance' ) ;
74
+ if ( shouldShow ( ) ) {
75
+ sheetRef . current ?. present ( ) ;
76
+ }
82
77
} , CHECK_DELAY ) ;
83
78
84
- return ( ) : void => {
85
- clearTimeout ( timer ) ;
86
- } ;
87
- } , [ shouldShowBottomSheet ] ) ;
79
+ return ( ) => clearTimeout ( timer ) ;
80
+ } , [ ignoreTimestamp , fiatValue , totalBalance , count ] ) ;
88
81
89
82
const onMore = ( ) : void => {
90
83
openURL ( 'https://en.bitcoin.it/wiki/Storing_bitcoins' ) ;
91
84
} ;
92
85
93
86
const onDismiss = ( ) : void => {
94
87
dispatch ( ignoreHighBalance ( true ) ) ;
95
- closeSheet ( 'highBalance' ) ;
88
+ sheetRef . current ?. close ( ) ;
96
89
} ;
97
90
98
91
return (
99
92
< BottomSheetWrapper
100
- view = "highBalance"
93
+ view = { sheetId }
101
94
snapPoints = { snapPoints }
102
95
onClose = { ( ) : void => {
103
96
dispatch ( ignoreHighBalance ( false ) ) ;
0 commit comments