Skip to content

Refresh workspaces when logging in and out #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
code-asher committed Aug 18, 2023
commit 78ff33c258d8e5350585342ba74f3836bd7dd825
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
commands.navigateToWorkspaceSettings.bind(commands),
)
vscode.commands.registerCommand("coder.refreshWorkspaces", () => {
myWorkspacesProvider.refresh()
allWorkspacesProvider.refresh()
myWorkspacesProvider.fetchAndRefresh()
allWorkspacesProvider.fetchAndRefresh()
})

// Since the "onResolveRemoteAuthority:ssh-remote" activation event exists
Expand Down
46 changes: 21 additions & 25 deletions src/workspacesProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,35 @@ export class WorkspaceProvider implements vscode.TreeDataProvider<vscode.TreeIte
private agentMetadata: Record<WorkspaceAgent["id"], AgentMetadataEvent[]> = {}

constructor(private readonly getWorkspacesQuery: WorkspaceQuery, private readonly storage: Storage) {
if (!storage.getURL()) {
// Not logged in.
return
}
getWorkspaces({ q: this.getWorkspacesQuery })
.then((workspaces) => {
const workspacesTreeItem: WorkspaceTreeItem[] = []
workspaces.workspaces.forEach((workspace) => {
const showMetadata = this.getWorkspacesQuery === WorkspaceQuery.Mine
if (showMetadata) {
const agents = extractAgents(workspace)
agents.forEach((agent) => this.monitorMetadata(agent.id)) // monitor metadata for all agents
}
const treeItem = new WorkspaceTreeItem(
workspace,
this.getWorkspacesQuery === WorkspaceQuery.All,
showMetadata,
)
workspacesTreeItem.push(treeItem)
})
return workspacesTreeItem
})
.then((workspaces) => {
this.workspaces = workspaces
this.refresh()
this.fetchAndRefresh()
}

// fetchAndRefrehsh fetches new workspaces then re-renders the entire tree.
async fetchAndRefresh() {
const workspacesTreeItem: WorkspaceTreeItem[] = []
// If the URL is set then we are logged in.
if (this.storage.getURL()) {
const resp = await getWorkspaces({ q: this.getWorkspacesQuery })
resp.workspaces.forEach((workspace) => {
const showMetadata = this.getWorkspacesQuery === WorkspaceQuery.Mine
if (showMetadata) {
const agents = extractAgents(workspace)
agents.forEach((agent) => this.monitorMetadata(agent.id)) // monitor metadata for all agents
}
const treeItem = new WorkspaceTreeItem(workspace, this.getWorkspacesQuery === WorkspaceQuery.All, showMetadata)
workspacesTreeItem.push(treeItem)
})
}
this.workspaces = workspacesTreeItem
this.refresh()
}

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

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