|
| 1 | +import { logger } from '@sentry/core'; |
| 2 | +import * as React from 'react'; |
| 3 | +import { Modal, View } from 'react-native'; |
| 4 | + |
| 5 | +import { FeedbackForm } from './FeedbackForm'; |
| 6 | +import defaultStyles from './FeedbackForm.styles'; |
| 7 | +import type { FeedbackFormStyles } from './FeedbackForm.types'; |
| 8 | +import { isModalSupported } from './utils'; |
| 9 | + |
| 10 | +class FeedbackFormManager { |
| 11 | + private static _isVisible = false; |
| 12 | + private static _setVisibility: (visible: boolean) => void; |
| 13 | + |
| 14 | + public static initialize(setVisibility: (visible: boolean) => void): void { |
| 15 | + this._setVisibility = setVisibility; |
| 16 | + } |
| 17 | + |
| 18 | + public static show(): void { |
| 19 | + if (this._setVisibility) { |
| 20 | + this._isVisible = true; |
| 21 | + this._setVisibility(true); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + public static hide(): void { |
| 26 | + if (this._setVisibility) { |
| 27 | + this._isVisible = false; |
| 28 | + this._setVisibility(false); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public static isFormVisible(): boolean { |
| 33 | + return this._isVisible; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +interface FeedbackFormProviderProps { |
| 38 | + children: React.ReactNode; |
| 39 | + styles?: FeedbackFormStyles; |
| 40 | +} |
| 41 | + |
| 42 | +class FeedbackFormProvider extends React.Component<FeedbackFormProviderProps> { |
| 43 | + public state = { |
| 44 | + isVisible: false, |
| 45 | + }; |
| 46 | + |
| 47 | + public constructor(props: FeedbackFormProviderProps) { |
| 48 | + super(props); |
| 49 | + FeedbackFormManager.initialize(this._setVisibilityFunction); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Renders the feedback form modal. |
| 54 | + */ |
| 55 | + public render(): React.ReactNode { |
| 56 | + if (!isModalSupported()) { |
| 57 | + logger.error('FeedbackForm Modal is not supported in React Native < 0.71 with Fabric renderer.'); |
| 58 | + return <>{this.props.children}</>; |
| 59 | + } |
| 60 | + |
| 61 | + const { isVisible } = this.state; |
| 62 | + const styles: FeedbackFormStyles = { ...defaultStyles, ...this.props.styles }; |
| 63 | + |
| 64 | + // Wrapping the `Modal` component in a `View` component is necessary to avoid |
| 65 | + // issues like https://github.com/software-mansion/react-native-reanimated/issues/6035 |
| 66 | + return ( |
| 67 | + <> |
| 68 | + {this.props.children} |
| 69 | + {isVisible && ( |
| 70 | + <View> |
| 71 | + <Modal visible={isVisible} transparent animationType="slide" onRequestClose={this._handleClose} testID="feedback-form-modal"> |
| 72 | + <View style={styles.modalBackground}> |
| 73 | + <FeedbackForm |
| 74 | + onFormClose={this._handleClose} |
| 75 | + onFormSubmitted={this._handleClose} |
| 76 | + /> |
| 77 | + </View> |
| 78 | + </Modal> |
| 79 | + </View> |
| 80 | + )} |
| 81 | + </> |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + private _setVisibilityFunction = (visible: boolean): void => { |
| 86 | + this.setState({ isVisible: visible }); |
| 87 | + }; |
| 88 | + |
| 89 | + private _handleClose = (): void => { |
| 90 | + FeedbackFormManager.hide(); |
| 91 | + this.setState({ isVisible: false }); |
| 92 | + }; |
| 93 | +} |
| 94 | + |
| 95 | +const showFeedbackForm = (): void => { |
| 96 | + FeedbackFormManager.show(); |
| 97 | +}; |
| 98 | + |
| 99 | +export { showFeedbackForm, FeedbackFormProvider }; |
0 commit comments