-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathuseTokenSecurityData.ts
More file actions
85 lines (73 loc) · 2.31 KB
/
useTokenSecurityData.ts
File metadata and controls
85 lines (73 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { useEffect, useState, useRef, useCallback } from 'react';
import type { CaipAssetType } from '@metamask/utils';
import {
fetchTokenAssets,
TokenSecurityData,
} from '@metamask/assets-controllers';
interface UseTokenSecurityDataOpts {
/** CAIP-19 asset ID. When null, no fetch is attempted. */
assetId: CaipAssetType | null;
/** Pre-fetched security data from trending/search — returned immediately if provided. */
prefetchedData?: TokenSecurityData;
}
interface UseTokenSecurityDataResult {
securityData: TokenSecurityData | null;
isLoading: boolean;
error: Error | null;
}
const isValidTokenSecurityData = (data: unknown): data is TokenSecurityData =>
data != null &&
typeof data === 'object' &&
typeof (data as TokenSecurityData).resultType === 'string' &&
Array.isArray((data as TokenSecurityData).features);
export const useTokenSecurityData = ({
assetId,
prefetchedData: rawPrefetchedData,
}: UseTokenSecurityDataOpts): UseTokenSecurityDataResult => {
const prefetchedData = isValidTokenSecurityData(rawPrefetchedData)
? rawPrefetchedData
: undefined;
const [securityData, setSecurityData] = useState<TokenSecurityData | null>(
prefetchedData ?? null,
);
const [isLoading, setIsLoading] = useState(!prefetchedData && !!assetId);
const [error, setError] = useState<Error | null>(null);
const isMountedRef = useRef(true);
const fetchData = useCallback(async () => {
if (!assetId) return;
try {
const assets = await fetchTokenAssets([assetId], {
includeTokenSecurityData: true,
});
if (!isMountedRef.current) return;
const asset = assets?.[0];
setSecurityData(asset?.securityData ?? null);
setError(null);
} catch (err) {
if (!isMountedRef.current) return;
setError(err as Error);
} finally {
if (isMountedRef.current) {
setIsLoading(false);
}
}
}, [assetId]);
useEffect(() => {
isMountedRef.current = true;
if (prefetchedData) {
setSecurityData(prefetchedData);
setIsLoading(false);
return;
}
if (!assetId) {
setIsLoading(false);
return;
}
setIsLoading(true);
fetchData();
return () => {
isMountedRef.current = false;
};
}, [assetId, prefetchedData, fetchData]);
return { securityData, isLoading, error };
};