11import { useCallback , useEffect , useMemo } from 'react' ;
22
3- import { useFocusEffect , useNavigation } from '@react-navigation/core' ;
3+ import { useNavigation } from '@react-navigation/core' ;
44import { omit } from 'lodash' ;
55import { useDebounce } from 'use-debounce' ;
66
@@ -27,8 +27,11 @@ import platformEnv from '@onekeyhq/shared/src/platformEnv';
2727
2828import backgroundApiProxy from '../../../background/instance/backgroundApiProxy' ;
2929import { useAccountTokens , useActiveSideAccount } from '../../../hooks' ;
30+ import { useStatus } from '../../../hooks/redux' ;
3031import { useAccountTokenLoading } from '../../../hooks/useTokens' ;
32+ import { useVisibilityFocused } from '../../../hooks/useVisibilityFocused' ;
3133import { OverviewDefiThumbnal } from '../../Overview/Thumbnail' ;
34+ import { WalletHomeTabEnum } from '../type' ;
3235
3336import AssetsListHeader from './AssetsListHeader' ;
3437import { EmptyListOfAccount } from './EmptyList' ;
@@ -127,15 +130,6 @@ function AssetsList({
127130 } ,
128131 ) ;
129132
130- const visibilityStateListener = useCallback ( ( ) => {
131- if ( document . visibilityState === 'hidden' ) {
132- stopRefresh ( ) ;
133- }
134- if ( document . visibilityState === 'visible' ) {
135- startRefresh ( ) ;
136- }
137- } , [ startRefresh , stopRefresh ] ) ;
138-
139133 useEffect ( ( ) => {
140134 const { serviceOverview } = backgroundApiProxy ;
141135 serviceOverview . subscribe ( ) ;
@@ -145,42 +139,20 @@ function AssetsList({
145139 }
146140 } , [ networkId , accountId , startRefresh , stopRefresh ] ) ;
147141
148- useFocusEffect (
149- useCallback ( ( ) => {
150- const { serviceToken } = backgroundApiProxy ;
142+ const { homeTabName } = useStatus ( ) ;
143+
144+ const isFocused = useVisibilityFocused ( ) ;
145+
146+ useEffect ( ( ) => {
147+ if ( isFocused && homeTabName === WalletHomeTabEnum . Tokens ) {
151148 if ( ! account || ! network ) {
152149 return ;
153150 }
154- serviceToken . fetchAccountTokens ( {
155- includeTop50TokensQuery : true ,
156- networkId : network ?. id ,
157- accountId : account ?. id ,
158- } ) ;
159-
160151 startRefresh ( ) ;
161- if ( platformEnv . isRuntimeBrowser ) {
162- document . addEventListener ( 'visibilitychange' , visibilityStateListener ) ;
163- if ( ! platformEnv . isDesktop ) {
164- window . addEventListener ( 'blur' , stopRefresh ) ;
165- window . addEventListener ( 'focus' , startRefresh ) ;
166- }
167- }
168-
169- return ( ) => {
170- stopRefresh ( ) ;
171- if ( platformEnv . isRuntimeBrowser ) {
172- document . removeEventListener (
173- 'visibilitychange' ,
174- visibilityStateListener ,
175- ) ;
176- if ( ! platformEnv . isDesktop ) {
177- window . removeEventListener ( 'blur' , stopRefresh ) ;
178- window . removeEventListener ( 'focus' , startRefresh ) ;
179- }
180- }
181- } ;
182- } , [ account , network , visibilityStateListener , startRefresh , stopRefresh ] ) ,
183- ) ;
152+ } else {
153+ stopRefresh ( ) ;
154+ }
155+ } , [ isFocused , startRefresh , stopRefresh , account , network , homeTabName ] ) ;
184156
185157 const onTokenCellPress = useCallback (
186158 ( item : Token ) => {
0 commit comments