Adrià Vilanova MartÃnez | f472d49 | 2024-03-05 21:26:53 +0100 | [diff] [blame] | 1 | import {getCurrentColorTheme, kColorThemeMode} from './darkTheme'; |
| 2 | |
| 3 | const kReportingWidgetThemes = { |
| 4 | [kColorThemeMode.Auto]: '0', |
| 5 | [kColorThemeMode.Light]: '1', |
| 6 | [kColorThemeMode.Dark]: '2', |
| 7 | }; |
| 8 | |
| 9 | export default class ReportDialogColorThemeFix { |
| 10 | constructor(options) { |
| 11 | this.optionsAtPageLoad = options; |
| 12 | } |
| 13 | |
| 14 | fixThemeIfReportDialogIframeAndApplicable(iframe) { |
| 15 | if (!this.isReportDialogIframe(iframe)) return; |
| 16 | |
| 17 | const currentColorTheme = getCurrentColorTheme(this.optionsAtPageLoad); |
| 18 | |
| 19 | // By default the report dialog is added with the light theme |
| 20 | if (currentColorTheme === kColorThemeMode.Light) return; |
| 21 | |
| 22 | console.debug('[reportDialogColorThemeFix] Fixing report dialog iframe'); |
| 23 | let url = new URL(iframe.src); |
| 24 | url.searchParams.set('theme', kReportingWidgetThemes[currentColorTheme]); |
| 25 | iframe.src = url.href; |
| 26 | } |
| 27 | |
| 28 | isReportDialogIframe(iframe) { |
| 29 | return iframe.src?.includes?.('reportingwidget') ?? false; |
| 30 | } |
| 31 | } |