1
1
import { ClientLike } from '@sentry/types' ;
2
2
import { captureException , getCarrier , getCurrentClient } from '@sentry/minimal' ;
3
3
import { addInstrumentationHandler , getGlobalObject , logger } from '@sentry/utils' ;
4
- import { ReportDialogOptions } from '@sentry/transport-base' ;
4
+ import { Dsn , getReportDialogEndpoint , ReportDialogOptions } from '@sentry/transport-base' ;
5
5
import { InboundFilters } from '@sentry/integration-common-inboundfilters' ;
6
6
import { UserAgent } from '@sentry/integration-browser-useragent' ;
7
7
import { EventTargetWrap , TimersWrap , XHRWrap } from '@sentry/integration-browser-wrap' ;
@@ -16,7 +16,6 @@ import { LinkedErrors } from '@sentry/integration-browser-linkederrors';
16
16
import { OnError , OnUnhandledRejection } from '@sentry/integration-browser-globalhandlers' ;
17
17
18
18
import { BrowserClient , BrowserOptions } from './client' ;
19
- import { injectReportDialog } from './helpers' ;
20
19
21
20
export const defaultIntegrations = [
22
21
new EventTargetWrap ( ) ,
@@ -121,28 +120,50 @@ export function init(options: BrowserOptions = {}): void {
121
120
*
122
121
* @param options Everything is optional, we try to fetch all info need from the global scope.
123
122
*/
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
+
125
129
// 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` ) ;
128
150
return ;
129
151
}
130
152
131
- const usableClient = client ?? getCurrentClient ( ) ;
132
- if ( ! usableClient ) {
153
+ if ( ! options . dsn ) {
154
+ logger . error ( ` ${ errPrefix } missing DSN` ) ;
133
155
return ;
134
156
}
135
157
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 ) ) ;
138
161
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
+ }
144
165
145
- injectReportDialog ( options ) ;
166
+ ( global . document . head || global . document . body ) . appendChild ( script ) ;
146
167
}
147
168
148
169
/**
0 commit comments