Skip to content

Commit

Permalink
feat: add referer option when calling attachTo/createWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
billyvg committed Oct 24, 2023
1 parent 45e66e9 commit e0037f1
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 16 deletions.
8 changes: 3 additions & 5 deletions packages/feedback/src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
SUBMIT_BUTTON_LABEL,
SUCCESS_MESSAGE_TEXT,
} from './constants';
import type { FeedbackConfigurationWithDefaults, Widget } from './types';
import type { CreateWidgetOptionOverrides, FeedbackConfigurationWithDefaults, Widget } from './types';
import { createActorStyles } from './widget/Actor.css';
import { createShadowHost } from './widget/createShadowHost';
import { createWidget } from './widget/createWidget';
Expand Down Expand Up @@ -84,7 +84,6 @@ export class Feedback implements Integration {

public constructor({
id = 'sentry-feedback',
// attachTo = null,
autoInject = true,
showEmail = true,
showName = true,
Expand Down Expand Up @@ -130,7 +129,6 @@ export class Feedback implements Integration {

this.options = {
id,
// attachTo,
autoInject,
isAnonymous,
isEmailRequired,
Expand Down Expand Up @@ -200,7 +198,7 @@ export class Feedback implements Integration {
/**
* Adds click listener to attached element to open a feedback dialog
*/
public attachTo(el: Node | string, optionOverrides: Partial<FeedbackConfigurationWithDefaults>): Widget | null {
public attachTo(el: Node | string, optionOverrides: CreateWidgetOptionOverrides): Widget | null {
try {
const options = Object.assign({}, this.options, optionOverrides);

Expand All @@ -227,7 +225,7 @@ export class Feedback implements Integration {
/**
* Creates a new widget. Accepts partial options to override any options passed to constructor.
*/
public createWidget(optionOverrides: Partial<FeedbackConfigurationWithDefaults>): Widget | null {
public createWidget(optionOverrides: CreateWidgetOptionOverrides): Widget | null {
try {
return this._createWidget(Object.assign({}, this.options, optionOverrides));
} catch (err) {
Expand Down
8 changes: 3 additions & 5 deletions packages/feedback/src/sendFeedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getCurrentHub } from '@sentry/core';
import { getLocationHref } from '@sentry/utils';

import { sendFeedbackRequest } from './util/sendFeedbackRequest';
import { SendFeedbackOptions } from './types';

interface SendFeedbackParams {
message: string;
Expand All @@ -11,16 +12,12 @@ interface SendFeedbackParams {
url?: string;
}

interface SendFeedbackOptions {
includeReplay?: boolean;
}

/**
* Public API to send a Feedback item to Sentry
*/
export function sendFeedback(
{ name, email, message, url = getLocationHref() }: SendFeedbackParams,
{ includeReplay = true }: SendFeedbackOptions = {},
{ referrer, includeReplay = true }: SendFeedbackOptions = {},
): ReturnType<typeof sendFeedbackRequest> {
const hub = getCurrentHub();
const client = hub && hub.getClient<BrowserClient>();
Expand All @@ -38,5 +35,6 @@ export function sendFeedback(
url,
replay_id: replayId,
},
referrer,
});
}
20 changes: 20 additions & 0 deletions packages/feedback/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,24 @@ export interface SendFeedbackData {
replay_id?: string;
name?: string;
};
referrer?: string;
}

export interface SendFeedbackOptions {
/**
* Should include replay with the feedback?
*/
includeReplay?: boolean;

/**
* Allows user to set a referrer for feedback, to act as a category for the feedback
*/
referrer?: string;
}

/**
* Feedback data expected from UI/form
*/
export interface FeedbackFormData {
message: string;
email?: string;
Expand Down Expand Up @@ -213,6 +229,10 @@ export interface FeedbackTheme {
error: string;
}

export interface CreateWidgetOptionOverrides extends Partial<FeedbackConfigurationWithDefaults> {
referrer?: string;
}

export interface FeedbackThemes {
dark: FeedbackTheme;
light: FeedbackTheme;
Expand Down
7 changes: 4 additions & 3 deletions packages/feedback/src/util/handleFeedbackSubmit.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { sendFeedback } from '../sendFeedback';
import type { FeedbackFormData } from '../types';
import type { FeedbackFormData, SendFeedbackOptions } from '../types';
import type { DialogComponent } from '../widget/Dialog';

/**
*
* Calls `sendFeedback` to send feedback, handles UI behavior of dialog.
*/
export async function handleFeedbackSubmit(
dialog: DialogComponent | null,
feedback: FeedbackFormData,
options?: SendFeedbackOptions
): Promise<Response | false> {
if (!dialog) {
// Not sure when this would happen
Expand All @@ -25,7 +26,7 @@ export async function handleFeedbackSubmit(
try {
dialog.hideError();
dialog.setSubmitDisabled();
const resp = await sendFeedback(feedback);
const resp = await sendFeedback(feedback, options);

if (!resp) {
// Errored... re-enable submit button
Expand Down
4 changes: 4 additions & 0 deletions packages/feedback/src/util/sendFeedbackRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { prepareFeedbackEvent } from './prepareFeedbackEvent';
*/
export async function sendFeedbackRequest({
feedback: { message, email, name, replay_id, url },
referrer,
}: SendFeedbackData): Promise<Response | null> {
const hub = getCurrentHub();

Expand All @@ -33,6 +34,9 @@ export async function sendFeedbackRequest({
replay_id,
url,
},
tags: {
referrer,
}
// type: 'feedback_event',
};

Expand Down
6 changes: 3 additions & 3 deletions packages/feedback/src/widget/createWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import { SuccessMessage } from './SuccessMessage';

interface CreateWidgetParams {
shadow: ShadowRoot;
options: FeedbackConfigurationWithDefaults;
options: FeedbackConfigurationWithDefaults & {referrer?: string};
attachTo?: Node;
}

/**
*
* Creates a new widget. Returns public methods that control widget behavior.
*/
export function createWidget({ shadow, options, attachTo }: CreateWidgetParams): Widget {
let actor: ActorComponent | undefined;
Expand Down Expand Up @@ -64,7 +64,7 @@ export function createWidget({ shadow, options, attachTo }: CreateWidgetParams):
return;
}

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

// Error submitting feedback
if (!result) {
Expand Down

0 comments on commit e0037f1

Please sign in to comment.