|
1 | 1 | import { useState, useEffect } from 'react';
|
2 |
| -import { CHAIN_ID, resolveDeweb } from '@massalabs/massa-web3'; |
| 2 | +import { resolveDeweb } from '@massalabs/massa-web3'; |
3 | 3 |
|
4 | 4 | interface UseResolveDewebResult {
|
5 | 5 | resolvedUrl: string;
|
6 | 6 | isLoading: boolean;
|
7 | 7 | error: string | null;
|
8 | 8 | }
|
9 | 9 |
|
10 |
| -interface UseResolveDewebOptions { |
11 |
| - fallbackUrl?: string; |
12 |
| - shouldResolve?: boolean; |
13 |
| - chainId?: bigint; |
14 |
| -} |
15 |
| - |
16 | 10 | /**
|
17 | 11 | * Custom hook to resolve DeWeb URLs using the massa-web3 resolveDeweb function
|
18 |
| - * @param originalUrl - The original URL to resolve (should contain massa.network domains) |
19 |
| - * @param options - Optional configuration |
| 12 | + * @param Url - The original URL to resolve (should contain massa.network domains) |
| 13 | + * @param chainId - The chain ID to resolve the URL on |
20 | 14 | * @returns Object containing the resolved URL, loading state, and error state
|
21 | 15 | */
|
22 | 16 | export function useResolveDeweb(
|
23 |
| - originalUrl: string, |
24 |
| - options: UseResolveDewebOptions = {}, |
| 17 | + Url: string, |
| 18 | + chainId: bigint, |
25 | 19 | ): UseResolveDewebResult {
|
26 |
| - const { |
27 |
| - fallbackUrl = originalUrl, |
28 |
| - shouldResolve = true, |
29 |
| - chainId = CHAIN_ID.Mainnet, |
30 |
| - } = options; |
31 |
| - |
32 |
| - const [resolvedUrl, setResolvedUrl] = useState<string>(fallbackUrl); |
33 |
| - const [isLoading, setIsLoading] = useState<boolean>(shouldResolve); |
| 20 | + const [resolvedUrl, setResolvedUrl] = useState<string>(Url); |
| 21 | + const [isLoading, setIsLoading] = useState<boolean>(false); |
34 | 22 | const [error, setError] = useState<string | null>(null);
|
35 | 23 |
|
36 | 24 | useEffect(() => {
|
37 |
| - // Only resolve if shouldResolve is true and URL contains massa.network |
38 |
| - if (!shouldResolve) { |
39 |
| - setResolvedUrl(originalUrl); |
40 |
| - setIsLoading(false); |
41 |
| - return; |
42 |
| - } |
43 |
| - |
44 | 25 | const resolveUrl = async () => {
|
45 | 26 | try {
|
46 | 27 | setIsLoading(true);
|
47 | 28 | setError(null);
|
48 | 29 |
|
49 | 30 | // Extract the path from the original URL to pass to resolveDeweb
|
50 |
| - const pathToResolve = extractMNSUrl(originalUrl); |
| 31 | + const pathToResolve = extractMNSUrl(Url); |
51 | 32 |
|
52 | 33 | const resolved = await resolveDeweb(pathToResolve, chainId);
|
53 | 34 | setResolvedUrl(resolved);
|
54 | 35 | } catch (err) {
|
55 | 36 | const errorMessage =
|
56 | 37 | err instanceof Error ? err.message : 'Failed to resolve DeWeb URL';
|
57 | 38 | setError(errorMessage);
|
58 |
| - setResolvedUrl(fallbackUrl); |
59 | 39 | } finally {
|
60 | 40 | setIsLoading(false);
|
61 | 41 | }
|
62 | 42 | };
|
63 | 43 |
|
64 | 44 | resolveUrl();
|
65 |
| - }, [originalUrl, fallbackUrl, shouldResolve, chainId]); |
| 45 | + }, [Url, chainId]); |
66 | 46 |
|
67 | 47 | return {
|
68 | 48 | resolvedUrl,
|
|
0 commit comments