Skip to content

Commit 413ee5a

Browse files
committed
feat: add referer option when calling attachTo/createWidget
1 parent e61571f commit 413ee5a

File tree

6 files changed

+37
-16
lines changed

6 files changed

+37
-16
lines changed

packages/feedback/src/integration.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
SUBMIT_BUTTON_LABEL,
1717
SUCCESS_MESSAGE_TEXT,
1818
} from './constants';
19-
import type { FeedbackConfigurationWithDefaults, Widget } from './types';
19+
import type { CreateWidgetOptionOverrides, FeedbackConfigurationWithDefaults, Widget } from './types';
2020
import { createActorStyles } from './widget/Actor.css';
2121
import { createShadowHost } from './widget/createShadowHost';
2222
import { createWidget } from './widget/createWidget';
@@ -94,7 +94,6 @@ export class Feedback implements Integration {
9494

9595
public constructor({
9696
id = 'sentry-feedback',
97-
// attachTo = null,
9897
autoInject = true,
9998
showEmail = true,
10099
showName = true,
@@ -140,7 +139,6 @@ export class Feedback implements Integration {
140139

141140
this.options = {
142141
id,
143-
// attachTo,
144142
autoInject,
145143
isAnonymous,
146144
isEmailRequired,
@@ -210,7 +208,7 @@ export class Feedback implements Integration {
210208
/**
211209
* Adds click listener to attached element to open a feedback dialog
212210
*/
213-
public attachTo(el: Node | string, optionOverrides: Partial<FeedbackConfigurationWithDefaults>): Widget | null {
211+
public attachTo(el: Node | string, optionOverrides: CreateWidgetOptionOverrides): Widget | null {
214212
try {
215213
const options = Object.assign({}, this.options, optionOverrides);
216214

@@ -237,7 +235,7 @@ export class Feedback implements Integration {
237235
/**
238236
* Creates a new widget. Accepts partial options to override any options passed to constructor.
239237
*/
240-
public createWidget(optionOverrides: Partial<FeedbackConfigurationWithDefaults>): Widget | null {
238+
public createWidget(optionOverrides: CreateWidgetOptionOverrides): Widget | null {
241239
try {
242240
return this._createWidget(Object.assign({}, this.options, optionOverrides));
243241
} catch (err) {

packages/feedback/src/sendFeedback.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getCurrentHub } from '@sentry/core';
33
import { getLocationHref } from '@sentry/utils';
44

55
import { sendFeedbackRequest } from './util/sendFeedbackRequest';
6+
import { SendFeedbackOptions } from './types';
67

78
interface SendFeedbackParams {
89
message: string;
@@ -11,16 +12,12 @@ interface SendFeedbackParams {
1112
url?: string;
1213
}
1314

14-
interface SendFeedbackOptions {
15-
includeReplay?: boolean;
16-
}
17-
1815
/**
1916
* Public API to send a Feedback item to Sentry
2017
*/
2118
export function sendFeedback(
2219
{ name, email, message, url = getLocationHref() }: SendFeedbackParams,
23-
{ includeReplay = true }: SendFeedbackOptions = {},
20+
{ referrer, includeReplay = true }: SendFeedbackOptions = {},
2421
): ReturnType<typeof sendFeedbackRequest> {
2522
const hub = getCurrentHub();
2623
const client = hub && hub.getClient<BrowserClient>();
@@ -38,5 +35,6 @@ export function sendFeedback(
3835
url,
3936
replay_id: replayId,
4037
},
38+
referrer,
4139
});
4240
}

packages/feedback/src/types/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,24 @@ export interface SendFeedbackData {
2929
replay_id?: string;
3030
name?: string;
3131
};
32+
referrer?: string;
3233
}
3334

35+
export interface SendFeedbackOptions {
36+
/**
37+
* Should include replay with the feedback?
38+
*/
39+
includeReplay?: boolean;
40+
41+
/**
42+
* Allows user to set a referrer for feedback, to act as a category for the feedback
43+
*/
44+
referrer?: string;
45+
}
46+
47+
/**
48+
* Feedback data expected from UI/form
49+
*/
3450
export interface FeedbackFormData {
3551
message: string;
3652
email?: string;
@@ -213,6 +229,10 @@ export interface FeedbackTheme {
213229
error: string;
214230
}
215231

232+
export interface CreateWidgetOptionOverrides extends Partial<FeedbackConfigurationWithDefaults> {
233+
referrer?: string;
234+
}
235+
216236
export interface FeedbackThemes {
217237
dark: FeedbackTheme;
218238
light: FeedbackTheme;

packages/feedback/src/util/handleFeedbackSubmit.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { sendFeedback } from '../sendFeedback';
2-
import type { FeedbackFormData } from '../types';
2+
import type { FeedbackFormData, SendFeedbackOptions } from '../types';
33
import type { DialogComponent } from '../widget/Dialog';
44

55
/**
6-
*
6+
* Calls `sendFeedback` to send feedback, handles UI behavior of dialog.
77
*/
88
export async function handleFeedbackSubmit(
99
dialog: DialogComponent | null,
1010
feedback: FeedbackFormData,
11+
options?: SendFeedbackOptions
1112
): Promise<Response | false> {
1213
if (!dialog) {
1314
// Not sure when this would happen
@@ -25,7 +26,7 @@ export async function handleFeedbackSubmit(
2526
try {
2627
dialog.hideError();
2728
dialog.setSubmitDisabled();
28-
const resp = await sendFeedback(feedback);
29+
const resp = await sendFeedback(feedback, options);
2930

3031
if (!resp) {
3132
// Errored... re-enable submit button

packages/feedback/src/util/sendFeedbackRequest.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { prepareFeedbackEvent } from './prepareFeedbackEvent';
99
*/
1010
export async function sendFeedbackRequest({
1111
feedback: { message, email, name, replay_id, url },
12+
referrer,
1213
}: SendFeedbackData): Promise<Response | null> {
1314
const hub = getCurrentHub();
1415

@@ -33,6 +34,9 @@ export async function sendFeedbackRequest({
3334
replay_id,
3435
url,
3536
},
37+
tags: {
38+
referrer,
39+
}
3640
// type: 'feedback_event',
3741
};
3842

packages/feedback/src/widget/createWidget.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import { SuccessMessage } from './SuccessMessage';
1111

1212
interface CreateWidgetParams {
1313
shadow: ShadowRoot;
14-
options: FeedbackConfigurationWithDefaults;
14+
options: FeedbackConfigurationWithDefaults & {referrer?: string};
1515
attachTo?: Node;
1616
}
1717

1818
/**
19-
*
19+
* Creates a new widget. Returns public methods that control widget behavior.
2020
*/
2121
export function createWidget({ shadow, options, attachTo }: CreateWidgetParams): Widget {
2222
let actor: ActorComponent | undefined;
@@ -64,7 +64,7 @@ export function createWidget({ shadow, options, attachTo }: CreateWidgetParams):
6464
return;
6565
}
6666

67-
const result = await handleFeedbackSubmit(dialog, feedback);
67+
const result = await handleFeedbackSubmit(dialog, feedback, {referrer: options.referrer});
6868

6969
// Error submitting feedback
7070
if (!result) {

0 commit comments

Comments
 (0)