Skip to content

Commit 97c25cb

Browse files
celestial-vaultElephant Lumps
andauthored
Pass id to webview on creation (RooCodeInc#3867)
* pass type of webview to webview * changeset --------- Co-authored-by: Elephant Lumps <[email protected]>
1 parent 0fb82c1 commit 97c25cb

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

.changeset/short-icons-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"claude-dev": minor
3+
---
4+
5+
Pass type of webview (tab or sidebar) to webview so it knows what type it is

src/core/webview/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Controller } from "@core/controller/index"
77
import { findLast } from "@shared/array"
88
import { readFile } from "fs/promises"
99
import path from "node:path"
10+
import { WebviewProviderType } from "@/shared/webview/types"
1011

1112
/*
1213
https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts
@@ -24,6 +25,7 @@ export class WebviewProvider implements vscode.WebviewViewProvider {
2425
constructor(
2526
readonly context: vscode.ExtensionContext,
2627
private readonly outputChannel: vscode.OutputChannel,
28+
private readonly providerType: WebviewProviderType = WebviewProviderType.TAB, // Default to tab provider
2729
) {
2830
WebviewProvider.activeInstances.add(this)
2931
this.controller = new Controller(context, outputChannel, (message) => this.view?.webview.postMessage(message))
@@ -239,6 +241,10 @@ export class WebviewProvider implements vscode.WebviewViewProvider {
239241
<body>
240242
<noscript>You need to enable JavaScript to run this app.</noscript>
241243
<div id="root"></div>
244+
<script type="text/javascript" nonce="${nonce}">
245+
// Inject the provider type
246+
window.WEBVIEW_PROVIDER_TYPE = ${JSON.stringify(this.providerType)};
247+
</script>
242248
<script type="module" nonce="${nonce}" src="${scriptUri}"></script>
243249
</body>
244250
</html>
@@ -348,6 +354,10 @@ export class WebviewProvider implements vscode.WebviewViewProvider {
348354
</head>
349355
<body>
350356
<div id="root"></div>
357+
<script type="text/javascript" nonce="${nonce}">
358+
// Inject the provider type
359+
window.WEBVIEW_PROVIDER_TYPE = ${JSON.stringify(this.providerType)};
360+
</script>
351361
${reactRefresh}
352362
<script type="module" src="${scriptUri}"></script>
353363
</body>

src/extension.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { ErrorService } from "./services/error/ErrorService"
1515
import { initializeTestMode, cleanupTestMode } from "./services/test/TestMode"
1616
import { telemetryService } from "./services/posthog/telemetry/TelemetryService"
1717
import { v4 as uuidv4 } from "uuid"
18-
18+
import { WebviewProviderType } from "./shared/webview/types"
1919
/*
2020
Built using https://github.com/microsoft/vscode-webview-ui-toolkit
2121
@@ -40,7 +40,7 @@ export async function activate(context: vscode.ExtensionContext) {
4040
// Version checking for autoupdate notification
4141
const currentVersion = context.extension.packageJSON.version
4242
const previousVersion = context.globalState.get<string>("clineVersion")
43-
const sidebarWebview = new WebviewProvider(context, outputChannel)
43+
const sidebarWebview = new WebviewProvider(context, outputChannel, WebviewProviderType.SIDEBAR)
4444

4545
// Initialize test mode and add disposables to context
4646
context.subscriptions.push(...initializeTestMode(context, sidebarWebview))
@@ -125,7 +125,7 @@ export async function activate(context: vscode.ExtensionContext) {
125125
Logger.log("Opening Cline in new tab")
126126
// (this example uses webviewProvider activation event which is necessary to deserialize cached webview, but since we use retainContextWhenHidden, we don't need to use that event)
127127
// https://github.com/microsoft/vscode-extension-samples/blob/main/webview-sample/src/extension.ts
128-
const tabWebview = new WebviewProvider(context, outputChannel)
128+
const tabWebview = new WebviewProvider(context, outputChannel, WebviewProviderType.TAB)
129129
//const column = vscode.window.activeTextEditor ? vscode.window.activeTextEditor.viewColumn : undefined
130130
const lastCol = Math.max(...vscode.window.visibleTextEditors.map((editor) => editor.viewColumn || 0))
131131

src/shared/webview/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export enum WebviewProviderType {
2+
SIDEBAR = "sidebar",
3+
TAB = "tab",
4+
}
5+
6+
declare global {
7+
interface Window {
8+
WEBVIEW_PROVIDER_TYPE?: WebviewProviderType
9+
}
10+
}

webview-ui/src/App.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { UiServiceClient } from "./services/grpc-client"
99
import McpView from "./components/mcp/configuration/McpConfigurationView"
1010
import { Providers } from "./Providers"
1111
import { Boolean, EmptyRequest } from "@shared/proto/common"
12+
import { WebviewProviderType } from "@shared/webview/types"
1213

1314
const AppContent = () => {
1415
const {
@@ -46,6 +47,11 @@ const AppContent = () => {
4647
}
4748
}, [shouldShowAnnouncement])
4849

50+
useEffect(() => {
51+
const providerType = window.WEBVIEW_PROVIDER_TYPE || WebviewProviderType.TAB
52+
console.log("[DEBUG] webviewProviderType", providerType)
53+
}, [])
54+
4955
if (!didHydrateState) {
5056
return null
5157
}

0 commit comments

Comments
 (0)