Skip to content

feat(react-native): Add Feedback Widget step #969

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 8 commits into from
Apr 24, 2025
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- fix(apple): add support for synchronized Xcode folders ([#904](https://github.com/getsentry/sentry-wizard/pull/904))
- feat(react-native): Add Feedback Widget step ([#969](https://github.com/getsentry/sentry-wizard/pull/969))

## 4.8.0

Expand Down
11 changes: 9 additions & 2 deletions e2e-tests/tests/expo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ describe('Expo', () => {
[KEYS.DOWN, KEYS.DOWN, KEYS.ENTER],
'Do you want to enable Session Replay to help debug issues? (See https://docs.sentry.io/platforms/react-native/session-replay/)',
));
const testEventPrompted =
const feedbackWidgetPrompted =
sessionReplayPrompted &&
(await wizardInstance.sendStdinAndWaitForOutput(
// Enable session replay
[KEYS.ENTER],
'Do you want to enable the Feedback Widget to collect feedback from your users? (See https://docs.sentry.io/platforms/react-native/user-feedback/)',
));
const testEventPrompted =
feedbackWidgetPrompted &&
(await wizardInstance.sendStdinAndWaitForOutput(
// Enable feedback widget
[KEYS.ENTER],
'Have you successfully sent a test event?',
));
testEventPrompted &&
Expand Down Expand Up @@ -66,7 +73,7 @@ Sentry.init({
// Configure Session Replay
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1,
integrations: [Sentry.mobileReplayIntegration()],
integrations: [Sentry.mobileReplayIntegration(), Sentry.feedbackIntegration()],

// uncomment the line below to enable Spotlight (https://spotlightjs.com)
// spotlight: __DEV__,
Expand Down
21 changes: 14 additions & 7 deletions e2e-tests/tests/react-native.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@ describe('ReactNative', () => {
[KEYS.DOWN, KEYS.DOWN, KEYS.ENTER],
'Do you want to enable Session Replay to help debug issues? (See https://docs.sentry.io/platforms/react-native/session-replay/)',
));
const feedbackWidgetPrompted =
sessionReplayPrompted &&
(await wizardInstance.sendStdinAndWaitForOutput(
// Enable session replay
[KEYS.ENTER],
'Do you want to enable the Feedback Widget to collect feedback from your users? (See https://docs.sentry.io/platforms/react-native/user-feedback/)',
));
const prettierPrompted =
sessionReplayPrompted &&
(await wizardInstance.sendStdinAndWaitForOutput(
// Enable session replay
[KEYS.ENTER],
'Looks like you have Prettier in your project. Do you want to run it on your files?',
));
feedbackWidgetPrompted &&
(await wizardInstance.sendStdinAndWaitForOutput(
// Enable feedback widget
[KEYS.ENTER],
'Looks like you have Prettier in your project. Do you want to run it on your files?',
));
const testEventPrompted =
prettierPrompted &&
(await wizardInstance.sendStdinAndWaitForOutput(
Expand Down Expand Up @@ -80,7 +87,7 @@ Sentry.init({
// Configure Session Replay
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1,
integrations: [Sentry.mobileReplayIntegration()],
integrations: [Sentry.mobileReplayIntegration(), Sentry.feedbackIntegration()],

// uncomment the line below to enable Spotlight (https://spotlightjs.com)
// spotlight: __DEV__,
Expand Down
66 changes: 49 additions & 17 deletions src/react-native/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@
import { generateCode, ProxifiedModule, parseModule } from 'magicast';
import * as t from '@babel/types';

const sessionReplaySampleRate = 0.1;
const sessionReplayOnErrorSampleRate = 1.0;
export const sessionReplaySampleRate = 0.1;
export const sessionReplayOnErrorSampleRate = 1.0;

export async function addSentryInit({
dsn,
enableSessionReplay = false,
enableFeedbackWidget = false,

Check warning on line 25 in src/react-native/javascript.ts

View check run for this annotation

Codecov / codecov/patch

src/react-native/javascript.ts#L25

Added line #L25 was not covered by tests
}: {
dsn: string;
enableSessionReplay?: boolean;
enableFeedbackWidget?: boolean;
}) {
const jsPath = getMainAppFilePath();
Sentry.setTag('app-js-file-status', jsPath ? 'found' : 'not-found');
Expand All @@ -34,7 +36,11 @@
);
await showCopyPasteInstructions({
filename: 'App.js or _layout.tsx',
codeSnippet: getSentryInitColoredCodeSnippet(dsn, enableSessionReplay),
codeSnippet: getSentryInitColoredCodeSnippet(
dsn,
enableSessionReplay,
enableFeedbackWidget,
),

Check warning on line 43 in src/react-native/javascript.ts

View check run for this annotation

Codecov / codecov/patch

src/react-native/javascript.ts#L39-L43

Added lines #L39 - L43 were not covered by tests
hint: 'This ensures the Sentry SDK is ready to capture errors.',
});
return;
Expand All @@ -55,19 +61,11 @@
return;
}

if (enableSessionReplay) {
clack.log.info(
`Session Replay will be enabled with default settings (replaysSessionSampleRate: ${sessionReplaySampleRate}, replaysOnErrorSampleRate: ${sessionReplayOnErrorSampleRate}).`,
);
clack.log.message(
'By default, all text content, images, and webviews will be masked for privacy. You can customize this in your code later.',
);
}

traceStep('add-sentry-init', () => {
const newContent = addSentryInitWithSdkImport(js, {
dsn,
enableSessionReplay,
enableFeedbackWidget,

Check warning on line 68 in src/react-native/javascript.ts

View check run for this annotation

Codecov / codecov/patch

src/react-native/javascript.ts#L68

Added line #L68 was not covered by tests
});

clack.log.success(
Expand All @@ -88,12 +86,21 @@
{
dsn,
enableSessionReplay = false,
}: { dsn: string; enableSessionReplay?: boolean },
enableFeedbackWidget = false,
}: {
dsn: string;
enableSessionReplay?: boolean;
enableFeedbackWidget?: boolean;
},
): string {
return js.replace(
/^([^]*)(import\s+[^;]*?;$)/m,
(match: string) => `${match}
${getSentryInitPlainTextSnippet(dsn, enableSessionReplay)}`,
${getSentryInitPlainTextSnippet(
dsn,
enableSessionReplay,
enableFeedbackWidget,
)}`,
);
}

Expand All @@ -107,15 +114,23 @@
export function getSentryInitColoredCodeSnippet(
dsn: string,
enableSessionReplay = false,
enableFeedbackWidget = false,

Check warning on line 117 in src/react-native/javascript.ts

View check run for this annotation

Codecov / codecov/patch

src/react-native/javascript.ts#L117

Added line #L117 was not covered by tests
) {
return makeCodeSnippet(true, (_unchanged, plus, _minus) => {
return plus(getSentryInitPlainTextSnippet(dsn, enableSessionReplay));
return plus(
getSentryInitPlainTextSnippet(
dsn,
enableSessionReplay,
enableFeedbackWidget,
),
);

Check warning on line 126 in src/react-native/javascript.ts

View check run for this annotation

Codecov / codecov/patch

src/react-native/javascript.ts#L120-L126

Added lines #L120 - L126 were not covered by tests
});
}

export function getSentryInitPlainTextSnippet(
dsn: string,
enableSessionReplay = false,
enableFeedbackWidget = false,
) {
return `import * as Sentry from '@sentry/react-native';

Expand All @@ -127,15 +142,32 @@
// Configure Session Replay
replaysSessionSampleRate: ${sessionReplaySampleRate},
replaysOnErrorSampleRate: ${sessionReplayOnErrorSampleRate},
integrations: [Sentry.mobileReplayIntegration()],
`
: ''
}
}${getSentryIntegrationsPlainTextSnippet(
enableSessionReplay,
enableFeedbackWidget,
)}
// uncomment the line below to enable Spotlight (https://spotlightjs.com)
// spotlight: __DEV__,
});`;
}

export function getSentryIntegrationsPlainTextSnippet(
enableSessionReplay = false,
enableFeedbackWidget = false,
) {
if (!enableSessionReplay && !enableFeedbackWidget) {
return '';
}
return ` integrations: [${
enableSessionReplay ? 'Sentry.mobileReplayIntegration()' : ''
}${enableSessionReplay && enableFeedbackWidget ? ', ' : ''}${
enableFeedbackWidget ? 'Sentry.feedbackIntegration()' : ''
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just adding the integration for now. We may add more options with with feature drop 2 (e.g. enable capture screenshot)

}],
`;
}

function getMainAppFilePath(): string | undefined {
const prefixGlob = '{.,./src,./app}';
const suffixGlob = '@(j|t|cj|mj)s?(x)';
Expand Down
56 changes: 55 additions & 1 deletion src/react-native/react-native-wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@
doesAppBuildGradleIncludeRNSentryGradlePlugin,
writeAppBuildGradle,
} from './gradle';
import { addSentryInit, wrapRootComponent } from './javascript';
import {
addSentryInit,
sessionReplayOnErrorSampleRate,
sessionReplaySampleRate,
wrapRootComponent,
} from './javascript';
import {
patchMetroConfigWithSentrySerializer,
patchMetroWithSentryConfig,
Expand Down Expand Up @@ -81,6 +86,11 @@
*/
export const SESSION_REPLAY_SUPPORTED_SDK_RANGE = '>=6.5.0';

/**
* The following SDK version supports the Feedback Widget
*/
export const FEEDBACK_WIDGET_SUPPORTED_SDK_RANGE = '>=6.9.0';

/**
* The following SDK version ship with bundled Expo plugin
*/
Expand Down Expand Up @@ -233,6 +243,15 @@
);

Sentry.setTag('enable-session-replay', enableSessionReplay);

if (enableSessionReplay) {
clack.log.info(
`Session Replay will be enabled with default settings (replaysSessionSampleRate: ${sessionReplaySampleRate}, replaysOnErrorSampleRate: ${sessionReplayOnErrorSampleRate}).`,
);
clack.log.message(
'By default, all text content, images, and webviews will be masked for privacy. You can customize this in your code later.',
);
}

Check warning on line 254 in src/react-native/react-native-wizard.ts

View check run for this annotation

Codecov / codecov/patch

src/react-native/react-native-wizard.ts#L247-L254

Added lines #L247 - L254 were not covered by tests
} else if (sdkVersion) {
clack.log.info(
`Session Replay is supported from Sentry React Native SDK version ${SESSION_REPLAY_SUPPORTED_SDK_RANGE}. Your version (${sdkVersion}) doesn't support it yet.`,
Expand All @@ -242,10 +261,45 @@
);
}

// Check if SDK version supports the Feedback Widget
let enableFeedbackWidget = false;
if (
sdkVersion &&
fulfillsVersionRange({
version: sdkVersion,
acceptableVersions: FEEDBACK_WIDGET_SUPPORTED_SDK_RANGE,
canBeLatest: true,
})
) {

Check warning on line 273 in src/react-native/react-native-wizard.ts

View check run for this annotation

Codecov / codecov/patch

src/react-native/react-native-wizard.ts#L265-L273

Added lines #L265 - L273 were not covered by tests
// Ask if user wants to enable the Feedback Widget
enableFeedbackWidget = await abortIfCancelled(
clack.confirm({
message:
'Do you want to enable the Feedback Widget to collect feedback from your users? (See https://docs.sentry.io/platforms/react-native/user-feedback/)',
}),
);

Check warning on line 280 in src/react-native/react-native-wizard.ts

View check run for this annotation

Codecov / codecov/patch

src/react-native/react-native-wizard.ts#L275-L280

Added lines #L275 - L280 were not covered by tests

Sentry.setTag('enable-feedback-widget', enableFeedbackWidget);

Check warning on line 282 in src/react-native/react-native-wizard.ts

View check run for this annotation

Codecov / codecov/patch

src/react-native/react-native-wizard.ts#L282

Added line #L282 was not covered by tests

if (enableFeedbackWidget) {
clack.log.info(
`The Feedback Widget will be enabled with default settings. You can show the widget by calling Sentry.showFeedbackWidget() in your code.`,
);
}
} else if (sdkVersion) {
clack.log.info(
`The Feedback Widget is supported from Sentry React Native SDK version ${FEEDBACK_WIDGET_SUPPORTED_SDK_RANGE}. Your version (${sdkVersion}) doesn't support it yet.`,
);
clack.log.message(
`To use the Feedback Widget, please upgrade your Sentry SDK: npm install ${RN_SDK_PACKAGE}@latest --save`,
);
}

Check warning on line 296 in src/react-native/react-native-wizard.ts

View check run for this annotation

Codecov / codecov/patch

src/react-native/react-native-wizard.ts#L284-L296

Added lines #L284 - L296 were not covered by tests

await traceStep('patch-app-js', () =>
addSentryInit({
dsn: selectedProject.keys[0].dsn.public,
enableSessionReplay,
enableFeedbackWidget,

Check warning on line 302 in src/react-native/react-native-wizard.ts

View check run for this annotation

Codecov / codecov/patch

src/react-native/react-native-wizard.ts#L302

Added line #L302 was not covered by tests
}),
);

Expand Down
Loading
Loading