-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathuseFeaturedCarouselData.ts
More file actions
57 lines (51 loc) · 1.74 KB
/
useFeaturedCarouselData.ts
File metadata and controls
57 lines (51 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 { useEffect, useMemo } from 'react';
import { useQuery } from '@tanstack/react-query';
import { useSelector } from 'react-redux';
import Logger from '../../../../util/Logger';
import { PREDICT_CONSTANTS } from '../constants/errors';
import { predictQueries } from '../queries';
import { selectPredictUpDownEnabledFlag } from '../selectors/featureFlags';
import type { PredictMarket } from '../types';
import { isCryptoUpDown } from '../utils/cryptoUpDown';
import { filterStandaloneMarkets } from '../utils/feed';
import { ensureError } from '../utils/predictErrorHandler';
export interface UseFeaturedCarouselDataResult {
markets: PredictMarket[];
isLoading: boolean;
error: string | null;
refetch: () => Promise<unknown>;
}
export const useFeaturedCarouselData = (): UseFeaturedCarouselDataResult => {
const query = useQuery(predictQueries.featuredCarousel.options());
const upDownEnabled = useSelector(selectPredictUpDownEnabledFlag);
useEffect(() => {
if (!query.error) return;
Logger.error(ensureError(query.error), {
tags: {
feature: PREDICT_CONSTANTS.FEATURE_NAME,
component: 'useFeaturedCarouselData',
},
context: {
name: 'useFeaturedCarouselData',
data: {
method: 'queryFn',
action: 'featured_carousel_load',
operation: 'data_fetching',
},
},
});
}, [query.error]);
const markets = useMemo(() => {
const data = filterStandaloneMarkets(query.data ?? []);
if (upDownEnabled) {
return data;
}
return data.filter((market) => !isCryptoUpDown(market));
}, [query.data, upDownEnabled]);
return {
markets,
isLoading: query.isLoading,
error: query.error?.message ?? null,
refetch: query.refetch,
};
};