1- import React , { FunctionComponent , useEffect } from "react" ;
1+ import React , { FunctionComponent , useEffect , useState , useRef } from "react" ;
22import { observer } from "mobx-react-lite" ;
33import { useIntl , IntlShape } from "react-intl" ;
44import { Box } from "../../../../components/box" ;
@@ -39,10 +39,50 @@ export const RewardsCard: FunctionComponent<{
3939 isLedger,
4040 isKeystone,
4141 claimAllIsCompleted,
42- count ,
43- claimCountText ,
42+ succeededCount ,
43+ totalClaimTokenCount ,
4444 } = useRewards ( ) ;
4545
46+ const [ showCompletionUI , setShowCompletionUI ] = useState ( false ) ;
47+ const [ count , setCount ] = useState ( 0 ) ;
48+ const prevClaimAllIsLoadingRef = useRef ( claimAllIsLoading ) ;
49+
50+ useEffect ( ( ) => {
51+ const wasLoading = prevClaimAllIsLoadingRef . current ;
52+ prevClaimAllIsLoadingRef . current = claimAllIsLoading ;
53+
54+ if ( wasLoading && claimAllIsCompleted && ! claimAllIsLoading ) {
55+ setShowCompletionUI ( true ) ;
56+ setCount ( 6 ) ;
57+
58+ const interval = setInterval ( ( ) => {
59+ setCount ( ( prev ) => {
60+ if ( prev <= 1 ) {
61+ clearInterval ( interval ) ;
62+ setShowCompletionUI ( false ) ;
63+ return 0 ;
64+ }
65+ return prev - 1 ;
66+ } ) ;
67+ } , 1000 ) ;
68+
69+ return ( ) => {
70+ clearInterval ( interval ) ;
71+ } ;
72+ }
73+ } , [ claimAllIsCompleted , claimAllIsLoading ] ) ;
74+
75+ const claimCountText =
76+ totalClaimTokenCount === 0
77+ ? ""
78+ : `${ succeededCount } /${ totalClaimTokenCount } ` ;
79+
80+ const handleClaimAll = ( ) => {
81+ setShowCompletionUI ( false ) ;
82+ setCount ( 0 ) ;
83+ claimAll ( ) ;
84+ } ;
85+
4686 const navigateToStake = React . useCallback ( ( ) => {
4787 mainHeaderAnimationStore . triggerShowForMainHeaderPrice ( ) ;
4888 navigate ( "/stake?intitialExpand=true" ) ;
@@ -119,7 +159,7 @@ export const RewardsCard: FunctionComponent<{
119159 id : "page.main.components.rewards-card.view-button" ,
120160 } ) }
121161 </ Body3 >
122- ) : claimAllIsLoading || claimAllIsCompleted ? (
162+ ) : claimAllIsLoading || showCompletionUI ? (
123163 < YAxis alignX = "right" >
124164 < XAxis alignY = "center" >
125165 < Body3
@@ -179,13 +219,12 @@ export const RewardsCard: FunctionComponent<{
179219 < ClaimAllButton
180220 intl = { intl }
181221 claimAllDisabled = { claimAllDisabled }
182- claimAllIsLoading = { claimAllIsLoading }
183222 claimAllIsCompleted = { claimAllIsCompleted }
184223 claimCountText = { claimCountText }
185224 count = { count }
186225 isLedger = { ! ! isLedger }
187226 isKeystone = { ! ! isKeystone }
188- onClaimAll = { claimAll }
227+ onClaimAll = { handleClaimAll }
189228 onNavigateToStake = { navigateToStake }
190229 isHover = { isClaimAllHover }
191230 setIsHover = { setIsClaimAllHover }
@@ -202,7 +241,6 @@ export const RewardsCard: FunctionComponent<{
202241type ClaimAllButtonProps = {
203242 intl : IntlShape ;
204243 claimAllDisabled : boolean ;
205- claimAllIsLoading : boolean ;
206244 claimAllIsCompleted : boolean ;
207245 claimCountText : string ;
208246 count : number ;
@@ -217,9 +255,7 @@ type ClaimAllButtonProps = {
217255const ClaimAllButton : FunctionComponent < ClaimAllButtonProps > = ( {
218256 intl,
219257 claimAllDisabled,
220- claimAllIsLoading,
221258 claimAllIsCompleted,
222- claimCountText,
223259 count,
224260 isLedger,
225261 isKeystone,
@@ -241,8 +277,7 @@ const ClaimAllButton: FunctionComponent<ClaimAllButtonProps> = ({
241277 id : "button.approve" ,
242278 } ) ;
243279
244- const shouldDimClaimAllButton =
245- ! claimAllDisabled && ! claimAllIsLoading && isPressed ;
280+ const shouldDimClaimAllButton = ! claimAllDisabled && isPressed ;
246281
247282 const handleClick = ( e : React . MouseEvent < HTMLDivElement > ) => {
248283 e . stopPropagation ( ) ;
@@ -251,12 +286,7 @@ const ClaimAllButton: FunctionComponent<ClaimAllButtonProps> = ({
251286 return ;
252287 }
253288
254- if (
255- isLedger ||
256- isKeystone ||
257- claimAllIsLoading ||
258- ( claimAllIsCompleted && count >= 1 )
259- ) {
289+ if ( isLedger || isKeystone || ( claimAllIsCompleted && count >= 1 ) ) {
260290 onNavigateToStake ( ) ;
261291 return ;
262292 }
@@ -265,15 +295,11 @@ const ClaimAllButton: FunctionComponent<ClaimAllButtonProps> = ({
265295 } ;
266296
267297 const updateLabelWidth = React . useCallback ( ( ) => {
268- if ( claimAllIsLoading ) {
269- setLabelWidth ( undefined ) ;
270- return ;
271- }
272298 const target = isHover ? approveTextRef . current : claimTextRef . current ;
273299 if ( target ) {
274300 setLabelWidth ( target . offsetWidth ) ;
275301 }
276- } , [ isHover , claimAllIsLoading ] ) ;
302+ } , [ isHover ] ) ;
277303
278304 React . useLayoutEffect ( ( ) => {
279305 updateLabelWidth ( ) ;
@@ -290,7 +316,7 @@ const ClaimAllButton: FunctionComponent<ClaimAllButtonProps> = ({
290316 return (
291317 < Box
292318 onHoverStateChange = { ( hovered ) => {
293- if ( claimAllDisabled || claimAllIsLoading ) {
319+ if ( claimAllDisabled ) {
294320 return ;
295321 }
296322 if ( ! hovered ) {
@@ -301,7 +327,7 @@ const ClaimAllButton: FunctionComponent<ClaimAllButtonProps> = ({
301327 cursor = { claimAllDisabled ? "not-allowed" : "pointer" }
302328 opacity = { shouldDimClaimAllButton ? COMMON_HOVER_OPACITY : undefined }
303329 onMouseDown = { ( ) => {
304- if ( claimAllDisabled || claimAllIsLoading ) {
330+ if ( claimAllDisabled ) {
305331 return ;
306332 }
307333 setIsPressed ( true ) ;
@@ -334,18 +360,14 @@ const ClaimAllButton: FunctionComponent<ClaimAllButtonProps> = ({
334360 : ColorPalette [ "blue-300" ]
335361 }
336362 >
337- { claimAllIsLoading ? (
338- claimCountText
339- ) : (
340- < ClaimTextWrapper $width = { labelWidth } >
341- < ClaimAllText ref = { claimTextRef } $visible = { ! isHover } >
342- { claimLabel }
343- </ ClaimAllText >
344- < ApproveText ref = { approveTextRef } $visible = { isHover } >
345- { approveLabel }
346- </ ApproveText >
347- </ ClaimTextWrapper >
348- ) }
363+ < ClaimTextWrapper $width = { labelWidth } >
364+ < ClaimAllText ref = { claimTextRef } $visible = { ! isHover } >
365+ { claimLabel }
366+ </ ClaimAllText >
367+ < ApproveText ref = { approveTextRef } $visible = { isHover } >
368+ { approveLabel }
369+ </ ApproveText >
370+ </ ClaimTextWrapper >
349371 </ Subtitle3 >
350372 </ XAxis >
351373 </ Box >
0 commit comments