Skip to content

Commit da0e3ea

Browse files
authored
Autoinject feedback form (#4370)
1 parent 265e629 commit da0e3ea

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ To learn how to attach context data to the feedback visit [the documentation](ht
3636
...
3737
<FeedbackForm/>
3838
```
39+
or auto-inject it by calling the `showFeedbackForm`:
40+
```jsx
41+
import { showFeedbackForm } from '@sentry/react-native';
42+
...
43+
<Button
44+
title="Show feedback form"
45+
onPress={() => {
46+
showFeedbackForm(_props.navigation);
47+
}}
48+
/>
49+
```
3950

4051
- Export `Span` type from `@sentry/types` ([#4345](https://github.com/getsentry/sentry-react-native/pull/4345))
4152
- Add RN SDK package to `sdk.packages` on Android ([#4380](https://github.com/getsentry/sentry-react-native/pull/4380))

packages/core/src/js/feedback/FeedbackForm.tsx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { SendFeedbackParams } from '@sentry/core';
2-
import { captureFeedback, getCurrentScope, lastEventId } from '@sentry/core';
2+
import { captureFeedback, getCurrentScope, lastEventId, logger } from '@sentry/core';
33
import * as React from 'react';
44
import type { KeyboardTypeOptions } from 'react-native';
55
import {
@@ -21,6 +21,31 @@ import { defaultConfiguration } from './defaults';
2121
import defaultStyles from './FeedbackForm.styles';
2222
import type { FeedbackFormProps, FeedbackFormState, FeedbackFormStyles,FeedbackGeneralConfiguration, FeedbackTextConfiguration } from './FeedbackForm.types';
2323

24+
let feedbackFormHandler: (() => void) | null = null;
25+
26+
const setFeedbackFormHandler = (handler: () => void): void => {
27+
feedbackFormHandler = handler;
28+
};
29+
30+
const clearFeedbackFormHandler = (): void => {
31+
feedbackFormHandler = null;
32+
};
33+
34+
type Navigation = {
35+
navigate: (screen: string, params?: Record<string, unknown>) => void;
36+
};
37+
38+
export const showFeedbackForm = (navigation: Navigation): void => {
39+
setFeedbackFormHandler(() => {
40+
navigation?.navigate?.('FeedbackForm');
41+
});
42+
if (feedbackFormHandler) {
43+
feedbackFormHandler();
44+
} else {
45+
logger.error('FeedbackForm handler is not set. Please ensure it is initialized.');
46+
}
47+
};
48+
2449
/**
2550
* @beta
2651
* Implements a feedback form screen that sends feedback to Sentry using Sentry.captureFeedback.
@@ -48,6 +73,13 @@ export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFor
4873
};
4974
}
5075

76+
/**
77+
* Clear the handler when the component unmounts
78+
*/
79+
public componentWillUnmount(): void {
80+
clearFeedbackFormHandler();
81+
}
82+
5183
public handleFeedbackSubmit: () => void = () => {
5284
const { name, email, description } = this.state;
5385
const { onFormClose } = this.props;

packages/core/src/js/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ export type { TimeToDisplayProps } from './tracing';
8585

8686
export { Mask, Unmask } from './replay/CustomMask';
8787

88-
export { FeedbackForm } from './feedback/FeedbackForm';
88+
export { FeedbackForm, showFeedbackForm } from './feedback/FeedbackForm';

samples/react-native/src/Screens/ErrorsScreen.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from 'react-native';
1313

1414
import * as Sentry from '@sentry/react-native';
15+
import { showFeedbackForm } from '@sentry/react-native';
1516

1617
import { setScopeProperties } from '../setScopeProperties';
1718
import { StackNavigationProp } from '@react-navigation/stack';
@@ -226,6 +227,12 @@ const ErrorsScreen = (_props: Props) => {
226227
_props.navigation.navigate('FeedbackForm');
227228
}}
228229
/>
230+
<Button
231+
title="Feedback form (autoinject)"
232+
onPress={() => {
233+
showFeedbackForm(_props.navigation);
234+
}}
235+
/>
229236
<Button
230237
title="Send user feedback"
231238
onPress={() => {

0 commit comments

Comments
 (0)