1- import { useEffect , useMemo , useRef , useState } from "react" ;
1+ import { useMemo } from "react" ;
22import { ViewToken } from "../pages/main" ;
33import { useStore } from "../stores" ;
44import {
@@ -34,8 +34,6 @@ const USDN_CURRENCY = {
3434
3535const zeroDec = new Dec ( 0 ) ;
3636
37- const homeRewardsSuppressedByKeyId = new Set < string > ( ) ;
38-
3937export function useRewards ( ) {
4038 const {
4139 chainStore,
@@ -53,11 +51,6 @@ export function useRewards() {
5351 useStarknetClaimRewards ( ) ;
5452
5553 const { states, getClaimAllEachState } = useClaimAllEachState ( ) ;
56- const prevSelectedKeyIdRef = useRef ( keyRingStore . selectedKeyInfo ?. id ) ;
57- const keyId = keyRingStore . selectedKeyInfo ?. id ?? "unknown" ;
58-
59- const [ claimAllIsCompleted , setClaimAllIsCompleted ] = useState ( false ) ;
60- const [ claimAllIsLoading , setClaimAllIsLoading ] = useState ( false ) ;
6154
6255 const viewClaimTokens : ViewClaimToken [ ] = ( ( ) => {
6356 const res : ViewClaimToken [ ] = [ ] ;
@@ -240,10 +233,6 @@ export function useRewards() {
240233
241234 const claimAll = ( ) => {
242235 analyticsStore . logEvent ( "click_claimAll" ) ;
243- homeRewardsSuppressedByKeyId . delete ( keyId ) ;
244-
245- setClaimAllIsCompleted ( false ) ;
246- setClaimAllIsLoading ( true ) ;
247236
248237 for ( const viewClaimToken of viewClaimTokens ) {
249238 const state = getClaimAllEachState (
@@ -300,7 +289,7 @@ export function useRewards() {
300289 return count ;
301290 } , [ viewClaimTokens , getClaimAllEachState ] ) ;
302291
303- const hasAnyInProgress = useMemo ( ( ) => {
292+ const claimAllIsLoading = useMemo ( ( ) => {
304293 for ( const viewClaimToken of viewClaimTokens ) {
305294 const state = getClaimAllEachState (
306295 viewClaimToken . modularChainInfo . chainId
@@ -312,100 +301,9 @@ export function useRewards() {
312301 return false ;
313302 } , [ viewClaimTokens , getClaimAllEachState ] ) ;
314303
315- useEffect ( ( ) => {
316- setClaimAllIsLoading ( hasAnyInProgress ) ;
317- } , [ hasAnyInProgress ] ) ;
318-
319- useEffect ( ( ) => {
320- if ( homeRewardsSuppressedByKeyId . has ( keyId ) ) {
321- setClaimAllIsCompleted ( false ) ;
322- return ;
323- }
324- setClaimAllIsCompleted (
325- totalClaimTokenCount > 0 && finishedCount === totalClaimTokenCount
326- ) ;
327- } , [ finishedCount , totalClaimTokenCount , keyId ] ) ;
328-
329- const [ count , setCount ] = useState ( 0 ) ;
330-
331- const claimCountText = useMemo ( ( ) => {
332- if ( totalClaimTokenCount === 0 ) {
333- return "" ;
334- }
335-
336- return `${ succeededCount } /${ totalClaimTokenCount } ` ;
337- } , [ succeededCount , totalClaimTokenCount ] ) ;
338-
339- useEffect ( ( ) => {
340- if ( keyRingStore . selectedKeyInfo ?. id !== prevSelectedKeyIdRef . current ) {
341- return ;
342- }
343-
344- if ( ! claimAllIsCompleted ) {
345- return ;
346- }
347-
348- setCount ( 6 ) ;
349-
350- const interval = setInterval ( ( ) => {
351- setCount ( ( prev ) => {
352- if ( prev <= 1 ) {
353- clearInterval ( interval ) ;
354- setClaimAllIsCompleted ( false ) ;
355- return 0 ;
356- }
357- return prev - 1 ;
358- } ) ;
359- } , 1000 ) ;
360-
361- return ( ) => {
362- setCount ( 0 ) ;
363- clearInterval ( interval ) ;
364- setClaimAllIsCompleted ( false ) ;
365- } ;
366- // eslint-disable-next-line react-hooks/exhaustive-deps
367- } , [ claimAllIsCompleted ] ) ;
368-
369- useEffect ( ( ) => {
370- if ( keyRingStore . selectedKeyInfo ?. id !== prevSelectedKeyIdRef . current ) {
371- setClaimAllIsLoading ( false ) ;
372- setClaimAllIsCompleted ( false ) ;
373- setCount ( 0 ) ;
374- for ( const s of states ) {
375- s . reset ( ) ;
376- }
377- prevSelectedKeyIdRef . current = keyRingStore . selectedKeyInfo ?. id ;
378- homeRewardsSuppressedByKeyId . delete ( keyId ) ;
379- }
380- // eslint-disable-next-line react-hooks/exhaustive-deps
381- } , [ keyRingStore . selectedKeyInfo ?. id ] ) ;
382-
383- const claimAllIsCompletedRef = useRef ( claimAllIsCompleted ) ;
384- const countRef = useRef ( count ) ;
385- const keyIdRef = useRef ( keyId ) ;
386-
387- // complete 되고 다른 페이지로 이동할 경우에만 UI상태를 초기화 하기 위해서 참조를 유지한다.
388- useEffect ( ( ) => {
389- claimAllIsCompletedRef . current = claimAllIsCompleted ;
390- } , [ claimAllIsCompleted ] ) ;
391-
392- useEffect ( ( ) => {
393- countRef . current = count ;
394- } , [ count ] ) ;
395-
396- useEffect ( ( ) => {
397- keyIdRef . current = keyId ;
398- } , [ keyId ] ) ;
399-
400- useEffect ( ( ) => {
401- return ( ) => {
402- if ( claimAllIsCompletedRef . current && countRef . current > 0 ) {
403- homeRewardsSuppressedByKeyId . add ( keyIdRef . current ) ;
404- setCount ( 0 ) ;
405- setClaimAllIsCompleted ( false ) ;
406- }
407- } ;
408- } , [ ] ) ;
304+ const claimAllIsCompleted = useMemo ( ( ) => {
305+ return totalClaimTokenCount > 0 && finishedCount === totalClaimTokenCount ;
306+ } , [ finishedCount , totalClaimTokenCount ] ) ;
409307
410308 return {
411309 viewClaimTokens,
@@ -418,7 +316,7 @@ export function useRewards() {
418316 claimAllIsCompleted,
419317 getClaimAllEachState,
420318 states,
421- count ,
422- claimCountText ,
319+ succeededCount ,
320+ totalClaimTokenCount ,
423321 } ;
424322}
0 commit comments