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

Add UserFeedback #2486

Merged
merged 10 commits into from
Sep 22, 2022
17 changes: 16 additions & 1 deletion src/js/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ import { BrowserClient, defaultStackParser, makeFetchTransport } from '@sentry/b
import { BrowserTransportOptions } from '@sentry/browser/types/transports/types';
import { FetchImpl } from '@sentry/browser/types/transports/utils';
import { BaseClient } from '@sentry/core';
import { Event, EventHint, SeverityLevel, Transport } from '@sentry/types';
import {
Event,
EventHint,
SeverityLevel,
Transport,
UserFeedback,
} from '@sentry/types';
// @ts-ignore LogBox introduced in RN 0.63
import { Alert, LogBox, YellowBox } from 'react-native';

import { createUserFeedbackEnvelope } from './envelope';
import { ReactNativeClientOptions } from './options';
import { NativeTransport } from './transports/native';
import { NATIVE } from './wrapper';
Expand Down Expand Up @@ -89,6 +96,14 @@ export class ReactNativeClient extends BaseClient<ReactNativeClientOptions> {
});
}

/**
* Sends user feedback to Sentry.
*/
public captureUserFeedback(feedback: UserFeedback): void {
const envelope = createUserFeedbackEnvelope(feedback);
this._sendEnvelope(envelope);
}

/**
* Starts native client with dsn and options
*/
Expand Down
31 changes: 31 additions & 0 deletions src/js/envelope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
EventEnvelope,
EventEnvelopeHeaders,
UserFeedback,
UserFeedbackItem,
} from '@sentry/types';
import { createEnvelope } from '@sentry/utils';

/**
* Creates an envelope from a user feedback.
*/
export function createUserFeedbackEnvelope(
feedback: UserFeedback
): EventEnvelope {
const headers: EventEnvelopeHeaders = {
event_id: feedback.event_id, // not sure feedback.event_id is correct
sent_at: new Date().toISOString(),
};
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved
const item = createUserFeedbackEnvelopeItem(feedback);

return createEnvelope(headers, [item]);
}

function createUserFeedbackEnvelopeItem(
feedback: UserFeedback
): UserFeedbackItem {
const feedbackHeaders: UserFeedbackItem[0] = {
type: 'user_report',
};
return [feedbackHeaders, feedback];
}
2 changes: 1 addition & 1 deletion src/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export {
StackFrame,
Stacktrace,
Thread,
User,
} from '@sentry/types';

export {
Expand Down Expand Up @@ -49,6 +48,7 @@ export {
withProfiler,
} from '@sentry/react';

export { User, UserFeedback, captureUserFeedback } from './user';
import * as Integrations from './integrations';
import { SDK_NAME, SDK_VERSION } from './version';
export { ReactNativeOptions } from './options';
Expand Down
13 changes: 13 additions & 0 deletions src/js/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getCurrentHub } from '@sentry/hub';
import { UserFeedback } from '@sentry/types';

import { ReactNativeClient } from './client';

/**
* Captures a user feedback and sends it to Sentry.
*/
export function captureUserFeedback(feedback: UserFeedback): void {
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved
getCurrentHub().getClient<ReactNativeClient>()?.captureUserFeedback(feedback);
}

export { User, UserFeedback } from '@sentry/types';