Skip to content

Commit

Permalink
Impl
Browse files Browse the repository at this point in the history
  • Loading branch information
MicroFish91 committed Dec 18, 2023
1 parent 2bc4333 commit 955405d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
6 changes: 5 additions & 1 deletion utils/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1733,7 +1733,7 @@ export interface PickExperienceContext extends IActionContext {
* @param context The action context
* @param tdp Azure resource tree data provider to perform the pick experience on
*/
export declare function subscriptionExperience(context: IActionContext, tdp: TreeDataProvider<ResourceGroupsItem>): Promise<AzureSubscription>;
export declare function subscriptionExperience(context: IActionContext, tdp: TreeDataProvider<ResourceGroupsItem>, options?: { selectSubscriptionId?: string, showLoadingPrompt?: boolean }): Promise<AzureSubscription>;

/**
* Prompts the user to pick an Azure resource using a wizard comprised of quick pick steps.
Expand Down Expand Up @@ -1876,6 +1876,10 @@ export declare interface SkipIfOneQuickPickOptions extends GenericQuickPickOptio
skipIfOne?: true;
}

export declare interface AzureSubscriptionQuickPickOptions extends GenericQuickPickOptions {
selectSubscriptionId?: string;
}

export declare abstract class GenericQuickPickStep<TContext extends QuickPickWizardContext, TOptions extends GenericQuickPickOptions> extends AzureWizardPromptStep<TContext> {
constructor(
treeDataProvider: TreeDataProvider<unknown>,
Expand Down
12 changes: 11 additions & 1 deletion utils/src/pickTreeItem/GenericQuickPickStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import * as vscode from 'vscode';
import * as types from '../../index';
import { AzureWizardPromptStep } from '../wizard/AzureWizardPromptStep';
import { getLastNode } from './getLastNode';
import { PickFilter } from './PickFilter';
import { getLastNode } from './getLastNode';

export abstract class GenericQuickPickStep<TContext extends types.QuickPickWizardContext, TOptions extends types.GenericQuickPickOptions> extends AzureWizardPromptStep<TContext> {
public readonly supportsDuplicateSteps = true;
Expand Down Expand Up @@ -50,6 +50,14 @@ export abstract class GenericQuickPickStep<TContext extends types.QuickPickWizar
}
}

const pickWithoutPrompt = this.getPickWithoutPrompt?.(picks);
if (pickWithoutPrompt) {
const ti = await this.treeDataProvider.getTreeItem(pickWithoutPrompt.data);
if (!ti.command) {
return pickWithoutPrompt.data;
}
}

const selected = await wizardContext.ui.showQuickPick(picks, {
...(this.promptOptions ?? {})
});
Expand Down Expand Up @@ -94,4 +102,6 @@ export abstract class GenericQuickPickStep<TContext extends types.QuickPickWizar
data: element,
};
}

protected getPickWithoutPrompt?(picks: types.IAzureQuickPickItem<unknown>[]): types.IAzureQuickPickItem<unknown> | undefined;
}
18 changes: 11 additions & 7 deletions utils/src/pickTreeItem/experiences/subscriptionExperience.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as types from '../../../index';
import type { AzureSubscription } from '@microsoft/vscode-azureresources-api';
import * as vscode from 'vscode';
import * as types from '../../../index';
import { PickSubscriptionWizardContext } from '../../../index';
import { NoResourceFoundError } from '../../errors';
import { AzureWizard } from '../../wizard/AzureWizard';
import { QuickPickAzureSubscriptionStep } from '../quickPickAzureResource/QuickPickAzureSubscriptionStep';
import { PickSubscriptionWizardContext } from '../../../index';
import { ResourceGroupsItem } from '../quickPickAzureResource/tempTypes';
import { NoResourceFoundError } from '../../errors';
import type { AzureSubscription } from '@microsoft/vscode-azureresources-api';

export async function subscriptionExperience(context: types.IActionContext, tdp: vscode.TreeDataProvider<ResourceGroupsItem>): Promise<AzureSubscription> {
export async function subscriptionExperience(
context: types.IActionContext,
tdp: vscode.TreeDataProvider<ResourceGroupsItem>,
options?: { selectSubscriptionId?: string, showLoadingPrompt?: boolean }
): Promise<AzureSubscription> {

const wizardContext = { ...context } as PickSubscriptionWizardContext;
wizardContext.pickedNodes = [];

const wizard = new AzureWizard(wizardContext, {
hideStepCount: true,
promptSteps: [new QuickPickAzureSubscriptionStep(tdp)],
showLoadingPrompt: true,
promptSteps: [new QuickPickAzureSubscriptionStep(tdp, { selectSubscriptionId: options?.selectSubscriptionId })],
showLoadingPrompt: options?.showLoadingPrompt ?? true,
});

await wizard.prompt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { AzureResourceQuickPickWizardContext, GenericQuickPickOptions, SkipIfOneQuickPickOptions } from '../../../index';
import { AzureResourceQuickPickWizardContext, AzureSubscriptionQuickPickOptions, IAzureQuickPickItem, SkipIfOneQuickPickOptions } from '../../../index';
import { GenericQuickPickStepWithCommands } from '../GenericQuickPickStepWithCommands';
import { PickFilter } from '../PickFilter';
import { ResourceGroupsItem, SubscriptionItem } from './tempTypes';

export class QuickPickAzureSubscriptionStep extends GenericQuickPickStepWithCommands<AzureResourceQuickPickWizardContext, SkipIfOneQuickPickOptions> {
public constructor(tdp: vscode.TreeDataProvider<ResourceGroupsItem>, options?: GenericQuickPickOptions) {
protected subscriptionId?: string;

public constructor(tdp: vscode.TreeDataProvider<ResourceGroupsItem>, options?: AzureSubscriptionQuickPickOptions) {
super(tdp, {
...options,
skipIfOne: true, // Subscription is always skip-if-one
}, {
placeHolder: vscode.l10n.t('Select subscription'),
noPicksMessage: vscode.l10n.t('No subscriptions found'),
});

this.subscriptionId = options?.selectSubscriptionId;
}

protected readonly pickFilter = new AzureSubscriptionPickFilter();
Expand All @@ -30,6 +34,14 @@ export class QuickPickAzureSubscriptionStep extends GenericQuickPickStepWithComm

return pickedSubscription;
}

protected override getPickWithoutPrompt(picks: IAzureQuickPickItem<SubscriptionItem>[]): IAzureQuickPickItem<SubscriptionItem> | undefined {
if (!this.subscriptionId) {
return undefined;
}

return picks.find(pick => pick.data.subscription.subscriptionId === this.subscriptionId);
}
}

class AzureSubscriptionPickFilter implements PickFilter {
Expand Down

0 comments on commit 955405d

Please sign in to comment.