Skip to content

Commit 503e81d

Browse files
committed
🐛 fix(settings-webview): resolve webview loading and routing issues
- correct vscode api initialization for webview context - switch to MemoryRouter to fix routing issues within the webview - ensure image base URI is correctly passed to the webview
1 parent 70374e5 commit 503e81d

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

settings-webview/src/App.tsx

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
11
import { useState, useEffect } from "react"
2-
import { BrowserRouter as Router, Routes, Route } from "react-router-dom"
2+
import { MemoryRouter as Router, Routes, Route } from "react-router-dom"
33
import { FluentProvider, webLightTheme, webDarkTheme } from "@fluentui/react-components"
44
import SettingsView from "./components/SettingsView"
55
import "./App.css"
66

77
// Define the VS Code API
88
declare global {
99
interface Window {
10-
acquireVsCodeApi?: () => {
11-
postMessage: (message: any) => void
12-
getState: () => any
13-
setState: (state: any) => void
10+
vscode?: {
11+
postMessage: (message: unknown) => void
12+
getState: () => unknown
13+
setState: (state: unknown) => void
1414
}
1515
}
1616
}
1717

18-
// Initialize VS Code API
19-
const vscode = (() => {
20-
if (window.acquireVsCodeApi) {
21-
return window.acquireVsCodeApi()
22-
}
23-
24-
// Fallback for when running outside of VS Code (e.g., in a browser for development)
25-
return {
26-
postMessage: (message: any) => console.log("VS Code message:", message),
27-
getState: () => ({}),
28-
setState: () => {},
29-
}
30-
})()
18+
// Use the global vscode object that was set in the HTML
19+
const vscode = window.vscode || {
20+
postMessage: (message: unknown) => console.log("VS Code message:", message),
21+
getState: () => ({}),
22+
setState: () => {},
23+
}
3124

3225
// Define the message type for communication with the extension
3326
interface VSCodeMessage {
@@ -73,7 +66,7 @@ function App() {
7366

7467
return (
7568
<FluentProvider theme={theme}>
76-
<Router>
69+
<Router initialEntries={["/"]} initialIndex={0}>
7770
<div className="app-container">
7871
{!initialized ? (
7972
<div style={{ padding: "20px" }}>Loading settings...</div>

src/core/webview/SettingsWebviewProvider.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,15 @@ export class SettingsWebviewProvider {
162162

163163
// Get URIs for resources
164164
const scriptUri = getUri(webview, this.context.extensionUri, ["settings-webview", "dist", "assets", "index.js"])
165+
this.log(`scriptUri: ${scriptUri}`)
165166

166167
const stylesUri = getUri(webview, this.context.extensionUri, [
167168
"settings-webview",
168169
"dist",
169170
"assets",
170171
"index.css",
171172
])
173+
this.log(`stylesUri: ${stylesUri}`)
172174

173175
// Get codicons URI
174176
const codiconsUri = getUri(webview, this.context.extensionUri, [
@@ -178,9 +180,11 @@ export class SettingsWebviewProvider {
178180
"dist",
179181
"codicon.css",
180182
])
183+
this.log(`codiconsUri: ${codiconsUri}`)
181184

182185
// Get images URI
183186
const imagesUri = getUri(webview, this.context.extensionUri, ["assets", "images"])
187+
this.log(`imagesUri: ${imagesUri}`)
184188

185189
return /*html*/ `
186190
<!DOCTYPE html>
@@ -193,7 +197,9 @@ export class SettingsWebviewProvider {
193197
<link rel="stylesheet" type="text/css" href="${stylesUri}">
194198
<link href="${codiconsUri}" rel="stylesheet" />
195199
<script nonce="${nonce}">
196-
window.IMAGES_BASE_URI = "${imagesUri}"
200+
window.IMAGES_BASE_URI = "${imagesUri}";
201+
// Make sure vscode is defined for the webview
202+
window.vscode = acquireVsCodeApi();
197203
</script>
198204
<title>Roo Code Settings</title>
199205
</head>

0 commit comments

Comments
 (0)