55import * as vscode from 'vscode'
66import globals from '../../extensionGlobals'
77import { isValidResponse , StepEstimator } from '../../wizards/wizard'
8- import { createQuickPick , ExtendedQuickPickOptions , ItemLoadTypes } from '../pickerPrompter'
8+ import { createQuickPick , DataQuickPickItem , ExtendedQuickPickOptions , ItemLoadTypes } from '../pickerPrompter'
99import { Prompter , PromptResult } from '../prompter'
1010import { createRegionPrompter } from './region'
1111import { QuickPickPrompter } from '../pickerPrompter'
1212import { Region } from '../../regions/endpoints'
13+ import { createRefreshButton } from '../buttons'
14+ import { getLogger } from '../../logger'
1315
1416const switchRegion = Symbol ( 'switchRegion' )
1517
@@ -22,6 +24,23 @@ export class RegionSubmenu<T> extends Prompter<RegionSubmenuResponse<T>> {
2224 private currentState : 'data' | 'region' = 'data'
2325 private steps ?: [ current : number , total : number ]
2426 public activePrompter ?: QuickPickPrompter < typeof switchRegion | T > | QuickPickPrompter < Region >
27+ private readonly defaultItems : DataQuickPickItem < typeof switchRegion | T > [ ] = [
28+ {
29+ label : 'Actions' ,
30+ kind : vscode . QuickPickItemKind . Separator ,
31+ data : undefined ,
32+ } ,
33+ {
34+ label : 'Switch Region' ,
35+ data : switchRegion ,
36+ description : `current region: ${ this . currentRegion } ` ,
37+ } ,
38+ {
39+ label : this . separatorLabel ,
40+ kind : vscode . QuickPickItemKind . Separator ,
41+ data : undefined ,
42+ } ,
43+ ]
2544
2645 public constructor (
2746 private readonly itemsProvider : ( region : string ) => ItemLoadTypes < T > ,
@@ -33,30 +52,32 @@ export class RegionSubmenu<T> extends Prompter<RegionSubmenuResponse<T>> {
3352 super ( )
3453 }
3554
55+ public refresh ( prompter : QuickPickPrompter < T | typeof switchRegion > ) : void {
56+ // This method cannot be async due to onClick() specifications. Thus we are forced to use .then, .catch as workaround.
57+ const activeBefore = prompter . quickPick . activeItems
58+ prompter
59+ . clearAndLoadItems ( this . itemsProvider ( this . currentRegion ) )
60+ . then ( ( ) => {
61+ prompter . quickPick . items = [ ...this . defaultItems , ...prompter . quickPick . items ]
62+ prompter . quickPick . activeItems = activeBefore
63+ } )
64+ . catch ( ( e ) => {
65+ getLogger ( ) . error ( 'clearAndLoadItems failed: %s' , ( e as Error ) . message )
66+ } )
67+ }
68+
3669 private createMenuPrompter ( ) {
37- const prompter = createQuickPick < T | typeof switchRegion > (
38- this . itemsProvider ( this . currentRegion ) ,
39- this . dataOptions as ExtendedQuickPickOptions < T | typeof switchRegion >
40- )
41-
42- prompter . quickPick . items = [
43- {
44- label : 'Actions' ,
45- kind : vscode . QuickPickItemKind . Separator ,
46- data : undefined ,
47- } ,
48- {
49- label : 'Switch Region' ,
50- data : switchRegion ,
51- description : `current region: ${ this . currentRegion } ` ,
52- } ,
53- {
54- label : this . separatorLabel ,
55- kind : vscode . QuickPickItemKind . Separator ,
56- data : undefined ,
57- } ,
58- ...prompter . quickPick . items ,
59- ]
70+ const refreshButton = createRefreshButton ( )
71+ const items = this . itemsProvider ( this . currentRegion )
72+ const prompter = createQuickPick < T | typeof switchRegion > ( items , {
73+ ...this . dataOptions ,
74+ buttons : [ ...( this . dataOptions ?. buttons ?? [ ] ) , refreshButton ] ,
75+ } as ExtendedQuickPickOptions < T | typeof switchRegion > )
76+
77+ prompter . quickPick . items = [ ...this . defaultItems , ...prompter . quickPick . items ]
78+
79+ refreshButton . onClick = ( ) => this . refresh ( prompter )
80+
6081 return prompter
6182 }
6283
0 commit comments