11import { ClientLike } from '@sentry/types' ;
22import { captureException , getCarrier , getCurrentClient } from '@sentry/minimal' ;
33import { addInstrumentationHandler , getGlobalObject , logger } from '@sentry/utils' ;
4- import { ReportDialogOptions } from '@sentry/transport-base' ;
4+ import { Dsn , getReportDialogEndpoint , ReportDialogOptions } from '@sentry/transport-base' ;
55import { InboundFilters } from '@sentry/integration-common-inboundfilters' ;
66import { UserAgent } from '@sentry/integration-browser-useragent' ;
77import { EventTargetWrap , TimersWrap , XHRWrap } from '@sentry/integration-browser-wrap' ;
@@ -16,7 +16,6 @@ import { LinkedErrors } from '@sentry/integration-browser-linkederrors';
1616import { OnError , OnUnhandledRejection } from '@sentry/integration-browser-globalhandlers' ;
1717
1818import { BrowserClient , BrowserOptions } from './client' ;
19- import { injectReportDialog } from './helpers' ;
2019
2120export const defaultIntegrations = [
2221 new EventTargetWrap ( ) ,
@@ -121,28 +120,50 @@ export function init(options: BrowserOptions = {}): void {
121120 *
122121 * @param options Everything is optional, we try to fetch all info need from the global scope.
123122 */
124- export function showReportDialog ( options : ReportDialogOptions = { } , client ?: ClientLike ) : void {
123+ export function showReportDialog (
124+ options : ReportDialogOptions & { onLoad ?( ) : void } = { } ,
125+ customClient ?: ClientLike ,
126+ ) : void {
127+ const errPrefix = `Trying to call showReportDialog with` ;
128+
125129 // doesn't work without a document (React Native)
126- const document = getGlobalObject < Window > ( ) . document ;
127- if ( ! document ) {
130+ const global = getGlobalObject < Window > ( ) ;
131+ if ( ! global . document ) {
132+ return ;
133+ }
134+
135+ const client = customClient ?? getCurrentClient ( ) ;
136+ if ( ! client ) {
137+ return ;
138+ }
139+
140+ options . eventId = options . eventId ?? client . lastEventId ( ) ;
141+ options . dsn = options . dsn ?? client . getDsn ( ) ?. toString ( ) ;
142+
143+ if ( client . options . enabled === false ) {
144+ logger . error ( `${ errPrefix } disabled client` ) ;
145+ return ;
146+ }
147+
148+ if ( ! options . eventId ) {
149+ logger . error ( `${ errPrefix } missing EventID` ) ;
128150 return ;
129151 }
130152
131- const usableClient = client ?? getCurrentClient ( ) ;
132- if ( ! usableClient ) {
153+ if ( ! options . dsn ) {
154+ logger . error ( ` ${ errPrefix } missing DSN` ) ;
133155 return ;
134156 }
135157
136- options . eventId = options . eventId ?? usableClient . lastEventId ( ) ;
137- options . dsn = options . dsn ?? usableClient . getDsn ( ) ?. toString ( ) ;
158+ const script = document . createElement ( 'script' ) ;
159+ script . async = true ;
160+ script . src = getReportDialogEndpoint ( new Dsn ( options . dsn ) ) ;
138161
139- // TODO: Should we keep `isEnabled` around?
140- // if (!this._isEnabled()) {
141- // logger.error('Trying to call showReportDialog with Sentry Client disabled');
142- // return;
143- // }
162+ if ( options . onLoad ) {
163+ script . onload = options . onLoad ; // eslint-disable-line @typescript-eslint/unbound-method
164+ }
144165
145- injectReportDialog ( options ) ;
166+ ( global . document . head || global . document . body ) . appendChild ( script ) ;
146167}
147168
148169/**
0 commit comments