Skip to content

Commit 36f084a

Browse files
committed
Add webview
1 parent a068eac commit 36f084a

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

src/extension.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { registerMcpServer } from "./registerMcpServer";
66
import { SettingsManager } from "./SettingsManager";
77
import type { PackageJSON } from "./types";
88
import { attachIncidentFileToChatContext } from "./uris/handlers/context";
9+
import { openWebviewWithUrl } from "./uris/handlers/webview";
910
import { UriRouter } from "./uris/UriRouter";
1011

1112
const START_PORT = 33100;
@@ -14,6 +15,12 @@ const END_PORT = 33199;
1415
let digmaClient: DigmaApiClient | null = null;
1516
// let mcpServerDisposable: vscode.Disposable | null = null;
1617

18+
const getBaseDomain = (url: string): string => {
19+
const hostname = new URL(url).hostname;
20+
const parts = hostname.split(".");
21+
return parts.slice(-2).join(".");
22+
};
23+
1724
const findAvailablePort = async (
1825
start: number,
1926
end: number
@@ -63,6 +70,9 @@ async function initializeClient(
6370
export async function activate(context: vscode.ExtensionContext) {
6471
const uriRouter = new UriRouter();
6572

73+
const settingsManager = new SettingsManager(context);
74+
await initializeClient(settingsManager);
75+
6676
uriRouter.route(
6777
"/chat/context/add/file/incident/:incidentId",
6878
async (params) => {
@@ -73,8 +83,25 @@ export async function activate(context: vscode.ExtensionContext) {
7383
}
7484
);
7585

76-
const settingsManager = new SettingsManager(context);
77-
await initializeClient(settingsManager);
86+
uriRouter.route("/webview/open", async (_, query) => {
87+
const url = query?.url;
88+
89+
const settingsUrl = await settingsManager.getSetting<string>("url");
90+
if (settingsUrl && url) {
91+
const settingsBaseDomain = getBaseDomain(settingsUrl);
92+
const urlBaseDomain = getBaseDomain(url);
93+
94+
if (settingsBaseDomain === urlBaseDomain) {
95+
openWebviewWithUrl(url);
96+
} else {
97+
vscode.window.showErrorMessage(
98+
"URL must be from the same base domain as configured"
99+
);
100+
}
101+
} else {
102+
vscode.window.showErrorMessage("URL parameter is required for webview");
103+
}
104+
});
78105

79106
// Listen for configuration changes
80107
const configChangeListener = vscode.workspace.onDidChangeConfiguration(

src/uris/handlers/webview.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import vscode from "vscode";
2+
3+
export const openWebviewWithUrl = (url: string) => {
4+
try {
5+
const panel = vscode.window.createWebviewPanel(
6+
"editorWebview",
7+
"Digma",
8+
vscode.ViewColumn.One,
9+
{
10+
enableScripts: true,
11+
retainContextWhenHidden: true
12+
}
13+
);
14+
15+
panel.webview.html = getWebviewContent(url);
16+
} catch {
17+
vscode.window.showErrorMessage(`Invalid URL provided: ${url}`);
18+
}
19+
};
20+
21+
function getWebviewContent(url: string): string {
22+
return `<!DOCTYPE html>
23+
<html lang="en">
24+
<head>
25+
<meta charset="UTF-8">
26+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
27+
<title>Digma Webview</title>
28+
<style>
29+
body, html {
30+
margin: 0;
31+
padding: 0;
32+
height: 100vh;
33+
overflow: hidden;
34+
}
35+
iframe {
36+
width: 100%;
37+
height: 100vh;
38+
border: none;
39+
}
40+
.error {
41+
padding: 20px;
42+
background: #2d2d2d;
43+
}
44+
</style>
45+
</head>
46+
<body>
47+
<iframe src="${url}"
48+
onload="this.style.display='block'"
49+
onerror="document.body.innerHTML='<div class=\\"error\\">Failed to load: ${url}</div>'">
50+
</iframe>
51+
</body>
52+
</html>`;
53+
}

0 commit comments

Comments
 (0)