Skip to content

feat(feedback): Add getFeedback utility to get typed feedback instance #11331

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 1 commit into from
Mar 28, 2024
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
3 changes: 2 additions & 1 deletion packages/browser/src/index.bundle.feedback.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This is exported so the loader does not fail when switching off Replay/Tracing
import { feedbackIntegration } from '@sentry-internal/feedback';
import { feedbackIntegration, getFeedback } from '@sentry-internal/feedback';
import {
addTracingExtensionsShim,
browserTracingIntegrationShim,
Expand All @@ -12,5 +12,6 @@ export {
addTracingExtensionsShim as addTracingExtensions,
replayIntegrationShim as replayIntegration,
feedbackIntegration,
getFeedback,
};
// Note: We do not export a shim for `Span` here, as that is quite complex and would blow up the bundle
3 changes: 2 additions & 1 deletion packages/browser/src/index.bundle.tracing.replay.feedback.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { feedbackIntegration } from '@sentry-internal/feedback';
import { feedbackIntegration, getFeedback } from '@sentry-internal/feedback';
import { replayIntegration } from '@sentry-internal/replay';
import {
browserTracingIntegration,
Expand Down Expand Up @@ -27,6 +27,7 @@ export {
addTracingExtensions,
startBrowserTracingNavigationSpan,
startBrowserTracingPageLoadSpan,
getFeedback,
};

export * from './index.bundle.base';
1 change: 1 addition & 0 deletions packages/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export { replayCanvasIntegration } from '@sentry-internal/replay-canvas';

export {
feedbackIntegration,
getFeedback,
sendFeedback,
} from '@sentry-internal/feedback';

Expand Down
40 changes: 40 additions & 0 deletions packages/feedback/src/core/getFeedback.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { getCurrentScope } from '@sentry/core';
import { getFeedback } from './getFeedback';
import { feedbackIntegration } from './integration';
import { mockSdk } from './mockSdk';

describe('getFeedback', () => {
beforeEach(() => {
getCurrentScope().setClient(undefined);
});

it('works without a client', () => {
const actual = getFeedback();
expect(actual).toBeUndefined();
});

it('works with a client without Feedback', () => {
mockSdk({
sentryOptions: {
integrations: [],
},
});

const actual = getFeedback();
expect(actual).toBeUndefined();
});

it('works with a client with Feedback', () => {
const feedback = feedbackIntegration();

mockSdk({
sentryOptions: {
integrations: [feedback],
},
});

const actual = getFeedback();
expect(actual).toBeDefined();
expect(actual === feedback).toBe(true);
});
});
10 changes: 10 additions & 0 deletions packages/feedback/src/core/getFeedback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getClient } from '@sentry/core';
import type { feedbackIntegration } from './integration';

/**
* This is a small utility to get a type-safe instance of the Feedback integration.
*/
export function getFeedback(): ReturnType<typeof feedbackIntegration> | undefined {
const client = getClient();
return client && client.getIntegrationByName<ReturnType<typeof feedbackIntegration>>('Feedback');
}
9 changes: 5 additions & 4 deletions packages/feedback/src/core/integration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineIntegration, getClient } from '@sentry/core';
import { getClient } from '@sentry/core';
import type { Integration, IntegrationFn } from '@sentry/types';
import { isBrowser, logger } from '@sentry/utils';
import {
Expand Down Expand Up @@ -45,7 +45,10 @@ interface PublicFeedbackIntegration {
}
export type IFeedbackIntegration = Integration & PublicFeedbackIntegration;

export const _feedbackIntegration = (({
/**
* Allow users to capture user feedback and send it to Sentry.
*/
export const feedbackIntegration = (({
// FeedbackGeneralConfiguration
id = 'sentry-feedback',
showBranding = true,
Expand Down Expand Up @@ -279,5 +282,3 @@ export const _feedbackIntegration = (({
},
};
}) satisfies IntegrationFn;

export const feedbackIntegration = defineIntegration(_feedbackIntegration);
Copy link
Member

Choose a reason for hiding this comment

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

Nice not to need this anymore!

Copy link
Member Author

Choose a reason for hiding this comment

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

for reference, the reason we generally have this is because for most integrations, we do not want to expose to users what shape they have, as then we cannot refactor internals anymore - we only expose it if we have to (which we do for replay & feedback) :)

1 change: 1 addition & 0 deletions packages/feedback/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { sendFeedback } from './core/sendFeedback';
export { feedbackIntegration } from './core/integration';
export { feedbackModalIntegration } from './modal/integration';
export { getFeedback } from './core/getFeedback';
export { feedbackScreenshotIntegration } from './screenshot/integration';

export type { OptionalFeedbackConfiguration } from './types';
Expand Down