-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathuseTokensFeed.ts
More file actions
57 lines (49 loc) · 1.74 KB
/
useTokensFeed.ts
File metadata and controls
57 lines (49 loc) · 1.74 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
import { useMemo } from 'react';
import type { TrendingAsset } from '@metamask/assets-controllers';
import { useTrendingSearch } from '../../../../UI/Trending/hooks/useTrendingSearch/useTrendingSearch';
import { useFeedRefresh } from '../../hooks/useFeedRefresh';
import type { RefreshConfig } from '../../hooks/useExploreRefresh';
import { fuseSearch, TOKEN_FUSE_OPTIONS } from '../search-utils';
interface UseTokensFeedOptions {
/** Search query; when present, results are sorted by market cap descending. */
query?: string;
refresh?: RefreshConfig;
/**
* When true, only Verified and Benign tokens (or unscanned ones) are shown.
* Use for surfaces that don't display a security badge.
*/
hideRiskyTokens?: boolean;
}
export interface UseTokensFeedResult {
data: TrendingAsset[];
isLoading: boolean;
refetch: () => Promise<void>;
}
/** Trending tokens feed; same source for the home list, "crypto movers" pills, and search. */
export const useTokensFeed = ({
query,
refresh,
hideRiskyTokens = false,
}: UseTokensFeedOptions = {}): UseTokensFeedResult => {
const { data, isLoading, refetch } = useTrendingSearch({
searchQuery: query,
enableDebounce: false,
});
useFeedRefresh(refresh, refetch);
const filteredData = useMemo(() => {
const searched = fuseSearch(
data,
query,
TOKEN_FUSE_OPTIONS,
(a, b) => (b.marketCap ?? 0) - (a.marketCap ?? 0),
);
if (!hideRiskyTokens) return searched;
return searched.filter(({ securityData }) => {
const { resultType } = securityData ?? {};
return (
!resultType || resultType === 'Verified' || resultType === 'Benign'
);
});
}, [data, query, hideRiskyTokens]);
return { data: filteredData, isLoading, refetch };
};