1- import { useEffect , useMemo , useState } from 'react' ;
1+ import { useEffect , useMemo , useRef , useState } from 'react' ;
22import BigNumber from 'bignumber.js' ;
33import { useSelector } from 'react-redux' ;
44
@@ -21,34 +21,97 @@ export const useFetchEpochProgress = () => {
2121 const { epochPercentage, epochTimeRemaining } = stats ;
2222 const { epoch, refreshRate, roundsPerEpoch, roundsPassed } = unprocessed ;
2323
24- const [ oldTestnetId , setOldTestnetId ] = useState ( activeNetworkId ) ;
25- const [ isNewState , setIsNewState ] = useState < boolean > ( true ) ;
26- const [ hasCallMade , setHasCallMade ] = useState < boolean > ( false ) ;
27- const [ epochRoundsLeft , setEpochRoundsLeft ] = useState < number > ( 0 ) ;
24+ const hasCallMadeRef = useRef < boolean > ( false ) ;
2825
29- const refreshInterval =
26+ const rawRefreshInterval =
3027 refreshRate || initialNetworkRefreshRate || REFRESH_RATE ;
31- const refreshIntervalSec = new BigNumber ( refreshInterval ) . dividedBy ( 1000 ) ;
3228
33- const stepInterval = getProgressStepInterval ( refreshInterval ) ;
34- const stepProgressSec = stepInterval . dividedBy ( 1000 ) ;
29+ const [ epochRoundsLeft , setEpochRoundsLeft ] = useState < number > ( 0 ) ;
30+ const [ effectiveRefreshInterval , setEffectiveRefreshInterval ] =
31+ useState ( rawRefreshInterval ) ;
32+
33+ const refreshIntervalSec = useMemo (
34+ ( ) => new BigNumber ( effectiveRefreshInterval ) . dividedBy ( 1000 ) ,
35+ [ effectiveRefreshInterval ]
36+ ) ;
37+
38+ const stepInterval = useMemo (
39+ ( ) => getProgressStepInterval ( effectiveRefreshInterval ) ,
40+ [ effectiveRefreshInterval ]
41+ ) ;
42+
43+ const stepProgressSec = useMemo (
44+ ( ) => stepInterval . dividedBy ( 1000 ) ,
45+ [ stepInterval ]
46+ ) ;
3547
3648 const [ roundTimeProgress , setRoundTimeProgress ] = useState (
3749 new BigNumber ( stepProgressSec )
3850 ) ;
3951
40- const updateStats = ( ) => {
41- if ( ! refreshInterval ) {
52+ const roundProgress = useMemo (
53+ ( ) => roundTimeProgress . times ( 100 ) . dividedBy ( refreshIntervalSec ) ,
54+ [ roundTimeProgress , refreshIntervalSec ]
55+ ) ;
56+
57+ const roundsLeft = useMemo ( ( ) => {
58+ if ( epochRoundsLeft ) {
59+ return epochRoundsLeft ;
60+ }
61+
62+ // add one in order to take into account the css animation and the api call sync on the first run
63+ return new BigNumber ( roundsPerEpoch ) . minus ( roundsPassed ) . plus ( 1 ) . toNumber ( ) ;
64+ } , [ epochRoundsLeft , roundsPerEpoch , roundsPassed ] ) ;
65+
66+ useEffect ( ( ) => {
67+ if ( ! rawRefreshInterval ) {
4268 return ;
4369 }
44- setIsNewState ( oldTestnetId !== activeNetworkId ) ;
45- if ( isNewState ) {
46- startRoundTime ( ) ;
70+ setEffectiveRefreshInterval ( ( prev ) =>
71+ rawRefreshInterval < prev ? rawRefreshInterval : prev
72+ ) ;
73+ } , [ rawRefreshInterval ] ) ;
74+
75+ // Reset on network change
76+ useEffect ( ( ) => {
77+ setEffectiveRefreshInterval ( rawRefreshInterval ) ;
78+ setRoundTimeProgress ( new BigNumber ( stepProgressSec ) ) ;
79+ hasCallMadeRef . current = false ;
80+ setEpochRoundsLeft ( 0 ) ;
81+ } , [ activeNetworkId ] ) ;
82+
83+ useEffect ( ( ) => {
84+ if ( ! effectiveRefreshInterval ) {
85+ return ;
86+ }
87+
88+ const intervalRoundTime = setInterval ( ( ) => {
89+ if ( ! document . hidden ) {
90+ setRoundTimeProgress ( ( prev ) =>
91+ prev . isGreaterThanOrEqualTo ( refreshIntervalSec )
92+ ? new BigNumber ( stepProgressSec )
93+ : prev . plus ( stepProgressSec )
94+ ) ;
95+ }
96+ } , stepInterval . toNumber ( ) ) ;
97+
98+ return ( ) => clearInterval ( intervalRoundTime ) ;
99+ } , [ effectiveRefreshInterval ] ) ;
100+
101+ useEffect ( ( ) => {
102+ if ( ! effectiveRefreshInterval || ! roundTimeProgress || ! timestamp ) {
103+ return ;
47104 }
48105
49- if ( roundTimeProgress . isEqualTo ( refreshIntervalSec ) && ! hasCallMade ) {
106+ if (
107+ roundTimeProgress . isGreaterThanOrEqualTo ( refreshIntervalSec ) &&
108+ ! hasCallMadeRef . current
109+ ) {
110+ hasCallMadeRef . current = true ;
111+
50112 fetchStats ( ) . then ( ( { success } ) => {
51113 if ( ! success ) {
114+ hasCallMadeRef . current = false ;
52115 return ;
53116 }
54117
@@ -59,7 +122,6 @@ export const useFetchEpochProgress = () => {
59122 return ;
60123 }
61124
62- setHasCallMade ( true ) ;
63125 setEpochRoundsLeft ( ( existingRound ) => {
64126 if ( ! existingRound ) {
65127 return roundsLeft ;
@@ -75,49 +137,10 @@ export const useFetchEpochProgress = () => {
75137 return existingRound ;
76138 } ) ;
77139 } ) ;
78- } else {
79- setHasCallMade ( false ) ;
80- }
81- } ;
82-
83- const startRoundTime = ( ) => {
84- if ( ! refreshInterval ) {
85- return ;
86- }
87- const intervalRoundTime = setInterval ( ( ) => {
88- if ( ! document . hidden ) {
89- setRoundTimeProgress ( ( roundTimeProgress ) =>
90- roundTimeProgress . isEqualTo ( refreshIntervalSec )
91- ? new BigNumber ( stepProgressSec )
92- : roundTimeProgress . plus ( stepProgressSec )
93- ) ;
94- }
95- } , stepInterval . toNumber ( ) ) ;
96- return ( ) => clearInterval ( intervalRoundTime ) ;
97- } ;
98-
99- useEffect ( ( ) => {
100- setOldTestnetId ( activeNetworkId ) ;
101- } , [ activeNetworkId ] ) ;
102-
103- useEffect ( ( ) => {
104- if ( refreshInterval && roundTimeProgress && timestamp ) {
105- updateStats ( ) ;
140+ } else if ( roundTimeProgress . isLessThan ( refreshIntervalSec ) ) {
141+ hasCallMadeRef . current = false ;
106142 }
107- } , [ timestamp , roundTimeProgress , refreshInterval ] ) ;
108-
109- const roundProgress = roundTimeProgress
110- . times ( 100 )
111- . dividedBy ( refreshIntervalSec ?? 1 ) ;
112-
113- const roundsLeft = useMemo ( ( ) => {
114- if ( epochRoundsLeft ) {
115- return epochRoundsLeft ;
116- }
117-
118- // add one in order to take into account the css animation and the api call sync on the first run
119- return new BigNumber ( roundsPerEpoch ) . minus ( roundsPassed ) . plus ( 1 ) . toNumber ( ) ;
120- } , [ epochRoundsLeft , roundsPerEpoch , roundsPassed ] ) ;
143+ } , [ timestamp , roundTimeProgress ] ) ;
121144
122145 return {
123146 isReady : isDataReady ,
0 commit comments