(options: ReportDialogOptions = {})
| 9 | * @param options Everything is optional, we try to fetch all info need from the current scope. |
| 10 | */ |
| 11 | export function showReportDialog(options: ReportDialogOptions = {}): void { |
| 12 | const optionalDocument = WINDOW.document as Document | undefined; |
| 13 | const injectionPoint = optionalDocument?.head || optionalDocument?.body; |
| 14 | |
| 15 | // doesn't work without a document (React Native) |
| 16 | if (!injectionPoint) { |
| 17 | DEBUG_BUILD && debug.error('[showReportDialog] Global document not defined'); |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | const scope = getCurrentScope(); |
| 22 | const client = getClient(); |
| 23 | const dsn = client?.getDsn(); |
| 24 | |
| 25 | if (!dsn) { |
| 26 | DEBUG_BUILD && debug.error('[showReportDialog] DSN not configured'); |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | const mergedOptions = { |
| 31 | ...options, |
| 32 | user: { |
| 33 | ...scope.getUser(), |
| 34 | ...options.user, |
| 35 | }, |
| 36 | eventId: options.eventId || lastEventId(), |
| 37 | }; |
| 38 | |
| 39 | const script = WINDOW.document.createElement('script'); |
| 40 | script.async = true; |
| 41 | script.crossOrigin = 'anonymous'; |
| 42 | script.src = getReportDialogEndpoint(dsn, mergedOptions); |
| 43 | |
| 44 | const { onLoad, onClose } = mergedOptions; |
| 45 | |
| 46 | if (onLoad) { |
| 47 | script.onload = onLoad; |
| 48 | } |
| 49 | |
| 50 | if (onClose) { |
| 51 | const reportDialogClosedMessageHandler = (event: MessageEvent): void => { |
| 52 | if (event.data === '__sentry_reportdialog_closed__') { |
| 53 | try { |
| 54 | onClose(); |
| 55 | } finally { |
| 56 | WINDOW.removeEventListener('message', reportDialogClosedMessageHandler); |
| 57 | } |
| 58 | } |
| 59 | }; |
| 60 | WINDOW.addEventListener('message', reportDialogClosedMessageHandler); |
| 61 | } |
| 62 | |
| 63 | injectionPoint.appendChild(script); |
| 64 | } |
no test coverage detected