Skip to content

Commit f0e1bef

Browse files
committed
Use Sentry user email and name when set
1 parent 458ebc2 commit f0e1bef

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as React from 'react';
44
import type { KeyboardTypeOptions } from 'react-native';
55
import { Alert, Text, TextInput, TouchableOpacity, View } from 'react-native';
66

7-
import { defaultConfiguration } from './constants';
7+
import { defaultConfiguration } from './defaults';
88
import defaultStyles from './FeedbackForm.styles';
99
import type { FeedbackFormProps, FeedbackFormState, FeedbackFormStyles,FeedbackGeneralConfiguration, FeedbackTextConfiguration } from './FeedbackForm.types';
1010

@@ -15,9 +15,11 @@ import type { FeedbackFormProps, FeedbackFormState, FeedbackFormStyles,FeedbackG
1515
export class FeedbackForm extends React.Component<FeedbackFormProps, FeedbackFormState> {
1616
public constructor(props: FeedbackFormProps) {
1717
super(props);
18+
19+
const config: FeedbackGeneralConfiguration = { ...defaultConfiguration, ...props };
1820
this.state = {
19-
name: '',
20-
email: '',
21+
name: config.useSentryUser.name,
22+
email: config.useSentryUser.email,
2123
description: '',
2224
};
2325
}

packages/core/src/js/feedback/FeedbackForm.types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ export interface FeedbackGeneralConfiguration {
2828
* Should the name input field be visible? Note: name will still be collected if set via `Sentry.setUser()`
2929
*/
3030
showName?: boolean;
31+
32+
/**
33+
* Fill in email/name input fields with Sentry user context if it exists.
34+
* The value of the email/name keys represent the properties of your user context.
35+
*/
36+
useSentryUser?: {
37+
email: string;
38+
name: string;
39+
};
3140
}
3241

3342
/**

packages/core/src/js/feedback/constants.ts renamed to packages/core/src/js/feedback/defaults.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { getCurrentScope } from '@sentry/core';
2+
13
import type { FeedbackFormProps } from './FeedbackForm.types';
24

35
const FORM_TITLE = 'Report a Bug';
@@ -20,6 +22,10 @@ export const defaultConfiguration: Partial<FeedbackFormProps> = {
2022
isNameRequired: false,
2123
showEmail: true,
2224
showName: true,
25+
useSentryUser: {
26+
email: getCurrentScope().getUser().email || '',
27+
name: getCurrentScope().getUser().name || '',
28+
},
2329

2430
// FeedbackTextConfiguration
2531
cancelButtonLabel: CANCEL_BUTTON_LABEL,

packages/core/test/feedback/FeedbackForm.test.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ jest.spyOn(Alert, 'alert');
1212

1313
jest.mock('@sentry/core', () => ({
1414
captureFeedback: jest.fn(),
15+
getCurrentScope: jest.fn(() => ({
16+
getUser: jest.fn(() => ({
17+
18+
name: 'Test User',
19+
})),
20+
})),
1521
}));
1622

1723
const defaultProps: FeedbackFormProps = {
@@ -50,6 +56,16 @@ describe('FeedbackForm', () => {
5056
expect(getByText(defaultProps.cancelButtonLabel)).toBeTruthy();
5157
});
5258

59+
it('name and email are prefilled when sentry user is set', () => {
60+
const { getByPlaceholderText } = render(<FeedbackForm {...defaultProps} />);
61+
62+
const nameInput = getByPlaceholderText(defaultProps.namePlaceholder);
63+
const emailInput = getByPlaceholderText(defaultProps.emailPlaceholder);
64+
65+
expect(nameInput.props.value).toBe('Test User');
66+
expect(emailInput.props.value).toBe('[email protected]');
67+
});
68+
5369
it('shows an error message if required fields are empty', async () => {
5470
const { getByText } = render(<FeedbackForm {...defaultProps} />);
5571

0 commit comments

Comments
 (0)