From 9f462ae82606435a471e4c76d8bdc5f74b5755b3 Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Wed, 7 Jun 2023 19:55:59 +0200 Subject: [PATCH] Use new APIQuickPick for project filter --- src/providers/tfc/workspaceFilters.ts | 72 ++++++++------------------ src/providers/tfc/workspaceProvider.ts | 6 ++- 2 files changed, 27 insertions(+), 51 deletions(-) diff --git a/src/providers/tfc/workspaceFilters.ts b/src/providers/tfc/workspaceFilters.ts index 8f26640b4f..941726cc59 100644 --- a/src/providers/tfc/workspaceFilters.ts +++ b/src/providers/tfc/workspaceFilters.ts @@ -6,6 +6,7 @@ import * as vscode from 'vscode'; import { apiClient } from '../../terraformCloud'; import { Project } from '../../terraformCloud/project'; +import { APIResource } from '../../utils/uiHelpers'; export class ResetProjectItem implements vscode.QuickPickItem { get label() { @@ -29,48 +30,35 @@ class ProjectItem implements vscode.QuickPickItem { } } -async function createProjectItems(organization: string, search?: string): Promise { - const projects = await apiClient.listProjects({ - params: { - organization_name: organization, - }, - // Include query parameter only if search argument is passed - ...(search && { - queries: { - q: search, - }, - }), - }); - - return projects.data.map((project) => new ProjectItem(project)); -} +export class ProjectsAPIResource implements APIResource { + name = 'projects'; + title = 'Filter Workspaces'; + placeholder = 'Select a project (type to search)'; -export class ProjectQuickPick { - private quickPick: vscode.QuickPick; - private fetchTimerKey: NodeJS.Timeout | undefined; + constructor(private organizationName: string) {} - constructor(private organizationName: string) { - this.quickPick = vscode.window.createQuickPick(); - this.quickPick.title = 'Filter Workspaces'; - this.quickPick.placeholder = 'Select a project (type to search)'; - this.quickPick.onDidChangeValue(this.onDidChangeValue, this); - } + private async createProjectItems(organization: string, search?: string): Promise { + const projects = await apiClient.listProjects({ + params: { + organization_name: organization, + }, + // Include query parameter only if search argument is passed + ...(search && { + queries: { + q: search, + }, + }), + }); - private onDidChangeValue() { - clearTimeout(this.fetchTimerKey); - // Only starts fetching projects after a user stopped typing for 300ms - this.fetchTimerKey = setTimeout(() => this.fetchProjects.apply(this), 300); + return projects.data.map((project) => new ProjectItem(project)); } - private async fetchProjects() { - // TODO?: To further improve performance, we could consider throttling this function + async fetchItems(query?: string): Promise { const resetProjectItem = new ResetProjectItem(); const picks: vscode.QuickPickItem[] = [resetProjectItem, { label: '', kind: vscode.QuickPickItemKind.Separator }]; - try { - this.quickPick.busy = true; - this.quickPick.show(); - picks.push(...(await createProjectItems(this.organizationName, this.quickPick.value))); + try { + picks.push(...(await this.createProjectItems(this.organizationName, query))); } catch (error) { let message = 'Failed to fetch projects'; if (error instanceof Error) { @@ -81,22 +69,8 @@ export class ProjectQuickPick { picks.push({ label: `$(error) Error: ${message}`, alwaysShow: true }); console.error(error); - } finally { - this.quickPick.items = picks; - this.quickPick.busy = false; } - } - - async pick() { - await this.fetchProjects(); - - const project = await new Promise((c) => { - this.quickPick.onDidAccept(() => c(this.quickPick.selectedItems[0])); - this.quickPick.onDidHide(() => c(undefined)); - this.quickPick.show(); - }); - this.quickPick.hide(); - return project; + return picks; } } diff --git a/src/providers/tfc/workspaceProvider.ts b/src/providers/tfc/workspaceProvider.ts index 94a94d8116..8b40b9c605 100644 --- a/src/providers/tfc/workspaceProvider.ts +++ b/src/providers/tfc/workspaceProvider.ts @@ -9,7 +9,8 @@ import axios from 'axios'; import { RunTreeDataProvider } from './runProvider'; import { apiClient } from '../../terraformCloud'; import { TerraformCloudAuthenticationProvider } from '../authenticationProvider'; -import { ProjectQuickPick, ResetProjectItem } from './workspaceFilters'; +import { ProjectsAPIResource, ResetProjectItem } from './workspaceFilters'; +import { APIQuickPick } from '../../utils/uiHelpers'; export class WorkspaceTreeDataProvider implements vscode.TreeDataProvider, vscode.Disposable { private readonly didChangeTreeData = new vscode.EventEmitter(); @@ -40,7 +41,8 @@ export class WorkspaceTreeDataProvider implements vscode.TreeDataProvider { // TODO! only run this if user is logged in const organization = this.ctx.workspaceState.get('terraform.cloud.organization', ''); - const projectQuickPick = new ProjectQuickPick(organization); + const projectAPIResource = new ProjectsAPIResource(organization); + const projectQuickPick = new APIQuickPick(projectAPIResource); const project = await projectQuickPick.pick(); if (project === undefined || project instanceof ResetProjectItem) {