Skip to content

Commit 78ff33c

Browse files
committed
Make workspaces refresh load new workspaces
Currently the refresh button does exactly nothing. This will also let us load new workspaces in other cases, like when logging in or out.
1 parent b921eb9 commit 78ff33c

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

src/extension.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
138138
commands.navigateToWorkspaceSettings.bind(commands),
139139
)
140140
vscode.commands.registerCommand("coder.refreshWorkspaces", () => {
141-
myWorkspacesProvider.refresh()
142-
allWorkspacesProvider.refresh()
141+
myWorkspacesProvider.fetchAndRefresh()
142+
allWorkspacesProvider.fetchAndRefresh()
143143
})
144144

145145
// Since the "onResolveRemoteAuthority:ssh-remote" activation event exists

src/workspacesProvider.ts

+21-25
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,35 @@ export class WorkspaceProvider implements vscode.TreeDataProvider<vscode.TreeIte
1616
private agentMetadata: Record<WorkspaceAgent["id"], AgentMetadataEvent[]> = {}
1717

1818
constructor(private readonly getWorkspacesQuery: WorkspaceQuery, private readonly storage: Storage) {
19-
if (!storage.getURL()) {
20-
// Not logged in.
21-
return
22-
}
23-
getWorkspaces({ q: this.getWorkspacesQuery })
24-
.then((workspaces) => {
25-
const workspacesTreeItem: WorkspaceTreeItem[] = []
26-
workspaces.workspaces.forEach((workspace) => {
27-
const showMetadata = this.getWorkspacesQuery === WorkspaceQuery.Mine
28-
if (showMetadata) {
29-
const agents = extractAgents(workspace)
30-
agents.forEach((agent) => this.monitorMetadata(agent.id)) // monitor metadata for all agents
31-
}
32-
const treeItem = new WorkspaceTreeItem(
33-
workspace,
34-
this.getWorkspacesQuery === WorkspaceQuery.All,
35-
showMetadata,
36-
)
37-
workspacesTreeItem.push(treeItem)
38-
})
39-
return workspacesTreeItem
40-
})
41-
.then((workspaces) => {
42-
this.workspaces = workspaces
43-
this.refresh()
19+
this.fetchAndRefresh()
20+
}
21+
22+
// fetchAndRefrehsh fetches new workspaces then re-renders the entire tree.
23+
async fetchAndRefresh() {
24+
const workspacesTreeItem: WorkspaceTreeItem[] = []
25+
// If the URL is set then we are logged in.
26+
if (this.storage.getURL()) {
27+
const resp = await getWorkspaces({ q: this.getWorkspacesQuery })
28+
resp.workspaces.forEach((workspace) => {
29+
const showMetadata = this.getWorkspacesQuery === WorkspaceQuery.Mine
30+
if (showMetadata) {
31+
const agents = extractAgents(workspace)
32+
agents.forEach((agent) => this.monitorMetadata(agent.id)) // monitor metadata for all agents
33+
}
34+
const treeItem = new WorkspaceTreeItem(workspace, this.getWorkspacesQuery === WorkspaceQuery.All, showMetadata)
35+
workspacesTreeItem.push(treeItem)
4436
})
37+
}
38+
this.workspaces = workspacesTreeItem
39+
this.refresh()
4540
}
4641

4742
private _onDidChangeTreeData: vscode.EventEmitter<vscode.TreeItem | undefined | null | void> =
4843
new vscode.EventEmitter<vscode.TreeItem | undefined | null | void>()
4944
readonly onDidChangeTreeData: vscode.Event<vscode.TreeItem | undefined | null | void> =
5045
this._onDidChangeTreeData.event
5146

47+
// refresh causes the tree to re-render. It does not fetch fresh workspaces.
5248
refresh(item: vscode.TreeItem | undefined | null | void): void {
5349
this._onDidChangeTreeData.fire(item)
5450
}

0 commit comments

Comments
 (0)