5
5
import * as vscode from 'vscode'
6
6
import globals from '../../extensionGlobals'
7
7
import { isValidResponse , StepEstimator } from '../../wizards/wizard'
8
- import { createQuickPick , ExtendedQuickPickOptions , ItemLoadTypes } from '../pickerPrompter'
8
+ import { createQuickPick , DataQuickPickItem , ExtendedQuickPickOptions , ItemLoadTypes } from '../pickerPrompter'
9
9
import { Prompter , PromptResult } from '../prompter'
10
10
import { createRegionPrompter } from './region'
11
11
import { QuickPickPrompter } from '../pickerPrompter'
12
12
import { Region } from '../../regions/endpoints'
13
+ import { createRefreshButton } from '../buttons'
14
+ import { getLogger } from '../../logger'
13
15
14
16
const switchRegion = Symbol ( 'switchRegion' )
15
17
@@ -22,6 +24,23 @@ export class RegionSubmenu<T> extends Prompter<RegionSubmenuResponse<T>> {
22
24
private currentState : 'data' | 'region' = 'data'
23
25
private steps ?: [ current : number , total : number ]
24
26
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
+ ]
25
44
26
45
public constructor (
27
46
private readonly itemsProvider : ( region : string ) => ItemLoadTypes < T > ,
@@ -33,30 +52,32 @@ export class RegionSubmenu<T> extends Prompter<RegionSubmenuResponse<T>> {
33
52
super ( )
34
53
}
35
54
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
+
36
69
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
+
60
81
return prompter
61
82
}
62
83
0 commit comments