-
-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathFeedbackWidgetManager.tsx
More file actions
238 lines (213 loc) · 7.87 KB
/
FeedbackWidgetManager.tsx
File metadata and controls
238 lines (213 loc) · 7.87 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
import { logger } from '@sentry/core';
import * as React from 'react';
import type { NativeScrollEvent, NativeSyntheticEvent} from 'react-native';
import { Animated, Dimensions, Easing, Modal, PanResponder, Platform, ScrollView, View } from 'react-native';
import { notWeb } from '../utils/environment';
import { FeedbackWidget } from './FeedbackWidget';
import { modalSheetContainer, modalWrapper, topSpacer } from './FeedbackWidget.styles';
import type { FeedbackWidgetStyles } from './FeedbackWidget.types';
import { getFeedbackOptions } from './integration';
import { lazyLoadAutoInjectFeedbackIntegration } from './lazy';
import { isModalSupported, isNativeDriverSupportedForColorAnimations } from './utils';
const PULL_DOWN_CLOSE_THRESHOLD = 200;
const SLIDE_ANIMATION_DURATION = 200;
const BACKGROUND_ANIMATION_DURATION = 200;
const useNativeDriverForColorAnimations = isNativeDriverSupportedForColorAnimations();
class FeedbackWidgetManager {
private static _isVisible = false;
private static _setVisibility: (visible: boolean) => void;
public static initialize(setVisibility: (visible: boolean) => void): void {
this._setVisibility = setVisibility;
}
/**
* For testing purposes only.
*/
public static reset(): void {
this._isVisible = false;
this._setVisibility = undefined;
}
public static show(): void {
if (this._setVisibility) {
this._isVisible = true;
this._setVisibility(true);
} else {
// This message should be always shown otherwise it's not possible to use the widget.
// eslint-disable-next-line no-console
console.warn('[Sentry] FeedbackWidget requires `Sentry.wrap(RootComponent)` to be called before `showFeedbackWidget()`.');
}
}
public static hide(): void {
if (this._setVisibility) {
this._isVisible = false;
this._setVisibility(false);
} else {
// This message should be always shown otherwise it's not possible to use the widget.
// eslint-disable-next-line no-console
console.warn('[Sentry] FeedbackWidget requires `Sentry.wrap(RootComponent)` before interacting with the widget.');
}
}
public static isFormVisible(): boolean {
return this._isVisible;
}
}
interface FeedbackWidgetProviderProps {
children: React.ReactNode;
styles?: FeedbackWidgetStyles;
}
interface FeedbackWidgetProviderState {
isVisible: boolean;
backgroundOpacity: Animated.Value;
panY: Animated.Value;
isScrollAtTop: boolean;
}
class FeedbackWidgetProvider extends React.Component<FeedbackWidgetProviderProps> {
public state: FeedbackWidgetProviderState = {
isVisible: false,
backgroundOpacity: new Animated.Value(0),
panY: new Animated.Value(Dimensions.get('screen').height),
isScrollAtTop: true,
};
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);
FeedbackWidgetManager.initialize(this._setVisibilityFunction);
}
/**
* 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(() => {
logger.info('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()) {
logger.error('FeedbackWidget Modal is not supported in React Native < 0.71 with Fabric renderer.');
return <>{this.props.children}</>;
}
const { 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}
{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, { 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 _handleClose = (): void => {
FeedbackWidgetManager.hide();
};
}
const showFeedbackWidget = (): void => {
lazyLoadAutoInjectFeedbackIntegration();
FeedbackWidgetManager.show();
};
const resetFeedbackWidgetManager = (): void => {
FeedbackWidgetManager.reset();
};
export { showFeedbackWidget, FeedbackWidgetProvider, resetFeedbackWidgetManager };