File tree Expand file tree Collapse file tree 1 file changed +15
-6
lines changed
src/lib/massa-react/hooks Expand file tree Collapse file tree 1 file changed +15
-6
lines changed Original file line number Diff line number Diff line change @@ -23,27 +23,36 @@ export function useResolveDeweb(
23
23
const [ error , setError ] = useState < string | null > ( null ) ;
24
24
25
25
useEffect ( ( ) => {
26
+ let isCanceled = false ;
27
+
26
28
const resolveUrl = async ( ) => {
27
29
try {
30
+ setIsLoading ( true ) ;
28
31
setError ( null ) ;
29
32
30
33
// Extract the path from the original URL to pass to resolveDeweb
31
34
const pathToResolve = extractMNSUrl ( Url ) ;
32
35
33
36
const resolved = await resolveDeweb ( pathToResolve , chainId ) ;
37
+ if ( isCanceled ) return ;
34
38
setResolvedUrl ( resolved ) ;
35
39
} catch ( err ) {
40
+ if ( isCanceled ) return ;
36
41
const errorMessage =
37
42
err instanceof Error ? err . message : 'Failed to resolve DeWeb URL' ;
38
43
setError ( errorMessage ) ;
39
44
} finally {
40
- setIsLoading ( false ) ;
45
+ if ( ! isCanceled ) {
46
+ setIsLoading ( false ) ;
47
+ }
41
48
}
42
49
} ;
43
50
44
- if ( isLoading ) return ;
45
- setIsLoading ( true ) ;
46
51
resolveUrl ( ) ;
52
+ // Cleanup cancels in-flight request
53
+ return ( ) => {
54
+ isCanceled = true ;
55
+ } ;
47
56
} , [ Url , chainId ] ) ;
48
57
49
58
return {
@@ -54,9 +63,9 @@ export function useResolveDeweb(
54
63
}
55
64
56
65
/**
57
- * Extracts the mns from a deweb url (expl: mns.massa.network to mns.massa)
58
- * @param url - the url to extract the mns from
59
- * @returns the mns url
66
+ * Extracts the MNS from a DeWeb URL (expl: mns.massa.network to mns.massa)
67
+ * @param url - the URL to extract the MNS from
68
+ * @returns the MNS URL
60
69
*/
61
70
export function extractMNSUrl ( url : string ) : string {
62
71
const urlObj = new URL ( url ) ;
You can’t perform that action at this time.
0 commit comments