1- import { useCallback , useEffect , useState } from 'react' ;
1+ import { useEffect , useState } from 'react' ;
22import { getIconUrl } from '../iconMapping' ;
33
44interface UseCryptoIconProps {
@@ -19,30 +19,42 @@ export const useCryptoIcon = ({ ledgerId, network }: UseCryptoIconProps): UseCry
1919 const [ loading , setLoading ] = useState < boolean > ( true ) ;
2020 const [ error , setError ] = useState < Error | null > ( null ) ;
2121
22- const loadIcon = useCallback ( async ( ) => {
23- setLoading ( true ) ;
24- setError ( null ) ;
25- setNetworkUrl ( null ) ;
26-
27- const iconsToResolve = [ getIconUrl ( ledgerId ) ] ;
28- if ( network ) iconsToResolve . push ( getIconUrl ( network ) ) ;
29-
30- try {
31- const [ url , networkUrlResolved ] = await Promise . all ( iconsToResolve ) ;
32- setIconUrl ( url ) ;
33- if ( network && networkUrlResolved ) setNetworkUrl ( networkUrlResolved ) ;
34- } catch ( e ) {
35- const errorInstance = e instanceof Error ? e : new Error ( 'Failed to load icon' ) ;
36- setError ( errorInstance ) ;
37- console . error ( 'Failed to load crypto icon:' , errorInstance ) ;
38- } finally {
39- setLoading ( false ) ;
40- }
41- } , [ ledgerId , network ] ) ;
42-
4322 useEffect ( ( ) => {
23+ let cancelled = false ;
24+
25+ const loadIcon = async ( ) => {
26+ setLoading ( true ) ;
27+ setError ( null ) ;
28+ setNetworkUrl ( null ) ;
29+
30+ const iconsToResolve = [ getIconUrl ( ledgerId ) ] ;
31+ if ( network ) iconsToResolve . push ( getIconUrl ( network ) ) ;
32+
33+ try {
34+ const [ url , networkUrlResolved ] = await Promise . all ( iconsToResolve ) ;
35+ if ( cancelled ) return ;
36+
37+ setIconUrl ( url ) ;
38+ if ( network && networkUrlResolved ) setNetworkUrl ( networkUrlResolved ) ;
39+ } catch ( e ) {
40+ if ( cancelled ) return ;
41+
42+ const errorInstance = e instanceof Error ? e : new Error ( 'Failed to load icon' ) ;
43+ setError ( errorInstance ) ;
44+ console . error ( 'Failed to load crypto icon:' , errorInstance ) ;
45+ } finally {
46+ if ( ! cancelled ) {
47+ setLoading ( false ) ;
48+ }
49+ }
50+ } ;
51+
4452 loadIcon ( ) ;
45- } , [ loadIcon ] ) ;
53+
54+ return ( ) => {
55+ cancelled = true ;
56+ } ;
57+ } , [ ledgerId , network ] ) ;
4658
4759 return {
4860 iconUrl,
0 commit comments