-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement workspace filtering by project (#1455)
* Contribute new filter icon and command * Add search query parameter to projects endpoint * Implement project quick pick * Improve error messages
- Loading branch information
Showing
4 changed files
with
153 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* Copyright (c) HashiCorp, Inc. | ||
* SPDX-License-Identifier: MPL-2.0 | ||
*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import { apiClient } from '../../terraformCloud'; | ||
import { Project } from '../../terraformCloud/project'; | ||
|
||
export class ResetProjectItem implements vscode.QuickPickItem { | ||
get label() { | ||
return '$(clear-all) Clear project filter. Show all workspaces'; | ||
} | ||
get description() { | ||
return ''; | ||
} | ||
get alwaysShow() { | ||
return true; | ||
} | ||
} | ||
|
||
class ProjectItem implements vscode.QuickPickItem { | ||
constructor(protected project: Project) {} | ||
get label() { | ||
return this.project.attributes.name; | ||
} | ||
get description() { | ||
return this.project.id; | ||
} | ||
} | ||
|
||
async function createProjectItems(organization: string, search?: string): Promise<ProjectItem[]> { | ||
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 ProjectQuickPick { | ||
private quickPick: vscode.QuickPick<vscode.QuickPickItem>; | ||
private fetchTimerKey: NodeJS.Timeout | undefined; | ||
|
||
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 onDidChangeValue() { | ||
clearTimeout(this.fetchTimerKey); | ||
// Only starts fetching projects after a user stopped typing for 300ms | ||
this.fetchTimerKey = setTimeout(() => this.fetchProjects.apply(this), 300); | ||
} | ||
|
||
private async fetchProjects() { | ||
// TODO?: To further improve performance, we could consider throttling this function | ||
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))); | ||
} catch (error) { | ||
let message = 'Failed to fetch projects'; | ||
if (error instanceof Error) { | ||
message = error.message; | ||
} else if (typeof error === 'string') { | ||
message = error; | ||
} | ||
|
||
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<vscode.QuickPickItem | undefined>((c) => { | ||
this.quickPick.onDidAccept(() => c(this.quickPick.selectedItems[0])); | ||
this.quickPick.onDidHide(() => c(undefined)); | ||
this.quickPick.show(); | ||
}); | ||
this.quickPick.hide(); | ||
|
||
return project; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters