@@ -103,107 +103,122 @@ export class QuickActionHandler {
103
103
}
104
104
}
105
105
106
- private handleScanCommand ( tabID : string , eventId : string | undefined ) {
107
- if ( ! this . isScanEnabled ) {
106
+ /**
107
+ * Common helper method to handle specialized tab commands (scan, test, transform)
108
+ * @param options Configuration options for the specialized tab
109
+ */
110
+ private handleSpecializedTabCommand ( options : {
111
+ tabID : string // Current tab ID
112
+ eventId ?: string // Event ID for tracking
113
+ isEnabled : boolean // Feature flag
114
+ tabType : TabType // Type of tab to create/switch to
115
+ existingTabType : TabType // Type to look for in existing tabs
116
+ taskName ?: string // Optional task name
117
+ promptText ?: string // Optional prompt text
118
+ onExistingTab : ( tabId : string ) => void // Callback for existing tab
119
+ onNewTab : ( tabId : string ) => void // Callback for new tab
120
+ } ) : void {
121
+ if ( ! options . isEnabled ) {
108
122
return
109
123
}
110
- let scanTabId : string | undefined = undefined
111
124
125
+ // Check if a tab of this type already exists
126
+ let existingTabId : string | undefined = undefined
112
127
for ( const tab of this . tabsStorage . getTabs ( ) ) {
113
- if ( tab . type === 'review' ) {
114
- scanTabId = tab . id
128
+ if ( tab . type === options . existingTabType ) {
129
+ existingTabId = tab . id
130
+ break
115
131
}
116
132
}
117
133
118
- if ( scanTabId !== undefined ) {
119
- this . mynahUI . selectTab ( scanTabId , eventId || '' )
120
- this . connector . onTabChange ( scanTabId )
121
- this . connector . scans ( scanTabId )
134
+ // If tab exists, select it and run the callback
135
+ if ( existingTabId !== undefined ) {
136
+ this . mynahUI . selectTab ( existingTabId , options . eventId || '' )
137
+ this . connector . onTabChange ( existingTabId )
138
+ options . onExistingTab ( existingTabId )
122
139
return
123
140
}
124
141
125
- let affectedTabId : string | undefined = tabID
126
- // if there is no scan tab, open a new one
142
+ // Otherwise, create a new tab
143
+ let affectedTabId : string | undefined = options . tabID
144
+
145
+ // If current tab is not unknown or welcome, create a new tab
127
146
const currentTabType = this . tabsStorage . getTab ( affectedTabId ) ?. type
128
147
if ( currentTabType !== 'unknown' && currentTabType !== 'welcome' ) {
129
148
affectedTabId = this . mynahUI . updateStore ( '' , {
130
149
loadingChat : true ,
131
150
} )
132
151
}
133
152
153
+ // Handle case where we can't create a new tab
134
154
if ( affectedTabId === undefined ) {
135
155
this . mynahUI . notify ( {
136
156
content : uiComponentsTexts . noMoreTabsTooltip ,
137
157
type : NotificationType . WARNING ,
138
158
} )
139
159
return
140
- } else {
141
- this . tabsStorage . updateTabTypeFromUnknown ( affectedTabId , 'review' )
142
- this . connector . onKnownTabOpen ( affectedTabId )
143
- this . connector . onUpdateTabType ( affectedTabId )
144
-
145
- // reset chat history
146
- this . mynahUI . updateStore ( affectedTabId , {
147
- chatItems : [ ] ,
148
- } )
149
-
150
- this . mynahUI . updateStore ( affectedTabId , this . tabDataGenerator . getTabData ( 'review' , true , undefined ) ) // creating a new tab and printing some title
151
-
152
- // disable chat prompt
153
- this . mynahUI . updateStore ( affectedTabId , {
154
- loadingChat : true ,
155
- } )
156
- this . connector . scans ( affectedTabId )
157
160
}
158
- }
159
161
160
- private handleTestCommand ( chatPrompt : ChatPrompt , tabID : string , eventId : string | undefined ) {
161
- if ( ! this . isTestEnabled ) {
162
- return
163
- }
164
- const testTabId = this . tabsStorage . getTabs ( ) . find ( ( tab ) => tab . type === 'testgen' ) ?. id
165
- const realPromptText = chatPrompt . escapedPrompt ?. trim ( ) ?? ''
162
+ // Set up the new tab
163
+ this . tabsStorage . updateTabTypeFromUnknown ( affectedTabId , options . tabType )
164
+ this . connector . onKnownTabOpen ( affectedTabId )
165
+ this . connector . onUpdateTabType ( affectedTabId )
166
166
167
- if ( testTabId !== undefined ) {
168
- this . mynahUI . selectTab ( testTabId , eventId || '' )
169
- this . connector . onTabChange ( testTabId )
170
- this . connector . startTestGen ( testTabId , realPromptText )
171
- return
172
- }
167
+ // Reset chat history
168
+ this . mynahUI . updateStore ( affectedTabId , {
169
+ chatItems : [ ] ,
170
+ } )
173
171
174
- let affectedTabId : string | undefined = tabID
175
- // if there is no test tab, open a new one
176
- const currentTabType = this . tabsStorage . getTab ( affectedTabId ) ?. type
177
- if ( currentTabType !== 'unknown' && currentTabType !== 'welcome' ) {
178
- affectedTabId = this . mynahUI . updateStore ( '' , {
179
- loadingChat : true ,
180
- } )
181
- }
172
+ // Set tab data
173
+ const isEmpty = options . promptText === undefined || options . promptText === ''
174
+ this . mynahUI . updateStore (
175
+ affectedTabId ,
176
+ this . tabDataGenerator . getTabData ( options . tabType , isEmpty , options . taskName )
177
+ )
182
178
183
- if ( affectedTabId === undefined ) {
184
- this . mynahUI . notify ( {
185
- content : uiComponentsTexts . noMoreTabsTooltip ,
186
- type : NotificationType . WARNING ,
187
- } )
188
- return
189
- } else {
190
- this . tabsStorage . updateTabTypeFromUnknown ( affectedTabId , 'testgen' )
191
- this . connector . onKnownTabOpen ( affectedTabId )
192
- this . connector . onUpdateTabType ( affectedTabId )
179
+ // Disable chat prompt while loading
180
+ this . mynahUI . updateStore ( affectedTabId , {
181
+ loadingChat : true ,
182
+ } )
193
183
194
- // reset chat history
195
- this . mynahUI . updateStore ( affectedTabId , {
196
- chatItems : [ ] ,
197
- } )
184
+ // Run the callback for the new tab
185
+ options . onNewTab ( affectedTabId )
186
+ }
198
187
199
- // creating a new tab and printing some title
200
- this . mynahUI . updateStore (
201
- affectedTabId ,
202
- this . tabDataGenerator . getTabData ( 'testgen' , realPromptText === '' , 'Q - Test' )
203
- )
188
+ private handleScanCommand ( tabID : string , eventId : string | undefined ) {
189
+ this . handleSpecializedTabCommand ( {
190
+ tabID,
191
+ eventId,
192
+ isEnabled : this . isScanEnabled ,
193
+ tabType : 'review' ,
194
+ existingTabType : 'review' ,
195
+ onExistingTab : ( tabId ) => {
196
+ this . connector . scans ( tabId )
197
+ } ,
198
+ onNewTab : ( tabId ) => {
199
+ this . connector . scans ( tabId )
200
+ } ,
201
+ } )
202
+ }
204
203
205
- this . connector . startTestGen ( affectedTabId , realPromptText )
206
- }
204
+ private handleTestCommand ( chatPrompt : ChatPrompt , tabID : string , eventId : string | undefined ) {
205
+ const realPromptText = chatPrompt . escapedPrompt ?. trim ( ) ?? ''
206
+
207
+ this . handleSpecializedTabCommand ( {
208
+ tabID,
209
+ eventId,
210
+ isEnabled : this . isTestEnabled ,
211
+ tabType : 'testgen' ,
212
+ existingTabType : 'testgen' ,
213
+ taskName : 'Q - Test' ,
214
+ promptText : realPromptText ,
215
+ onExistingTab : ( tabId ) => {
216
+ this . connector . startTestGen ( tabId , realPromptText )
217
+ } ,
218
+ onNewTab : ( tabId ) => {
219
+ this . connector . startTestGen ( tabId , realPromptText )
220
+ } ,
221
+ } )
207
222
}
208
223
209
224
private handleCommand ( props : HandleCommandProps ) {
@@ -272,7 +287,6 @@ export class QuickActionHandler {
272
287
273
288
this . mynahUI . updateStore ( affectedTabId , {
274
289
loadingChat : true ,
275
- cancelButtonWhenLoading : false ,
276
290
promptInputDisabledState : true ,
277
291
} )
278
292
@@ -286,60 +300,19 @@ export class QuickActionHandler {
286
300
}
287
301
288
302
private handleGumbyCommand ( tabID : string , eventId : string | undefined ) {
289
- if ( ! this . isGumbyEnabled ) {
290
- return
291
- }
292
-
293
- let gumbyTabId : string | undefined = undefined
294
-
295
- for ( const tab of this . tabsStorage . getTabs ( ) ) {
296
- if ( tab . type === 'gumby' ) {
297
- gumbyTabId = tab . id
298
- }
299
- }
300
-
301
- if ( gumbyTabId !== undefined ) {
302
- this . mynahUI . selectTab ( gumbyTabId , eventId || '' )
303
- this . connector . onTabChange ( gumbyTabId )
304
- return
305
- }
306
-
307
- let affectedTabId : string | undefined = tabID
308
- // if there is no gumby tab, open a new one
309
- const currentTabType = this . tabsStorage . getTab ( affectedTabId ) ?. type
310
- if ( currentTabType !== 'unknown' && currentTabType !== 'welcome' ) {
311
- affectedTabId = this . mynahUI . updateStore ( '' , {
312
- loadingChat : true ,
313
- cancelButtonWhenLoading : false ,
314
- } )
315
- }
316
-
317
- if ( affectedTabId === undefined ) {
318
- this . mynahUI . notify ( {
319
- content : uiComponentsTexts . noMoreTabsTooltip ,
320
- type : NotificationType . WARNING ,
321
- } )
322
- return
323
- } else {
324
- this . tabsStorage . updateTabTypeFromUnknown ( affectedTabId , 'gumby' )
325
- this . connector . onKnownTabOpen ( affectedTabId )
326
- this . connector . onUpdateTabType ( affectedTabId )
327
-
328
- // reset chat history
329
- this . mynahUI . updateStore ( affectedTabId , {
330
- chatItems : [ ] ,
331
- } )
332
-
333
- this . mynahUI . updateStore ( affectedTabId , this . tabDataGenerator . getTabData ( 'gumby' , true , undefined ) )
334
-
335
- // disable chat prompt
336
- this . mynahUI . updateStore ( affectedTabId , {
337
- loadingChat : true ,
338
- cancelButtonWhenLoading : false ,
339
- } )
340
-
341
- this . connector . transform ( affectedTabId )
342
- }
303
+ this . handleSpecializedTabCommand ( {
304
+ tabID,
305
+ eventId,
306
+ isEnabled : this . isGumbyEnabled ,
307
+ tabType : 'gumby' ,
308
+ existingTabType : 'gumby' ,
309
+ onExistingTab : ( ) => {
310
+ // Nothing special to do for existing transform tab
311
+ } ,
312
+ onNewTab : ( tabId ) => {
313
+ this . connector . transform ( tabId )
314
+ } ,
315
+ } )
343
316
}
344
317
345
318
private handleClearCommand ( tabID : string ) {
0 commit comments