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

(2.3) Auto-inject feedback widget #4370

Open
wants to merge 3 commits into
base: antonis/3859-newCaptureFeedbackAPI-Form
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@
...
<FeedbackForm/>
```
or auto-inject it by calling the `showFeedbackForm`:
```jsx
import { showFeedbackForm } from '@sentry/react-native';
...
<Button
title="Show feedback form"
onPress={() => {
showFeedbackForm(_props.navigation);
}}
/>
```

- Export `Span` type from `@sentry/types` ([#4345](https://github.com/getsentry/sentry-react-native/pull/4345))

Expand Down
34 changes: 33 additions & 1 deletion packages/core/src/js/feedback/FeedbackForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { captureFeedback, getCurrentScope, lastEventId } from '@sentry/core';
import { captureFeedback, getCurrentScope, lastEventId, logger } from '@sentry/core';
import type { SendFeedbackParams } from '@sentry/types';
import * as React from 'react';
import type { KeyboardTypeOptions } from 'react-native';
Expand All @@ -19,6 +19,31 @@ import { defaultConfiguration } from './defaults';
import defaultStyles from './FeedbackForm.styles';
import type { FeedbackFormProps, FeedbackFormState, FeedbackFormStyles,FeedbackGeneralConfiguration, FeedbackTextConfiguration } from './FeedbackForm.types';

let feedbackFormHandler: (() => void) | null = null;

const setFeedbackFormHandler = (handler: () => void): void => {
feedbackFormHandler = handler;
};

const clearFeedbackFormHandler = (): void => {
feedbackFormHandler = null;
};

type Navigation = {
navigate: (screen: string, params?: Record<string, unknown>) => void;
};

export const showFeedbackForm = (navigation: Navigation): void => {
setFeedbackFormHandler(() => {
navigation?.navigate?.('FeedbackForm');
});
if (feedbackFormHandler) {
feedbackFormHandler();
} else {
logger.error('FeedbackForm handler is not set. Please ensure it is initialized.');
}
};

/**
* @beta
* Implements a feedback form screen that sends feedback to Sentry using Sentry.captureFeedback.
Expand All @@ -45,6 +70,13 @@ export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFor
};
}

/**
* Clear the handler when the component unmounts
*/
public componentWillUnmount(): void {
clearFeedbackFormHandler();
}

public handleFeedbackSubmit: () => void = () => {
const { name, email, description } = this.state;
const { onFormClose } = this._config;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ export type { TimeToDisplayProps } from './tracing';

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

export { FeedbackForm } from './feedback/FeedbackForm';
export { FeedbackForm, showFeedbackForm } from './feedback/FeedbackForm';
7 changes: 7 additions & 0 deletions samples/react-native/src/Screens/ErrorsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from 'react-native';

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

import { setScopeProperties } from '../setScopeProperties';
import { StackNavigationProp } from '@react-navigation/stack';
Expand Down Expand Up @@ -226,6 +227,12 @@ const ErrorsScreen = (_props: Props) => {
_props.navigation.navigate('FeedbackForm');
}}
/>
<Button
title="Feedback form (autoinject)"
onPress={() => {
showFeedbackForm(_props.navigation);
}}
/>
<Button
title="Send user feedback"
onPress={() => {
Expand Down
Loading