-
-
Notifications
You must be signed in to change notification settings - Fork 354
Add UserFeedback #2486
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
Merged
Merged
Add UserFeedback #2486
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a92e87a
Add UserFeedback draft
krystofwoldrich d9b0aab
Add user feedback to the sample
krystofwoldrich cdd4f01
Fix user feedback sample ui
krystofwoldrich e945ae3
Add headers to user feedback
krystofwoldrich dac2a92
Add user feedback integration test
krystofwoldrich 590e007
Merge branch 'main' into krystofwoldrich/addUserFeedback
krystofwoldrich 5ed2138
Update changelog
krystofwoldrich d8eca88
Fix lint
krystofwoldrich f15f49a
Move user feedback to sdk.tsx
krystofwoldrich 47b0837
Merge branch 'main' into krystofwoldrich/addUserFeedback
krystofwoldrich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import React, { useState } from 'react'; | ||
| import { View, Modal, StyleSheet, Text, TouchableOpacity, TextInput, Image } from 'react-native'; | ||
| import * as Sentry from '@sentry/react-native'; | ||
| import { UserFeedback } from '@sentry/react-native'; | ||
| import { styles as homeScreenStyles } from '../screens/HomeScreen'; | ||
|
|
||
| export const DEFAULT_COMMENTS = `It's broken again! Please fix it.`; | ||
|
|
||
| export function UserFeedbackModal() { | ||
| const [comments, onChangeComments] = React.useState(DEFAULT_COMMENTS); | ||
| const [modalVisible, setModalVisible] = useState(false); | ||
| const clearComments = () => onChangeComments(DEFAULT_COMMENTS); | ||
|
|
||
| return ( | ||
| <View> | ||
| <Modal | ||
| animationType="slide" | ||
| transparent={true} | ||
| visible={modalVisible} | ||
| onRequestClose={() => { | ||
| setModalVisible(!modalVisible); | ||
| }} | ||
| > | ||
| <View style={styles.centeredView}> | ||
| <View style={styles.modalView}> | ||
| <Image | ||
| source={require('../assets/sentry-announcement.png')} | ||
| style={styles.modalImage} | ||
| /> | ||
| <Text style={styles.modalText}>Whoops, what happened?</Text> | ||
| <TextInput | ||
| style={styles.input} | ||
| onChangeText={onChangeComments} | ||
| value={comments} | ||
| multiline={true} | ||
| numberOfLines={4} | ||
| /> | ||
| <TouchableOpacity | ||
| onPress={async () => { | ||
| setModalVisible(!modalVisible); | ||
|
|
||
| const sentryId = Sentry.captureMessage('Message that needs user feedback'); | ||
|
|
||
| const userFeedback: UserFeedback = { | ||
| event_id: sentryId, | ||
| name: 'John Doe', | ||
| email: '[email protected]', | ||
| comments, | ||
| }; | ||
|
|
||
| Sentry.captureUserFeedback(userFeedback); | ||
| clearComments(); | ||
| }}> | ||
| <Text style={homeScreenStyles.buttonText}>Send feedback</Text> | ||
| </TouchableOpacity> | ||
| <TouchableOpacity | ||
| onPress={async () => { | ||
| setModalVisible(!modalVisible); | ||
| }}> | ||
| <Text style={homeScreenStyles.buttonText}>Close</Text> | ||
| </TouchableOpacity> | ||
| </View> | ||
| </View> | ||
| </Modal> | ||
| <TouchableOpacity | ||
| onPress={async () => { | ||
| setModalVisible(true); | ||
| }}> | ||
| <Text style={homeScreenStyles.buttonText}>Send user feedback</Text> | ||
| </TouchableOpacity> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| centeredView: { | ||
| flex: 1, | ||
| justifyContent: "center", | ||
| alignItems: "center", | ||
| }, | ||
| modalView: { | ||
| margin: 5, | ||
| backgroundColor: "white", | ||
| borderRadius: 6, | ||
| padding: 25, | ||
| alignItems: "center", | ||
| shadowColor: "#000", | ||
| shadowOffset: { | ||
| width: 0, | ||
| height: 2 | ||
| }, | ||
| shadowOpacity: 0.25, | ||
| shadowRadius: 4, | ||
| elevation: 5 | ||
| }, | ||
| input: { | ||
| margin: 12, | ||
| marginBottom: 20, | ||
| borderWidth: 0.5, | ||
| borderColor: '#c6becf', | ||
| padding: 15, | ||
| borderRadius: 6, | ||
| height: 100, | ||
| width: 250, | ||
| textAlignVertical: 'top', | ||
| }, | ||
| modalText: { | ||
| marginBottom: 15, | ||
| textAlign: "center", | ||
| fontSize: 18, | ||
| }, | ||
| modalImage: { | ||
| marginBottom: 20, | ||
| width: 80, | ||
| height: 80, | ||
| } | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { | ||
| BaseEnvelopeHeaders, | ||
| DsnComponents, | ||
| EventEnvelope, | ||
| EventEnvelopeHeaders, | ||
| SdkMetadata, | ||
| UserFeedback, | ||
| UserFeedbackItem, | ||
| } from '@sentry/types'; | ||
| import { createEnvelope, dsnToString } from '@sentry/utils'; | ||
|
|
||
| /** | ||
| * Creates an envelope from a user feedback. | ||
| */ | ||
| export function createUserFeedbackEnvelope( | ||
| feedback: UserFeedback, | ||
| { | ||
| metadata, | ||
| tunnel, | ||
| dsn, | ||
| }: { | ||
| metadata: SdkMetadata | undefined, | ||
| tunnel: string | undefined, | ||
| dsn: DsnComponents | undefined, | ||
| }, | ||
| ): EventEnvelope { | ||
| // TODO: Use EventEnvelope[0] when JS sdk fix is released | ||
| const headers: EventEnvelopeHeaders & BaseEnvelopeHeaders = { | ||
| event_id: feedback.event_id, | ||
| sent_at: new Date().toISOString(), | ||
| ...(metadata && metadata.sdk && { sdk: metadata.sdk }), | ||
| ...(!!tunnel && !!dsn && { dsn: dsnToString(dsn) }), | ||
| }; | ||
| const item = createUserFeedbackEnvelopeItem(feedback); | ||
|
|
||
| return createEnvelope(headers, [item]); | ||
| } | ||
|
|
||
| function createUserFeedbackEnvelopeItem( | ||
| feedback: UserFeedback | ||
| ): UserFeedbackItem { | ||
| const feedbackHeaders: UserFeedbackItem[0] = { | ||
| type: 'user_report', | ||
| }; | ||
| return [feedbackHeaders, feedback]; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,17 +5,44 @@ import { ReactNativeClient } from '../src/js/client'; | |
| import { ReactNativeClientOptions, ReactNativeOptions } from '../src/js/options'; | ||
| import { NativeTransport } from '../src/js/transports/native'; | ||
| import { NATIVE } from '../src/js/wrapper'; | ||
| import { | ||
| envelopeHeader, | ||
| envelopeItemHeader, | ||
| envelopeItemPayload, | ||
| envelopeItems, | ||
| firstArg, | ||
| } from './testutils'; | ||
|
|
||
| const EXAMPLE_DSN = | ||
| 'https://[email protected]/148053'; | ||
|
|
||
| interface MockedReactNative { | ||
| NativeModules: { | ||
| RNSentry: { | ||
| initNativeSdk: jest.Mock; | ||
| crash: jest.Mock; | ||
| captureEnvelope: jest.Mock; | ||
| }; | ||
| }; | ||
| Platform: { | ||
| OS: 'mock'; | ||
| }; | ||
| LogBox: { | ||
| ignoreLogs: jest.Mock; | ||
| }; | ||
| YellowBox: { | ||
| ignoreWarnings: jest.Mock; | ||
| }; | ||
| } | ||
|
|
||
| jest.mock( | ||
| 'react-native', | ||
| () => ({ | ||
| (): MockedReactNative => ({ | ||
| NativeModules: { | ||
| RNSentry: { | ||
| initNativeSdk: jest.fn(() => Promise.resolve(true)), | ||
| crash: jest.fn(), | ||
| captureEnvelope: jest.fn(), | ||
| }, | ||
| }, | ||
| Platform: { | ||
|
|
@@ -100,7 +127,7 @@ describe('Tests ReactNativeClient', () => { | |
| // eslint-disable-next-line deprecation/deprecation | ||
| await expect(RN.YellowBox.ignoreWarnings).toBeCalled(); | ||
| }); | ||
|
|
||
| test('use custom transport function', async () => { | ||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| const mySend = (request: Envelope) => Promise.resolve(); | ||
|
|
@@ -149,11 +176,11 @@ describe('Tests ReactNativeClient', () => { | |
| }); | ||
|
|
||
| test('calls onReady callback with false if Native SDK failed to initialize', (done) => { | ||
| const RN = require('react-native'); | ||
| const RN: MockedReactNative = require('react-native'); | ||
|
|
||
| RN.NativeModules.RNSentry.initNativeSdk = async () => { | ||
| RN.NativeModules.RNSentry.initNativeSdk = jest.fn(() => { | ||
| throw new Error(); | ||
| }; | ||
| }); | ||
|
|
||
| new ReactNativeClient({ | ||
| dsn: EXAMPLE_DSN, | ||
|
|
@@ -170,7 +197,7 @@ describe('Tests ReactNativeClient', () => { | |
|
|
||
| describe('nativeCrash', () => { | ||
| test('calls NativeModules crash', () => { | ||
| const RN = require('react-native'); | ||
| const RN: MockedReactNative = require('react-native'); | ||
|
|
||
| const client = new ReactNativeClient({ | ||
| ...DEFAULT_OPTIONS, | ||
|
|
@@ -183,4 +210,36 @@ describe('Tests ReactNativeClient', () => { | |
| expect(RN.NativeModules.RNSentry.crash).toBeCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('UserFeedback', () => { | ||
| test('sends UserFeedback to native Layer', () => { | ||
| const mockTransportSend: jest.Mock = jest.fn(() => Promise.resolve()); | ||
| const client = new ReactNativeClient({ | ||
| ...DEFAULT_OPTIONS, | ||
| dsn: EXAMPLE_DSN, | ||
| transport: () => ({ | ||
| send: mockTransportSend, | ||
| flush: jest.fn(), | ||
| }), | ||
| } as ReactNativeClientOptions); | ||
|
|
||
| client.captureUserFeedback({ | ||
| comments: 'Test Comments', | ||
| email: '[email protected]', | ||
| name: 'Test User', | ||
| event_id: 'testEvent123', | ||
| }); | ||
|
|
||
| expect(mockTransportSend.mock.calls[0][firstArg][envelopeHeader].event_id).toEqual('testEvent123'); | ||
| expect(mockTransportSend.mock.calls[0][firstArg][envelopeItems][0][envelopeItemHeader].type).toEqual( | ||
| 'user_report' | ||
| ); | ||
| expect(mockTransportSend.mock.calls[0][firstArg][envelopeItems][0][envelopeItemPayload]).toEqual({ | ||
| comments: 'Test Comments', | ||
| email: '[email protected]', | ||
| name: 'Test User', | ||
| event_id: 'testEvent123', | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.