Skip to content

Commit 2a9105d

Browse files
authored
feat: Add Analytics webview panel (#269)
1 parent 5037632 commit 2a9105d

File tree

4 files changed

+97
-6
lines changed

4 files changed

+97
-6
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
uses: actions/checkout@v2
1212

1313
- name: Cache dependencies
14-
uses: actions/cache@v2
14+
uses: actions/cache@v4
1515
with:
1616
path: ~/.npm
1717
key: npm-${{ hashFiles('package-lock.json') }}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
},
157157
{
158158
"command": "sourcery.hub.start",
159-
"title": "Manage Sourcery account",
159+
"title": "Open Sourcery Analytics",
160160
"category": "Sourcery"
161161
},
162162
{

src/chat.ts

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export class ChatProvider implements vscode.WebviewViewProvider {
6161
private _view?: vscode.WebviewView;
6262
private _extensionUri: vscode.Uri;
6363
private _assetsUri: vscode.Uri;
64+
private _panel?: vscode.WebviewPanel; // Track the current panel
6465

6566
public recipes: Recipe[] = []; // this data is used in the "Ask Sourcery" command prompt, so can't be removed
6667

@@ -131,6 +132,77 @@ export class ChatProvider implements vscode.WebviewViewProvider {
131132
);
132133
}
133134

135+
/**
136+
* Creates a new webview panel with chat functionality or reveals an existing one
137+
*/
138+
public async createOrShowWebviewPanel(): Promise<vscode.WebviewPanel> {
139+
if (this._panel) {
140+
// If panel already exists, reveal it
141+
this._panel.reveal();
142+
return this._panel;
143+
}
144+
145+
// Create a new panel
146+
const panel = vscode.window.createWebviewPanel(
147+
"sourceryChatPanel", // Unique identifier
148+
"Analytics", // Title
149+
vscode.ViewColumn.Active,
150+
{
151+
enableScripts: true,
152+
retainContextWhenHidden: true,
153+
localResourceRoots: [this._extensionUri, this._assetsUri],
154+
}
155+
);
156+
157+
// Set the panel's HTML content
158+
panel.webview.html = await this._getHtmlForWebview(panel.webview);
159+
160+
// Set up message handling
161+
panel.webview.onDidReceiveMessage(
162+
async ({ message, ...rest }: { message: OutboundMessage }) => {
163+
switch (message.target) {
164+
case "languageServer":
165+
vscode.commands.executeCommand("sourcery.coding_assistant", {
166+
message,
167+
...rest,
168+
});
169+
break;
170+
case "extension":
171+
switch (message.request) {
172+
case "openLink":
173+
this.handleOpenLinkRequest(message);
174+
break;
175+
case "copyToClipboard": {
176+
this.handleCopyToClipboardRequest(message);
177+
break;
178+
}
179+
case "insertAtCursor": {
180+
this.handleInsertAtCursorRequest(message);
181+
break;
182+
}
183+
case "updateConfiguration": {
184+
await vscode.workspace
185+
.getConfiguration()
186+
.update(
187+
message.section,
188+
message.value,
189+
message.configurationTarget
190+
);
191+
}
192+
}
193+
}
194+
}
195+
);
196+
197+
// Track this panel and handle disposal
198+
this._panel = panel;
199+
panel.onDidDispose(() => {
200+
this._panel = undefined;
201+
});
202+
203+
return panel;
204+
}
205+
134206
public postCommand(command: any) {
135207
// Intercept the addRecipes command
136208
// The "Ask Sourcery" user interface needs to have the recipes available
@@ -142,7 +214,22 @@ export class ChatProvider implements vscode.WebviewViewProvider {
142214
break;
143215
}
144216
}
145-
this._view.webview.postMessage(command);
217+
218+
// Send to the sidebar view if it exists
219+
if (this._view?.webview) {
220+
this._view.webview.postMessage(command);
221+
}
222+
223+
// Also send to the panel if it exists
224+
if (this._panel?.webview) {
225+
switch (command.command) {
226+
case "context/update": {
227+
command["updates"]["isAnalyticsPanel"] = true;
228+
break;
229+
}
230+
}
231+
this._panel.webview.postMessage(command);
232+
}
146233
}
147234

148235
private handleOpenLinkRequest({

src/extension.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ function showSourceryStatusBarItem(context: ExtensionContext) {
9999
// Create the status bar
100100
const myStatusBarItem = window.createStatusBarItem(StatusBarAlignment.Left);
101101
myStatusBarItem.command = "sourcery.hub.start";
102-
myStatusBarItem.text = "Sourcery";
103-
myStatusBarItem.tooltip = "Manage Sourcery account";
102+
myStatusBarItem.text = "Sourcery Analytics";
103+
myStatusBarItem.tooltip = "See analytics for your repo";
104104
context.subscriptions.push(myStatusBarItem);
105105
myStatusBarItem.show();
106106
}
@@ -477,10 +477,14 @@ function registerCommands(
477477
// This is activated from the status bar (see below)
478478
context.subscriptions.push(
479479
commands.registerCommand("sourcery.hub.start", async () => {
480+
// Instruct the language server to start the hub server
480481
languageClient.sendRequest(ExecuteCommandRequest.type, {
481-
command: "sourcery.openHub",
482+
command: "sourcery.startHub",
482483
arguments: [],
483484
});
485+
486+
// Create or show the chat panel
487+
await chatProvider.createOrShowWebviewPanel();
484488
})
485489
);
486490
}

0 commit comments

Comments
 (0)