Skip to content

Commit ad6113c

Browse files
committed
fix review copilot 2
1 parent 7f1744d commit ad6113c

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/lib/massa-react/hooks/useResolveDeweb.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,36 @@ export function useResolveDeweb(
2323
const [error, setError] = useState<string | null>(null);
2424

2525
useEffect(() => {
26+
let isCanceled = false;
27+
2628
const resolveUrl = async () => {
2729
try {
30+
setIsLoading(true);
2831
setError(null);
2932

3033
// Extract the path from the original URL to pass to resolveDeweb
3134
const pathToResolve = extractMNSUrl(Url);
3235

3336
const resolved = await resolveDeweb(pathToResolve, chainId);
37+
if (isCanceled) return;
3438
setResolvedUrl(resolved);
3539
} catch (err) {
40+
if (isCanceled) return;
3641
const errorMessage =
3742
err instanceof Error ? err.message : 'Failed to resolve DeWeb URL';
3843
setError(errorMessage);
3944
} finally {
40-
setIsLoading(false);
45+
if (!isCanceled) {
46+
setIsLoading(false);
47+
}
4148
}
4249
};
4350

44-
if (isLoading) return;
45-
setIsLoading(true);
4651
resolveUrl();
52+
// Cleanup cancels in-flight request
53+
return () => {
54+
isCanceled = true;
55+
};
4756
}, [Url, chainId]);
4857

4958
return {
@@ -54,9 +63,9 @@ export function useResolveDeweb(
5463
}
5564

5665
/**
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
6069
*/
6170
export function extractMNSUrl(url: string): string {
6271
const urlObj = new URL(url);

0 commit comments

Comments
 (0)