diff --git a/src/Common/MessageHandler.ts b/src/Common/MessageHandler.ts index d9bbaae8b..c225bc9d3 100644 --- a/src/Common/MessageHandler.ts +++ b/src/Common/MessageHandler.ts @@ -48,16 +48,31 @@ export function sendCachedDataMessage( export function sendMessage(data: any): void { if (canSendMessage()) { - window.parent.postMessage( - { - signature: "pcIframe", - data: data - }, - window.document.referrer - ); + const dataExplorerWindow = getDataExplorerWindow(); + if (dataExplorerWindow) { + dataExplorerWindow.parent.postMessage( + { + signature: "pcIframe", + data: data + }, + dataExplorerWindow.document.referrer + ); + } } } +const getDataExplorerWindow = (): Window => { + // Traverse up the window to find a window with `dataExplorerPlatform` property + let dataExplorerWindow: Window = window; + // TODO: Need to `any` here since the window imports Explorer which can't be in strict mode yet + // eslint-disable-next-line @typescript-eslint/no-explicit-any + while (!(dataExplorerWindow as any).dataExplorerPlatform && dataExplorerWindow.parent) { + dataExplorerWindow = dataExplorerWindow.parent; + } + + return dataExplorerWindow; +}; + export function canSendMessage(): boolean { return window.parent !== window; } diff --git a/src/Shared/Telemetry/TelemetryConstants.ts b/src/Shared/Telemetry/TelemetryConstants.ts index 53a03cd75..97a4c303f 100644 --- a/src/Shared/Telemetry/TelemetryConstants.ts +++ b/src/Shared/Telemetry/TelemetryConstants.ts @@ -70,7 +70,8 @@ export enum Action { NotebooksGitHubManualRepoAdd, NotebooksGitHubManageRepo, NotebooksGitHubCommit, - NotebooksGitHubDisconnect + NotebooksGitHubDisconnect, + OpenTerminal } export const ActionModifiers = { diff --git a/src/Terminal/index.ts b/src/Terminal/index.ts index 6826ad0ff..605d290f7 100644 --- a/src/Terminal/index.ts +++ b/src/Terminal/index.ts @@ -4,6 +4,8 @@ import "@jupyterlab/terminal/style/index.css"; import "./index.css"; import { ServerConnection } from "@jupyterlab/services"; import { JupyterLabAppFactory } from "./JupyterLabAppFactory"; +import { Action } from "../Shared/Telemetry/TelemetryConstants"; +import * as TelemetryProcessor from "../Shared/Telemetry/TelemetryProcessor"; const getUrlVars = (): { [key: string]: string } => { const vars: { [key: string]: string } = {}; @@ -14,10 +16,7 @@ const getUrlVars = (): { [key: string]: string } => { return vars; }; -const main = (): void => { - const urlVars = getUrlVars(); - console.log("URL parameters", urlVars); - +const createServerSettings = (urlVars: { [key: string]: string }): ServerConnection.ISettings => { let body: BodyInit; if (urlVars.hasOwnProperty("terminalEndpoint")) { body = JSON.stringify({ @@ -39,14 +38,29 @@ const main = (): void => { fetch: window.parent.fetch }; } - const serverSettings = ServerConnection.makeSettings(options); - if (urlVars.hasOwnProperty("terminal")) { - JupyterLabAppFactory.createTerminalApp(serverSettings); - return; + return ServerConnection.makeSettings(options); +}; + +const main = async (): Promise => { + const urlVars = getUrlVars(); + const serverSettings = createServerSettings(urlVars); + + const startTime = TelemetryProcessor.traceStart(Action.OpenTerminal, { + baseUrl: serverSettings.baseUrl + }); + + try { + if (urlVars.hasOwnProperty("terminal")) { + await JupyterLabAppFactory.createTerminalApp(serverSettings); + } else { + throw new Error("Only terminal is supported"); + } + + TelemetryProcessor.traceSuccess(Action.OpenTerminal, startTime); + } catch (error) { + TelemetryProcessor.traceFailure(Action.OpenTerminal, startTime); } - - throw new Error("Only terminal is supported"); }; window.addEventListener("load", main);