aminghadersohi commented on code in PR #36933: URL: https://github.com/apache/superset/pull/36933#discussion_r2795361973
########## superset-frontend/src/embeddedChart/index.tsx: ########## @@ -0,0 +1,338 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import 'src/public-path'; + +import { useState, useEffect } from 'react'; +import ReactDOM from 'react-dom'; +import { + makeApi, + t, + logging, + QueryFormData, + StatefulChart, +} from '@superset-ui/core'; +import Switchboard from '@superset-ui/switchboard'; +import getBootstrapData, { applicationRoot } from 'src/utils/getBootstrapData'; +import setupClient from 'src/setup/setupClient'; +import setupPlugins from 'src/setup/setupPlugins'; +import { store, USER_LOADED } from 'src/views/store'; +import { Loading } from '@superset-ui/core/components'; +import { ErrorBoundary, DynamicPluginProvider } from 'src/components'; +import { addDangerToast } from 'src/components/MessageToasts/actions'; +import ToastContainer from 'src/components/MessageToasts/ToastContainer'; +import { UserWithPermissionsAndRoles } from 'src/types/bootstrapTypes'; +import { SupersetThemeProvider } from 'src/theme/ThemeProvider'; +import { ThemeController } from 'src/theme/ThemeController'; +import type { ThemeStorage } from '@apache-superset/core/ui'; +import { Provider as ReduxProvider } from 'react-redux'; + +setupPlugins(); + +const debugMode = process.env.WEBPACK_MODE === 'development'; +const bootstrapData = getBootstrapData(); + +function log(...info: unknown[]) { + if (debugMode) logging.debug(`[superset-embedded-chart]`, ...info); +} + +/** + * In-memory implementation of ThemeStorage interface for embedded contexts. + */ +class ThemeMemoryStorageAdapter implements ThemeStorage { + private storage = new Map<string, string>(); + + getItem(key: string): string | null { + return this.storage.get(key) || null; + } + + setItem(key: string, value: string): void { + this.storage.set(key, value); + } + + removeItem(key: string): void { + this.storage.delete(key); + } +} + +const themeController = new ThemeController({ + storage: new ThemeMemoryStorageAdapter(), +}); + +interface PermalinkState { + formData: QueryFormData; +} + +const appMountPoint = document.getElementById('app')!; +const MESSAGE_TYPE = '__embedded_comms__'; + +function showFailureMessage(message: string) { + appMountPoint.innerHTML = `<div style="display: flex; align-items: center; justify-content: center; height: 100vh; font-family: sans-serif;">${message}</div>`; +} + +if (!window.parent || window.parent === window) { + showFailureMessage( + t( + 'This page is intended to be embedded in an iframe, but it looks like that is not the case.', + ), + ); +} + +let displayedUnauthorizedToast = false; + +/** + * Handle unauthorized errors from the API. + */ +function guestUnauthorizedHandler() { + if (displayedUnauthorizedToast) return; + displayedUnauthorizedToast = true; + store.dispatch( + addDangerToast( + t( + 'This session has encountered an interruption. The embedded chart may not load correctly.', + ), + { + duration: -1, + noDuplicate: true, + }, + ), + ); +} + +/** + * Configures SupersetClient with the correct settings for the embedded chart page. + */ +function setupGuestClient(guestToken: string) { + setupClient({ + appRoot: applicationRoot(), + guestToken, + guestTokenHeaderName: bootstrapData.config?.GUEST_TOKEN_HEADER_NAME, + unauthorizedHandler: guestUnauthorizedHandler, + }); +} + +function validateMessageEvent(event: MessageEvent) { + const { data } = event; + if (data == null || typeof data !== 'object' || data.type !== MESSAGE_TYPE) { Review Comment: Fixed in commit c7f47d7 - relaxed validation to accept messages with `guestToken` or `handshake` fields in addition to typed messages. This allows legitimate initialization messages (both direct guest token and Switchboard-based handshake) to pass through correctly. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
