|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import * as vscode from 'vscode' |
| 7 | +import { getLogger } from '../../shared/logger/logger' |
| 8 | +import request from '../../shared/request' |
| 9 | +import { ToolkitError } from '../../shared/errors' |
| 10 | +import { i18n } from '../../shared/i18n-helper' |
| 11 | +import { ComponentType } from '../workflowStudio/types' |
| 12 | +import { isLocalDev, localhost, cdn } from '../constants/webviewResources' |
| 13 | + |
| 14 | +/** |
| 15 | + * Provider for Execution Details panels. |
| 16 | + * |
| 17 | + * Execution Details displays information about state machine executions in a WebView panel. |
| 18 | + */ |
| 19 | +export class ExecutionDetailProvider { |
| 20 | + public static readonly viewType = 'stepfunctions.executionDetails' |
| 21 | + |
| 22 | + /** |
| 23 | + * Opens execution details in a WebView panel. |
| 24 | + * @param executionArn The ARN of the execution to display details for |
| 25 | + * @param params Optional parameters to customize the WebView panel |
| 26 | + */ |
| 27 | + public static async openExecutionDetails( |
| 28 | + executionArn: string, |
| 29 | + params?: vscode.WebviewPanelOptions & vscode.WebviewOptions |
| 30 | + ): Promise<void> { |
| 31 | + // Create and show the webview panel |
| 32 | + const panel = vscode.window.createWebviewPanel( |
| 33 | + ExecutionDetailProvider.viewType, |
| 34 | + `Execution: ${executionArn.split(':').pop() || executionArn}`, |
| 35 | + vscode.ViewColumn.Beside, |
| 36 | + { |
| 37 | + enableScripts: true, |
| 38 | + retainContextWhenHidden: true, |
| 39 | + ...params, |
| 40 | + } |
| 41 | + ) |
| 42 | + // Create the provider and initialize the panel |
| 43 | + const provider = new ExecutionDetailProvider() |
| 44 | + await provider.initializePanel(panel, executionArn) |
| 45 | + } |
| 46 | + |
| 47 | + protected webviewHtml: string |
| 48 | + protected readonly logger = getLogger() |
| 49 | + |
| 50 | + constructor() { |
| 51 | + this.webviewHtml = '' |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Fetches the webview HTML from the CDN or local server. |
| 56 | + * @private |
| 57 | + */ |
| 58 | + private async fetchWebviewHtml(): Promise<void> { |
| 59 | + const source = isLocalDev ? localhost : cdn |
| 60 | + const response = await request.fetch('GET', `${source}/index.html`).response |
| 61 | + this.webviewHtml = await response.text() |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Gets the webview content for Execution Details. |
| 66 | + * @private |
| 67 | + */ |
| 68 | + private getWebviewContent = async (): Promise<string> => { |
| 69 | + const htmlFileSplit = this.webviewHtml.split('<head>') |
| 70 | + |
| 71 | + // Set asset source to CDN |
| 72 | + const source = isLocalDev ? localhost : cdn |
| 73 | + const baseTag = `<base href='${source}'/>` |
| 74 | + |
| 75 | + // Set locale, dark mode |
| 76 | + const locale = vscode.env.language |
| 77 | + const localeTag = `<meta name='locale' content='${locale}'>` |
| 78 | + const theme = vscode.window.activeColorTheme.kind |
| 79 | + const isDarkMode = theme === vscode.ColorThemeKind.Dark || theme === vscode.ColorThemeKind.HighContrast |
| 80 | + const darkModeTag = `<meta name='dark-mode' content='${isDarkMode}'>` |
| 81 | + |
| 82 | + // Set component type to ExecutionDetails |
| 83 | + const componentTypeTag = `<meta name="component-type" content="${ComponentType.ExecutionDetails}" />` |
| 84 | + |
| 85 | + return `${htmlFileSplit[0]} <head> ${baseTag} ${localeTag} ${darkModeTag} ${componentTypeTag} ${htmlFileSplit[1]}` |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Initializes a WebView panel with execution details. |
| 90 | + * @param panel The WebView panel to initialize |
| 91 | + * @param executionArn The ARN of the execution to display |
| 92 | + */ |
| 93 | + public async initializePanel(panel: vscode.WebviewPanel, executionArn: string): Promise<void> { |
| 94 | + try { |
| 95 | + if (!this.webviewHtml) { |
| 96 | + await this.fetchWebviewHtml() |
| 97 | + } |
| 98 | + |
| 99 | + // Set up the content |
| 100 | + panel.webview.html = await this.getWebviewContent() |
| 101 | + |
| 102 | + // Handle messages from the webview |
| 103 | + panel.webview.onDidReceiveMessage(async (message) => { |
| 104 | + this.logger.debug('Received message from execution details webview: %O', message) |
| 105 | + // Add message handlers as needed |
| 106 | + }) |
| 107 | + } catch (err) { |
| 108 | + void vscode.window.showErrorMessage(i18n('AWS.stepFunctions.executionDetails.failed')) |
| 109 | + throw ToolkitError.chain(err, 'Could not open Execution Details', { code: 'OpenExecutionDetailsFailed' }) |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments