-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathPredictWorldCupMainFeedBanner.tsx
More file actions
105 lines (91 loc) · 3.16 KB
/
PredictWorldCupMainFeedBanner.tsx
File metadata and controls
105 lines (91 loc) · 3.16 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import React, { useCallback, useMemo } from 'react';
import {
Image,
ImageSourcePropType,
Pressable,
useWindowDimensions,
} from 'react-native';
import { useNavigation } from '@react-navigation/native';
import { useSelector } from 'react-redux';
import { useTailwind } from '@metamask/design-system-twrnc-preset';
import Routes from '../../../../../constants/navigation/Routes';
import { selectPredictWorldCupConfig } from '../../selectors/featureFlags';
import type { PredictWorldCupConfig } from '../../types/flags';
import { PredictWorldCupMainFeedBannerSelectorsIDs } from './PredictWorldCupMainFeedBanner.testIds';
import worldCupMainFeedBannerImage from '../../assets/world-cup-main-feed-banner.png';
const WORLD_CUP_BANNER_ASPECT_RATIO = 360 / 177;
const WORLD_CUP_BANNER_HORIZONTAL_MARGIN = 16;
const WORLD_CUP_BANNER_HORIZONTAL_MARGIN_TOTAL =
WORLD_CUP_BANNER_HORIZONTAL_MARGIN * 2;
export const getPredictWorldCupBannerSource = (
bannerImageUrl?: string,
fallbackImageSource?: ImageSourcePropType,
): ImageSourcePropType | undefined => {
const trimmedBannerImageUrl = bannerImageUrl?.trim();
if (trimmedBannerImageUrl) {
return { uri: trimmedBannerImageUrl };
}
return fallbackImageSource;
};
interface PredictWorldCupMainFeedBannerProps {
fallbackImageSource?: ImageSourcePropType | null;
}
const shouldRenderBanner = ({
enabled,
showMainFeedBanner,
showWorldCupScreen,
}: Pick<
PredictWorldCupConfig,
'enabled' | 'showMainFeedBanner' | 'showWorldCupScreen'
>): boolean => enabled && showMainFeedBanner && showWorldCupScreen;
const PredictWorldCupMainFeedBanner: React.FC<
PredictWorldCupMainFeedBannerProps
> = ({ fallbackImageSource }) => {
const tw = useTailwind();
const { width: windowWidth } = useWindowDimensions();
const navigation = useNavigation();
const predictWorldCupConfig = useSelector(selectPredictWorldCupConfig);
const bannerWidth = Math.max(
windowWidth - WORLD_CUP_BANNER_HORIZONTAL_MARGIN_TOTAL,
0,
);
const bannerHeight = bannerWidth / WORLD_CUP_BANNER_ASPECT_RATIO;
const resolvedFallbackImageSource =
fallbackImageSource === undefined
? worldCupMainFeedBannerImage
: (fallbackImageSource ?? undefined);
const imageSource = useMemo(
() =>
shouldRenderBanner(predictWorldCupConfig)
? getPredictWorldCupBannerSource(
predictWorldCupConfig.bannerImageUrl,
resolvedFallbackImageSource,
)
: undefined,
[resolvedFallbackImageSource, predictWorldCupConfig],
);
const handlePress = useCallback(() => {
navigation.navigate(Routes.PREDICT.ROOT, {
screen: Routes.PREDICT.WORLD_CUP,
});
}, [navigation]);
if (!imageSource) {
return null;
}
return (
<Pressable
accessibilityRole="button"
onPress={handlePress}
style={tw.style('mx-4 pb-3')}
testID={PredictWorldCupMainFeedBannerSelectorsIDs.CONTAINER}
>
<Image
source={imageSource}
resizeMode="cover"
testID={PredictWorldCupMainFeedBannerSelectorsIDs.IMAGE}
style={tw.style('w-full rounded-xl', { height: bannerHeight })}
/>
</Pressable>
);
};
export default PredictWorldCupMainFeedBanner;