1- import { getCurrentHub , getIntegrationsToSetup , initAndBind , Integrations as CoreIntegrations } from '@sentry/core' ;
2- import { Hub } from '@sentry/types' ;
1+ import {
2+ getCurrentHub ,
3+ getIntegrationsToSetup ,
4+ getReportDialogEndpoint ,
5+ Hub ,
6+ initAndBind ,
7+ Integrations as CoreIntegrations ,
8+ } from '@sentry/core' ;
39import {
410 addInstrumentationHandler ,
511 getGlobalObject ,
@@ -121,8 +127,19 @@ export function init(options: BrowserOptions = {}): void {
121127 *
122128 * @param options Everything is optional, we try to fetch all info need from the global scope.
123129 */
124- export function showReportDialog ( options : ReportDialogOptions = { } ) : void {
125- const hub = getCurrentHub ( ) ;
130+ export function showReportDialog ( options : ReportDialogOptions = { } , hub : Hub = getCurrentHub ( ) ) : void {
131+ // doesn't work without a document (React Native)
132+ const global = getGlobalObject < Window > ( ) ;
133+ if ( ! global . document ) {
134+ IS_DEBUG_BUILD && logger . error ( 'Global document not defined in showReportDialog call' ) ;
135+ return ;
136+ }
137+
138+ if ( ! options . dsn ) {
139+ IS_DEBUG_BUILD && logger . error ( 'Missing dsn option in showReportDialog call' ) ;
140+ return ;
141+ }
142+
126143 const scope = hub . getScope ( ) ;
127144 if ( scope ) {
128145 options . user = {
@@ -134,9 +151,21 @@ export function showReportDialog(options: ReportDialogOptions = {}): void {
134151 if ( ! options . eventId ) {
135152 options . eventId = hub . lastEventId ( ) ;
136153 }
137- const client = hub . getClient < BrowserClient > ( ) ;
138- if ( client ) {
139- client . showReportDialog ( options ) ;
154+
155+ const script = global . document . createElement ( 'script' ) ;
156+ script . async = true ;
157+ script . src = getReportDialogEndpoint ( options . dsn , options ) ;
158+
159+ if ( options . onLoad ) {
160+ // eslint-disable-next-line @typescript-eslint/unbound-method
161+ script . onload = options . onLoad ;
162+ }
163+
164+ const injectionPoint = global . document . head || global . document . body ;
165+ if ( injectionPoint ) {
166+ injectionPoint . appendChild ( script ) ;
167+ } else {
168+ IS_DEBUG_BUILD && logger . error ( 'Not injecting report dialog. No injection point found in HTML' ) ;
140169 }
141170}
142171
0 commit comments