Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoinject feedback widget #4483

Merged
merged 14 commits into from
Jan 29, 2025
92 changes: 92 additions & 0 deletions packages/core/src/js/feedback/FeedbackFormManager.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import * as React from 'react';
import { Modal, StyleSheet,View } from 'react-native';

import { FeedbackForm } from './FeedbackForm';

class FeedbackFormManager {
private static _isVisible = false;
private static _setVisibility: (visible: boolean) => void;

public static initialize(setVisibility: (visible: boolean) => void): void {
this._setVisibility = setVisibility;
}

public static show(): void {
if (this._setVisibility) {
this._isVisible = true;
this._setVisibility(true);
}
}

public static hide(): void {
if (this._setVisibility) {
this._isVisible = false;
this._setVisibility(false);
}
}

public static isFormVisible(): boolean {
return this._isVisible;
}
}

interface FeedbackFormProviderProps {
children: React.ReactNode;
}

class FeedbackFormProvider extends React.Component<FeedbackFormProviderProps> {
public state = {
isVisible: false,
};

public constructor(props: FeedbackFormProviderProps) {
super(props);
FeedbackFormManager.initialize(this._setVisibilityFunction);
}

/**
* Renders the feedback form modal.
*/
public render(): React.ReactNode {
const { isVisible } = this.state;

return (
<>
{this.props.children}
{isVisible && (
<Modal visible={isVisible} transparent animationType="slide">
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved
<View style={styles.modalBackground}>
<FeedbackForm
onFormClose={this._handleClose}
onFormSubmitted={this._handleClose}
/>
</View>
</Modal>
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved
)}
</>
);
}

private _setVisibilityFunction = (visible: boolean): void => {
this.setState({ isVisible: visible });
};

private _handleClose = (): void => {
FeedbackFormManager.hide();
this.setState({ isVisible: false });
};
}

const showFeedbackForm = (): void => {
FeedbackFormManager.show();
};

const styles = StyleSheet.create({
modalBackground: {
flex: 1,
justifyContent: 'center',
backgroundColor: 'rgba(0,0,0,0.5)',
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved
},
});

export { showFeedbackForm, FeedbackFormProvider };
1 change: 1 addition & 0 deletions packages/core/src/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ export type { TimeToDisplayProps } from './tracing';
export { Mask, Unmask } from './replay/CustomMask';

export { FeedbackForm } from './feedback/FeedbackForm';
export { showFeedbackForm } from './feedback/FeedbackFormManager';
5 changes: 4 additions & 1 deletion packages/core/src/js/sdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import * as React from 'react';

import { ReactNativeClient } from './client';
import { FeedbackFormProvider } from './feedback/FeedbackFormManager';
import { getDevServer } from './integrations/debugsymbolicatorutils';
import { getDefaultIntegrations } from './integrations/default';
import type { ReactNativeClientOptions, ReactNativeOptions, ReactNativeWrapperOptions } from './options';
Expand Down Expand Up @@ -163,7 +164,9 @@ export function wrap<P extends Record<string, unknown>>(
return (
<TouchEventBoundary {...(options?.touchEventBoundaryProps ?? {})}>
<ReactNativeProfiler {...profilerProps}>
<RootComponent {...appProps} />
<FeedbackFormProvider>
<RootComponent {...appProps} />
</FeedbackFormProvider>
</ReactNativeProfiler>
</TouchEventBoundary>
);
Expand Down
1 change: 0 additions & 1 deletion samples/react-native/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ function App() {
return (
<>
<RootNavigationContainer />
<RunningIndicator />
antonis marked this conversation as resolved.
Show resolved Hide resolved
</>
);
}
Expand Down
6 changes: 6 additions & 0 deletions samples/react-native/src/Screens/ErrorsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@ const ErrorsScreen = (_props: Props) => {
_props.navigation.navigate('FeedbackForm');
}}
/>
<Button
title="Feedback form (auto)"
onPress={() => {
Sentry.showFeedbackForm();
}}
/>
<Button
title="Send user feedback"
onPress={() => {
Expand Down
Loading