-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
FeatureTrainingModal.tsx
242 lines (215 loc) · 9.48 KB
/
FeatureTrainingModal.tsx
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import type {VideoReadyForDisplayEvent} from 'expo-av';
import React, {useCallback, useEffect, useState} from 'react';
import {View} from 'react-native';
import {GestureHandlerRootView} from 'react-native-gesture-handler';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useOnboardingLayout from '@hooks/useOnboardingLayout';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import variables from '@styles/variables';
import * as User from '@userActions/User';
import CONST from '@src/CONST';
import Button from './Button';
import CheckboxWithLabel from './CheckboxWithLabel';
import Lottie from './Lottie';
import LottieAnimations from './LottieAnimations';
import type DotLottieAnimation from './LottieAnimations/types';
import Modal from './Modal';
import SafeAreaConsumer from './SafeAreaConsumer';
import Text from './Text';
import VideoPlayer from './VideoPlayer';
// Aspect ratio and height of the video.
// Useful before video loads to reserve space.
const VIDEO_ASPECT_RATIO = 1280 / 960;
const MODAL_PADDING = variables.spacing2;
type VideoLoadedEventType = {
srcElement: {
videoWidth: number;
videoHeight: number;
};
};
type VideoStatus = 'video' | 'animation';
type FeatureTrainingModalProps = {
/** Animation to show when video is unavailable. Useful when app is offline */
animation?: DotLottieAnimation;
/** URL for the video */
videoURL: string;
videoAspectRatio?: number;
/** Title for the modal */
title?: string;
/** Describe what is showing */
description?: string;
/** Secondary description rendered with additional space */
secondaryDescription?: string;
/** Whether to show `Don't show me this again` option */
shouldShowDismissModalOption?: boolean;
/** Text to show on primary button */
confirmText: string;
/** A callback to call when user confirms the tutorial */
onConfirm?: () => void;
/** Text to show on secondary button */
helpText?: string;
/** Link to navigate to when user wants to learn more */
onHelp?: () => void;
};
function FeatureTrainingModal({
animation,
videoURL,
videoAspectRatio: videoAspectRatioProp,
title = '',
description = '',
secondaryDescription = '',
shouldShowDismissModalOption = false,
confirmText = '',
onConfirm = () => {},
helpText = '',
onHelp = () => {},
}: FeatureTrainingModalProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const {isMediumOrLargerScreenWidth} = useOnboardingLayout();
const [isModalVisible, setIsModalVisible] = useState(true);
const [willShowAgain, setWillShowAgain] = useState(true);
const [videoStatus, setVideoStatus] = useState<VideoStatus>('video');
const [isVideoStatusLocked, setIsVideoStatusLocked] = useState(false);
const [videoAspectRatio, setVideoAspectRatio] = useState(videoAspectRatioProp ?? VIDEO_ASPECT_RATIO);
const {shouldUseNarrowLayout} = useResponsiveLayout();
const {isOffline} = useNetwork();
useEffect(() => {
if (isVideoStatusLocked) {
return;
}
if (isOffline) {
setVideoStatus('animation');
} else if (!isOffline) {
setVideoStatus('video');
setIsVideoStatusLocked(true);
}
}, [isOffline, isVideoStatusLocked]);
const setAspectRatio = (event: VideoReadyForDisplayEvent | VideoLoadedEventType | undefined) => {
if (!event) {
return;
}
if ('naturalSize' in event) {
setVideoAspectRatio(event.naturalSize.width / event.naturalSize.height);
} else {
setVideoAspectRatio(event.srcElement.videoWidth / event.srcElement.videoHeight);
}
};
const renderIllustration = useCallback(() => {
const aspectRatio = videoAspectRatio || VIDEO_ASPECT_RATIO;
return (
<View
style={[
styles.w100,
// Prevent layout jumps by reserving height
// for the video until it loads. Also, when
// videoStatus === 'animation' it will
// set the same aspect ratio as the video would.
{aspectRatio},
]}
>
{videoStatus === 'video' ? (
<VideoPlayer
url={videoURL}
videoPlayerStyle={[styles.onboardingVideoPlayer, {aspectRatio}]}
onVideoLoaded={setAspectRatio}
controlsStatus={CONST.VIDEO_PLAYER.CONTROLS_STATUS.SHOW}
shouldUseControlsBottomMargin={false}
shouldPlay
isLooping
/>
) : (
<View style={[styles.flex1, styles.alignItemsCenter, {aspectRatio}]}>
<Lottie
source={animation ?? LottieAnimations.Hands}
style={styles.h100}
webStyle={shouldUseNarrowLayout ? styles.h100 : undefined}
autoPlay
loop
/>
</View>
)}
</View>
);
}, [animation, videoURL, videoAspectRatio, videoStatus, shouldUseNarrowLayout, styles]);
const toggleWillShowAgain = useCallback(() => setWillShowAgain((prevWillShowAgain) => !prevWillShowAgain), []);
const closeModal = useCallback(() => {
if (!willShowAgain) {
User.dismissTrackTrainingModal();
}
setIsModalVisible(false);
Navigation.goBack();
}, [willShowAgain]);
const closeAndConfirmModal = useCallback(() => {
closeModal();
onConfirm?.();
}, [onConfirm, closeModal]);
return (
<SafeAreaConsumer>
{({safeAreaPaddingBottomStyle}) => (
<Modal
isVisible={isModalVisible}
type={isMediumOrLargerScreenWidth ? CONST.MODAL.MODAL_TYPE.CENTERED_UNSWIPEABLE : CONST.MODAL.MODAL_TYPE.BOTTOM_DOCKED}
onClose={closeModal}
innerContainerStyle={{
boxShadow: 'none',
borderRadius: 16,
paddingBottom: 20,
paddingTop: isMediumOrLargerScreenWidth ? undefined : MODAL_PADDING,
...(isMediumOrLargerScreenWidth
? // Override styles defined by MODAL.MODAL_TYPE.CENTERED_UNSWIPEABLE
// To make it take as little space as possible.
{
flex: undefined,
width: 'auto',
}
: {}),
}}
>
<GestureHandlerRootView>
<View style={[styles.mh100, isMediumOrLargerScreenWidth && styles.welcomeVideoNarrowLayout, safeAreaPaddingBottomStyle]}>
<View style={isMediumOrLargerScreenWidth ? {padding: MODAL_PADDING} : {paddingHorizontal: MODAL_PADDING}}>{renderIllustration()}</View>
<View style={[styles.mt5, styles.mh5]}>
{title && description && (
<View style={[isMediumOrLargerScreenWidth ? [styles.gap1, styles.mb8] : [styles.mb10]]}>
<Text style={[styles.textHeadlineH1]}>{title}</Text>
<Text style={styles.textSupporting}>{description}</Text>
{secondaryDescription.length > 0 && <Text style={[styles.textSupporting, styles.mt4]}>{secondaryDescription}</Text>}
</View>
)}
{shouldShowDismissModalOption && (
<CheckboxWithLabel
label={translate('featureTraining.doNotShowAgain')}
accessibilityLabel={translate('featureTraining.doNotShowAgain')}
style={[styles.mb5]}
isChecked={!willShowAgain}
onInputChange={toggleWillShowAgain}
/>
)}
{helpText && (
<Button
large
style={[styles.mb3]}
onPress={onHelp}
text={helpText}
/>
)}
<Button
large
success
pressOnEnter
onPress={closeAndConfirmModal}
text={confirmText}
/>
</View>
</View>
</GestureHandlerRootView>
</Modal>
)}
</SafeAreaConsumer>
);
}
export default FeatureTrainingModal;