-
-
Notifications
You must be signed in to change notification settings - Fork 361
Expand file tree
/
Copy pathFeedbackWidgetProvider.tsx
More file actions
251 lines (229 loc) · 8.56 KB
/
Copy pathFeedbackWidgetProvider.tsx
File metadata and controls
251 lines (229 loc) · 8.56 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
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
243
244
245
246
247
248
249
250
251
import { debug } from '@sentry/core';
import * as React from 'react';
import { Animated, Appearance, Dimensions, Easing, Modal, type NativeEventSubscription, type NativeScrollEvent,type NativeSyntheticEvent, PanResponder, Platform, ScrollView, View } from 'react-native';
import { notWeb } from '../utils/environment';
import { FeedbackButton } from './FeedbackButton';
import { FeedbackWidget } from './FeedbackWidget';
import { modalSheetContainer,modalWrapper, topSpacer } from './FeedbackWidget.styles';
import { getTheme } from './FeedbackWidget.theme';
import type { FeedbackWidgetStyles } from './FeedbackWidget.types';
import {
BACKGROUND_ANIMATION_DURATION,
FeedbackButtonManager,
FeedbackWidgetManager,
PULL_DOWN_CLOSE_THRESHOLD,
ScreenshotButtonManager,
showFeedbackWidget,
SLIDE_ANIMATION_DURATION,
} from './FeedbackWidgetManager';
import { getFeedbackButtonOptions, getFeedbackOptions, getScreenshotButtonOptions, isShakeToReportEnabled } from './integration';
import { ScreenshotButton } from './ScreenshotButton';
import { startShakeListener, stopShakeListener } from './ShakeToReportBug';
import { isModalSupported, isNativeDriverSupportedForColorAnimations } from './utils';
const useNativeDriverForColorAnimations = isNativeDriverSupportedForColorAnimations();
export interface FeedbackWidgetProviderProps {
children: React.ReactNode;
styles?: FeedbackWidgetStyles;
}
export interface FeedbackWidgetProviderState {
isButtonVisible: boolean;
isScreenshotButtonVisible: boolean;
isVisible: boolean;
backgroundOpacity: Animated.Value;
panY: Animated.Value;
isScrollAtTop: boolean;
}
/**
* FeedbackWidgetProvider is a component that wraps the feedback widget and provides
* functionality to show and hide the widget. It also manages the visibility of the
* feedback button and screenshot button.
*/
export class FeedbackWidgetProvider extends React.Component<FeedbackWidgetProviderProps> {
public state: FeedbackWidgetProviderState = {
isButtonVisible: false,
isScreenshotButtonVisible: false,
isVisible: false,
backgroundOpacity: new Animated.Value(0),
panY: new Animated.Value(Dimensions.get('screen').height),
isScrollAtTop: true,
};
private _themeListener: NativeEventSubscription | undefined;
private _startedShakeListener: boolean = false;
private _panResponder = PanResponder.create({
onStartShouldSetPanResponder: (_, gestureState) => {
return notWeb() && this.state.isScrollAtTop && gestureState.dy > 0;
},
onMoveShouldSetPanResponder: (_, gestureState) => {
return notWeb() && this.state.isScrollAtTop && gestureState.dy > 0;
},
onPanResponderMove: (_, gestureState) => {
if (gestureState.dy > 0) {
this.state.panY.setValue(gestureState.dy);
}
},
onPanResponderRelease: (_, gestureState) => {
if (gestureState.dy > PULL_DOWN_CLOSE_THRESHOLD) {
// Close on swipe below a certain threshold
Animated.timing(this.state.panY, {
toValue: Dimensions.get('screen').height,
duration: SLIDE_ANIMATION_DURATION,
useNativeDriver: true,
}).start(() => {
this._handleClose();
});
} else {
// Animate it back to the original position
Animated.spring(this.state.panY, {
toValue: 0,
useNativeDriver: true,
}).start();
}
},
});
public constructor(props: FeedbackWidgetProviderProps) {
super(props);
FeedbackButtonManager.initialize(this._setButtonVisibilityFunction);
ScreenshotButtonManager.initialize(this._setScreenshotButtonVisibilityFunction);
FeedbackWidgetManager.initialize(this._setVisibilityFunction);
}
/**
* Add a listener to the theme change event and start shake detection if configured.
*/
public componentDidMount(): void {
this._themeListener = Appearance.addChangeListener(() => {
this.forceUpdate();
});
if (isShakeToReportEnabled()) {
this._startedShakeListener = startShakeListener(showFeedbackWidget);
}
}
/**
* Clean up the theme listener and stop shake detection.
*/
public componentWillUnmount(): void {
if (this._themeListener) {
this._themeListener.remove();
}
if (this._startedShakeListener) {
stopShakeListener();
}
}
/**
* Animates the background opacity when the modal is shown.
*/
public componentDidUpdate(_prevProps: any, prevState: FeedbackWidgetProviderState): void {
if (!prevState.isVisible && this.state.isVisible) {
Animated.parallel([
Animated.timing(this.state.backgroundOpacity, {
toValue: 1,
duration: BACKGROUND_ANIMATION_DURATION,
useNativeDriver: useNativeDriverForColorAnimations,
easing: Easing.in(Easing.quad),
}),
Animated.timing(this.state.panY, {
toValue: 0,
duration: SLIDE_ANIMATION_DURATION,
useNativeDriver: true,
easing: Easing.in(Easing.quad),
}),
]).start(() => {
debug.log('FeedbackWidgetProvider componentDidUpdate');
});
} else if (prevState.isVisible && !this.state.isVisible) {
this.state.backgroundOpacity.setValue(0);
}
}
/**
* Renders the feedback form modal.
*/
public render(): React.ReactNode {
if (!isModalSupported()) {
debug.error('FeedbackWidget Modal is not supported in React Native < 0.71 with Fabric renderer.');
return <>{this.props.children}</>;
}
const theme = getTheme();
const { isButtonVisible, isScreenshotButtonVisible, isVisible, backgroundOpacity } = this.state;
const backgroundColor = backgroundOpacity.interpolate({
inputRange: [0, 1],
outputRange: ['rgba(0, 0, 0, 0)', 'rgba(0, 0, 0, 0.9)'],
});
// Wrapping the `Modal` component in a `View` component is necessary to avoid
// issues like https://github.com/software-mansion/react-native-reanimated/issues/6035
return (
<>
{this.props.children}
{isButtonVisible && <FeedbackButton {...getFeedbackButtonOptions()} />}
{isScreenshotButtonVisible && <ScreenshotButton {...getScreenshotButtonOptions()} />}
{isVisible && (
<Animated.View style={[modalWrapper, { backgroundColor }]}>
<Modal
visible={isVisible}
transparent
animationType="none"
onRequestClose={this._handleClose}
testID="feedback-form-modal"
>
<View style={topSpacer} />
<Animated.View
style={[modalSheetContainer(theme), { transform: [{ translateY: this.state.panY }] }]}
{...this._panResponder.panHandlers}
>
<ScrollView
bounces={false}
keyboardShouldPersistTaps="handled"
automaticallyAdjustKeyboardInsets={Platform.OS === 'ios'}
onScroll={this._handleScroll}
>
<FeedbackWidget
{...getFeedbackOptions()}
onFormClose={this._handleClose}
onFormSubmitted={this._handleClose}
/>
</ScrollView>
</Animated.View>
</Modal>
</Animated.View>
)}
</>
);
}
private _handleScroll = (event: NativeSyntheticEvent<NativeScrollEvent>): void => {
this.setState({ isScrollAtTop: event.nativeEvent.contentOffset.y <= 0 });
};
private _setVisibilityFunction = (visible: boolean): void => {
const updateState = (): void => {
this.setState({ isVisible: visible });
};
if (!visible) {
Animated.parallel([
Animated.timing(this.state.panY, {
toValue: Dimensions.get('screen').height,
duration: SLIDE_ANIMATION_DURATION,
useNativeDriver: true,
easing: Easing.out(Easing.quad),
}),
Animated.timing(this.state.backgroundOpacity, {
toValue: 0,
duration: BACKGROUND_ANIMATION_DURATION,
useNativeDriver: useNativeDriverForColorAnimations,
easing: Easing.out(Easing.quad),
}),
]).start(() => {
// Change of the state unmount the component
// which would cancel the animation
updateState();
});
} else {
updateState();
}
};
private _setButtonVisibilityFunction = (visible: boolean): void => {
this.setState({ isButtonVisible: visible });
};
private _setScreenshotButtonVisibilityFunction = (visible: boolean): void => {
this.setState({ isScreenshotButtonVisible: visible });
};
private _handleClose = (): void => {
FeedbackWidgetManager.hide();
};
}