Skip to content

Commit 98d8324

Browse files
authored
feat(notification): Use virtual txt file and telemetry url wrapper (#5939)
Created a `ReadonlyDocument` class in `textDocumentUtilities` to show a virtual read-only txt file. Converted existing txt notifications. Use telemetry url wrapper instead of `openExternal` Also updated some local notification JSON for future testing
1 parent 5c37d1d commit 98d8324

File tree

4 files changed

+100
-98
lines changed

4 files changed

+100
-98
lines changed

packages/core/src/notifications/panelNode.ts

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ import { NotificationType, ToolkitNotification } from './types'
1212
import { ToolkitError } from '../shared/errors'
1313
import { isAmazonQ } from '../shared/extensionUtilities'
1414
import { getLogger } from '../shared/logger/logger'
15-
import { tempDirPath } from '../shared/filesystemUtilities'
16-
import path from 'path'
17-
import fs from '../shared/fs/fs'
1815
import { registerToolView } from '../awsexplorer/activationShared'
16+
import { readonlyDocument } from '../shared/utilities/textDocumentUtilities'
17+
import { openUrl } from '../shared/utilities/vsCodeUtils'
1918

2019
/**
2120
* Controls the "Notifications" side panel/tree in each extension. It takes purely UX actions
@@ -143,49 +142,19 @@ export class NotificationsNode implements TreeNode {
143142
}
144143
// Show open url option
145144
getLogger('notifications').verbose(`opening url for notification: ${notification.id} ...`)
146-
await vscode.env.openExternal(vscode.Uri.parse(notification.uiRenderInstructions.onClick.url))
145+
await openUrl(vscode.Uri.parse(notification.uiRenderInstructions.onClick.url))
147146
break
148147
case 'openTextDocument':
149148
// Display read-only txt document
150149
getLogger('notifications').verbose(`showing txt document for notification: ${notification.id} ...`)
151-
await this.showReadonlyTextDocument(notification.uiRenderInstructions.content['en-US'].description)
150+
await readonlyDocument.show(
151+
notification.uiRenderInstructions.content['en-US'].description,
152+
`Notification: ${notification.id}`
153+
)
152154
break
153155
}
154156
}
155157

156-
/**
157-
* Shows a read only txt file for the contect of notification on a side column
158-
* It's read-only so that the "save" option doesn't appear when user closes the notification
159-
*/
160-
private async showReadonlyTextDocument(content: string): Promise<void> {
161-
try {
162-
const tempFilePath = path.join(tempDirPath, 'AWSToolkitNotifications.txt')
163-
164-
if (await fs.existsFile(tempFilePath)) {
165-
// If file exist, make sure it has write permission (0o644)
166-
await fs.chmod(tempFilePath, 0o644)
167-
}
168-
169-
await fs.writeFile(tempFilePath, content)
170-
171-
// Set the file permissions to read-only (0o444)
172-
await fs.chmod(tempFilePath, 0o444)
173-
174-
// Now, open the document
175-
const document = await vscode.workspace.openTextDocument(tempFilePath)
176-
177-
const options: vscode.TextDocumentShowOptions = {
178-
viewColumn: vscode.ViewColumn.Beside,
179-
preserveFocus: true,
180-
preview: true,
181-
}
182-
183-
await vscode.window.showTextDocument(document, options)
184-
} catch (error) {
185-
throw new ToolkitError(`Error showing text document: ${error}`)
186-
}
187-
}
188-
189158
/**
190159
* Renders information window with the notification's content and buttons.
191160
* Can be either a blocking modal or a bottom-right corner toast
@@ -217,16 +186,17 @@ export class NotificationsNode implements TreeNode {
217186
if (selectedButton) {
218187
switch (selectedButton.type) {
219188
case 'openTxt':
220-
await this.showReadonlyTextDocument(
221-
notification.uiRenderInstructions.content['en-US'].description
189+
await readonlyDocument.show(
190+
notification.uiRenderInstructions.content['en-US'].description,
191+
`Notification: ${notification.id}`
222192
)
223193
break
224194
case 'updateAndReload':
225195
await this.updateAndReload(notification.displayIf.extensionId)
226196
break
227197
case 'openUrl':
228198
if (selectedButton.url) {
229-
await vscode.env.openExternal(vscode.Uri.parse(selectedButton.url))
199+
await openUrl(vscode.Uri.parse(selectedButton.url))
230200
} else {
231201
throw new ToolkitError('url not provided')
232202
}

packages/core/src/shared/utilities/textDocumentUtilities.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,47 @@ export async function addEofNewline(editor: vscode.TextEditor) {
223223
})
224224
}
225225
}
226+
227+
class ReadonlyTextDocumentProvider implements vscode.TextDocumentContentProvider {
228+
private content = ''
229+
230+
setContent(content: string) {
231+
this.content = content
232+
}
233+
234+
provideTextDocumentContent(uri: vscode.Uri): string {
235+
return this.content
236+
}
237+
}
238+
239+
/**
240+
* Shows a read only virtual txt file on a side column
241+
* It's read-only so that the "save" option doesn't appear when user closes the pop up window
242+
* Usage: ReadonlyDocument.show(content, filename)
243+
* @param content The content to be displayed in the virtual document
244+
* @param filename The title on top of the pop up window
245+
*/
246+
class ReadonlyDocument {
247+
private readonly scheme = 'AWStoolkit-readonly'
248+
private readonly provider = new ReadonlyTextDocumentProvider()
249+
250+
constructor() {
251+
vscode.workspace.registerTextDocumentContentProvider(this.scheme, this.provider)
252+
}
253+
254+
public async show(content: string, filename: string) {
255+
this.provider.setContent(content)
256+
const uri = vscode.Uri.parse(`${this.scheme}:/${filename}.txt`)
257+
const options: vscode.TextDocumentShowOptions = {
258+
viewColumn: vscode.ViewColumn.Beside,
259+
preserveFocus: true,
260+
preview: true,
261+
}
262+
263+
// Open the document with the updated content
264+
const document = await vscode.workspace.openTextDocument(uri)
265+
await vscode.window.showTextDocument(document, options)
266+
}
267+
}
268+
269+
export const readonlyDocument = new ReadonlyDocument()

packages/core/src/test/notifications/resources/emergency/1.x.json

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,88 +2,48 @@
22
"schemaVersion": "1.x",
33
"notifications": [
44
{
5-
"id": "amazonq_emergency_local_1",
5+
"id": "emergency1",
66
"displayIf": {
77
"extensionId": "amazonwebservices.amazon-q-vscode"
88
},
99
"uiRenderInstructions": {
1010
"content": {
1111
"en-US": {
12-
"title": "[local] Emergency: url",
13-
"description": "Something crazy is happening! Please update your extension.",
14-
"toastPreview": "omg"
15-
}
16-
},
17-
"onRecieve": "toast",
18-
"onClick": {
19-
"type": "modal"
20-
},
21-
"actions": [
22-
{
23-
"type": "updateAndReload",
24-
"displayText": {
25-
"en-US": "Update and Reload"
26-
}
27-
},
28-
{
29-
"type": "openUrl",
30-
"url": "https://aws.amazon.com/visualstudiocode/",
31-
"displayText": {
32-
"en-US": "Proceed to wiki"
33-
}
34-
}
35-
]
36-
}
37-
},
38-
{
39-
"id": "amazonq_emergency_local_2",
40-
"displayIf": {
41-
"extensionId": "amazonwebservices.amazon-q-vscode"
42-
},
43-
"uiRenderInstructions": {
44-
"content": {
45-
"en-US": {
46-
"title": "[local] Emergency: txt",
47-
"description": "Something crazy is happening! Please update your extension.",
48-
"toastPreview": "omg"
12+
"title": "Can't sign in to Amazon Q",
13+
"description": "There is currently a bug that is preventing users from signing into Amazon Q. If this impacts you, please try this workaround:\n\n 1. Reload your IDE\n 2. Run the command in the command palette:: `Amazon Q: Reset State`.\n 3. Set your default region to `us-east-3`.\n 4. Try to sign into Amazon Q with your desired region in the dropdown.\n\nWe are currently working on releasing a fix so that this workaround is not required.\nPlease reach out on our github issues with any questions.",
14+
"toastPreview": "Signing into Amazon Q is broken, please try this workaround while we work on releasing a fix."
4915
}
5016
},
5117
"onRecieve": "toast",
5218
"onClick": {
5319
"type": "openTextDocument"
54-
}
20+
},
21+
"actions": []
5522
}
5623
},
5724
{
58-
"id": "amazonq_emergency_local_3",
25+
"id": "emergency2",
5926
"displayIf": {
6027
"extensionId": "amazonwebservices.amazon-q-vscode"
6128
},
6229
"uiRenderInstructions": {
6330
"content": {
6431
"en-US": {
65-
"title": "[local] Emergency: modal",
66-
"description": "Something crazy is happening! Please update your extension.",
67-
"toastPreview": "new notification: something crazy is happening! Please update your entension and look on our wiki."
32+
"title": "Update Amazon Q to avoid breaking bugs",
33+
"description": "There is currently a bug that prevents Amazon Q from responding to chat requests. It is fixed in the latest version. Please update your Amazon Q now.",
34+
"toastPreview": "This version of Amazon Q is currently broken, please update to avoid issues."
6835
}
6936
},
7037
"onRecieve": "toast",
7138
"onClick": {
72-
"type": "openTextDocument"
39+
"type": "modal"
7340
},
7441
"actions": [
7542
{
7643
"type": "updateAndReload",
7744
"displayText": {
7845
"en-US": "Update and Reload"
7946
}
80-
},
81-
{
82-
"type": "openUrl",
83-
"url": "https://aws.amazon.com/visualstudiocode/",
84-
"displayText": {
85-
"en-US": "Proceed to wiki"
86-
}
8747
}
8848
]
8949
}

packages/core/src/test/notifications/resources/startup/1.x.json

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,51 @@
22
"schemaVersion": "1.x",
33
"notifications": [
44
{
5-
"id": "amazonq_startUp_local_1",
5+
"id": "startup1",
66
"displayIf": {
77
"extensionId": "amazonwebservices.amazon-q-vscode"
88
},
99
"uiRenderInstructions": {
1010
"content": {
1111
"en-US": {
12-
"title": "[local] What's New, url",
13-
"description": "Check out this new stuff!",
14-
"toastPreview": "omg"
12+
"title": "New Amazon Q Chat features",
13+
"description": "You can now use Amazon Q inline in your IDE, without ever touching the mouse or using copy and paste. \nPress ⌘+I (Ctrl+I on Windows) to trigger inline chat. \nDescribe a function or feature you'd like to develop and Amazon Q will generate and display a code diff that inserts new code at the cursor position. \nPress Enter to accept and apply the diff, or Escape to reject it. \nAlternatively you select a block of code (maybe even the entire file) then press ⌘+I (Ctrl+I on Windows) to provide instructions on how to refactor the selected code. \nYou will see a diff against the selected code and can press Enter to accept and apply the diff.",
14+
"toastPreview": "New Amazon Q features available: inline chat"
15+
}
16+
},
17+
"onRecieve": "toast",
18+
"onClick": {
19+
"type": "openTextDocument"
20+
},
21+
"actions": [
22+
{
23+
"type": "openUrl",
24+
"url": "https://aws.amazon.com/developer/generative-ai/amazon-q/change-log/",
25+
"displayText": {
26+
"en-US": "Learn more"
27+
}
28+
}
29+
]
30+
}
31+
},
32+
{
33+
"id": "startup2",
34+
"displayIf": {
35+
"extensionId": "amazonwebservices.amazon-q-vscode"
36+
},
37+
"uiRenderInstructions": {
38+
"content": {
39+
"en-US": {
40+
"title": "What's New",
41+
"toastPreview": "New Amazon Q features are available!"
1542
}
1643
},
1744
"onRecieve": "toast",
1845
"onClick": {
1946
"type": "openUrl",
20-
"url": "https://aws.amazon.com/visualstudiocode/"
21-
}
47+
"url": "https://aws.amazon.com/developer/generative-ai/amazon-q/change-log/"
48+
},
49+
"actions": []
2250
}
2351
}
2452
]

0 commit comments

Comments
 (0)