@@ -7,7 +7,7 @@ import * as vscode from 'vscode'
77import { ToolkitError } from '../shared/errors'
88import globals from '../shared/extensionGlobals'
99import { globalKey } from '../shared/globalState'
10- import { NotificationsState , NotificationType , NotificationData , ToolkitNotification } from './types'
10+ import { NotificationsState , NotificationsStateConstructor , NotificationType , ToolkitNotification } from './types'
1111import { HttpResourceFetcher } from '../shared/resourcefetcher/httpResourceFetcher'
1212import { getLogger } from '../shared/logger/logger'
1313import { NotificationsNode } from './panelNode'
@@ -16,6 +16,7 @@ import { RuleEngine } from './rules'
1616import { TreeNode } from '../shared/treeview/resourceTreeDataProvider'
1717import { withRetries } from '../shared/utilities/functionUtils'
1818import { FileResourceFetcher } from '../shared/resourcefetcher/fileResourceFetcher'
19+ import { isAmazonQ } from '../shared/extensionUtilities'
1920
2021const startUpEndpoint = 'https://idetoolkits-hostedfiles.amazonaws.com/Notifications/VSCode/startup/1.x.json'
2122const emergencyEndpoint = 'https://idetoolkits-hostedfiles.amazonaws.com/Notifications/VSCode/emergency/1.x.json'
@@ -43,27 +44,21 @@ export class NotificationsController {
4344
4445 /** Internal memory state that is written to global state upon modification. */
4546 private readonly state : NotificationsState
46- private readonly notificationsNode : NotificationsNode
4747
4848 static #instance: NotificationsController | undefined
4949
50- constructor ( extPrefix : 'amazonq' | 'toolkit' , node : NotificationsNode ) {
50+ constructor ( private readonly notificationsNode : NotificationsNode ) {
5151 if ( ! NotificationsController . #instance) {
52- registerDismissCommand ( extPrefix )
52+ registerDismissCommand ( )
5353 }
5454 NotificationsController . #instance = this
5555
56- this . storageKey = `aws.${ extPrefix } .notifications`
57- this . notificationsNode = node
58-
59- this . state = globals . globalState . get ( this . storageKey ) ?? {
60- startUp : { } as NotificationData ,
61- emergency : { } as NotificationData ,
56+ this . storageKey = 'aws.notifications'
57+ this . state = globals . globalState . tryGet < NotificationsState > ( this . storageKey , NotificationsStateConstructor , {
58+ startUp : { } ,
59+ emergency : { } ,
6260 dismissed : [ ] ,
63- }
64- this . state . startUp = this . state . startUp ?? { }
65- this . state . emergency = this . state . emergency ?? { }
66- this . state . dismissed = this . state . dismissed ?? [ ]
61+ } )
6762 }
6863
6964 public pollForStartUp ( ruleEngine : RuleEngine ) {
@@ -78,7 +73,7 @@ export class NotificationsController {
7873 try {
7974 await this . fetchNotifications ( category )
8075 } catch ( err : any ) {
81- getLogger ( ) . error ( `Unable to fetch %s notifications: %s` , category , err )
76+ getLogger ( 'notifications' ) . error ( `Unable to fetch %s notifications: %s` , category , err )
8277 }
8378
8479 await this . displayNotifications ( ruleEngine )
@@ -113,7 +108,7 @@ export class NotificationsController {
113108 * hide all notifications.
114109 */
115110 public async dismissNotification ( notificationId : string ) {
116- getLogger ( ) . debug ( 'Dismissing notification: %s' , notificationId )
111+ getLogger ( 'notifications' ) . debug ( 'Dismissing notification: %s' , notificationId )
117112 this . state . dismissed . push ( notificationId )
118113 await this . writeState ( )
119114
@@ -126,17 +121,17 @@ export class NotificationsController {
126121 private async fetchNotifications ( category : NotificationType ) {
127122 const response = _useLocalFiles ? await this . fetchLocally ( category ) : await this . fetchRemotely ( category )
128123 if ( ! response . content ) {
129- getLogger ( ) . verbose ( 'No new notifications for category: %s' , category )
124+ getLogger ( 'notifications' ) . verbose ( 'No new notifications for category: %s' , category )
130125 return
131126 }
132127
133- getLogger ( ) . verbose ( 'ETAG has changed for notifications category: %s' , category )
128+ getLogger ( 'notifications' ) . verbose ( 'ETAG has changed for notifications category: %s' , category )
134129
135130 this . state [ category ] . payload = JSON . parse ( response . content )
136131 this . state [ category ] . eTag = response . eTag
137132 await this . writeState ( )
138133
139- getLogger ( ) . verbose (
134+ getLogger ( 'notifications' ) . verbose (
140135 "Fetched notifications JSON for category '%s' with schema version: %s. There were %d notifications." ,
141136 category ,
142137 this . state [ category ] . payload ?. schemaVersion ,
@@ -169,7 +164,7 @@ export class NotificationsController {
169164 const uri = category === 'startUp' ? startUpLocalPath : emergencyLocalPath
170165 const content = await new FileResourceFetcher ( globals . context . asAbsolutePath ( uri ) ) . get ( )
171166
172- getLogger ( ) . verbose ( 'Fetched notifications locally for category: %s at path: %s' , category , uri )
167+ getLogger ( 'notifications' ) . verbose ( 'Fetched notifications locally for category: %s at path: %s' , category , uri )
173168 return {
174169 content,
175170 eTag : 'LOCAL_PATH' ,
@@ -180,7 +175,7 @@ export class NotificationsController {
180175 * Write the latest memory state to global state.
181176 */
182177 private async writeState ( ) {
183- getLogger ( ) . debug ( 'NotificationsController: Updating notifications state at %s' , this . storageKey )
178+ getLogger ( 'notifications' ) . debug ( 'NotificationsController: Updating notifications state at %s' , this . storageKey )
184179
185180 // Clean out anything in 'dismissed' that doesn't exist anymore.
186181 const notifications = new Set (
@@ -203,8 +198,8 @@ export class NotificationsController {
203198 }
204199}
205200
206- function registerDismissCommand ( extPrefix : string ) {
207- const name = ` _aws.${ extPrefix } .notifications.dismiss`
201+ function registerDismissCommand ( ) {
202+ const name = isAmazonQ ( ) ? ' _aws.amazonq .notifications.dismiss' : '_aws.toolkit.notifications.dismiss'
208203
209204 globals . context . subscriptions . push (
210205 Commands . register ( name , async ( node : TreeNode ) => {
@@ -216,7 +211,7 @@ function registerDismissCommand(extPrefix: string) {
216211
217212 await NotificationsController . instance . dismissNotification ( notification . id )
218213 } else {
219- getLogger ( ) . error ( `${ name } : Cannot dismiss notification: item is not a vscode.TreeItem` )
214+ getLogger ( 'notifications' ) . error ( `${ name } : Cannot dismiss notification: item is not a vscode.TreeItem` )
220215 }
221216 } )
222217 )
@@ -229,5 +224,6 @@ function registerDismissCommand(extPrefix: string) {
229224const _useLocalFiles = false
230225export const _useLocalFilesCheck = _useLocalFiles // export for testing
231226
227+ // Paths relative to current extension
232228const startUpLocalPath = '../core/src/test/notifications/resources/startup/1.x.json'
233229const emergencyLocalPath = '../core/src/test/notifications/resources/emergency/1.x.json'
0 commit comments