Skip to content

Commit 30839c8

Browse files
committed
Fix selection when workspace name collides
This can happen if multiple users are using the same name for their workspaces. Turns out we can just match the workspace ID. It hardly seems worth a test to see that a == is working, so I did not bother to update it.
1 parent 0f66e3e commit 30839c8

File tree

3 files changed

+13
-65
lines changed

3 files changed

+13
-65
lines changed

src/main/kotlin/com/coder/gateway/models/WorkspaceAgentListModel.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ data class WorkspaceAgentListModel(
1616
var icon: Icon? = null,
1717
// The combined status of the workspace and agent to display on the row.
1818
val status: WorkspaceAndAgentStatus = WorkspaceAndAgentStatus.from(workspace, agent),
19-
// The combined `workspace.agent` name to display on the row.
19+
// The combined `workspace.agent` name to display on the row. Users can have workspaces with the same name, so it
20+
// must not be used as a unique identifier.
2021
val name: String = if (agent != null) "${workspace.name}.${agent.name}" else workspace.name,
2122
)

src/main/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepView.kt

+11-9
Original file line numberDiff line numberDiff line change
@@ -989,17 +989,19 @@ class WorkspacesTable : TableView<WorkspaceAgentListModel>(WorkspacesTableModel(
989989
}
990990
}
991991

992-
fun getNewSelection(oldSelection: WorkspaceAgentListModel?): Int {
992+
/**
993+
* If a row becomes unselected because the workspace turned on, find the
994+
* first agent row and select that.
995+
*
996+
* If a row becomes unselected because the workspace turned off, find the
997+
* workspace row and select that.
998+
*/
999+
private fun getNewSelection(oldSelection: WorkspaceAgentListModel?): Int {
9931000
if (oldSelection == null) {
9941001
return -1
9951002
}
996-
val index = listTableModel.items.indexOfFirst { it.name == oldSelection.name }
997-
if (index > -1) {
998-
return index
999-
}
1000-
// If there is no matching agent, try matching on just the workspace.
1001-
// It is possible it turned off so it no longer has agents displaying;
1002-
// in this case we want to keep it highlighted.
1003-
return listTableModel.items.indexOfFirst { it.workspace.name == oldSelection.workspace.name }
1003+
// Both cases are handled by just looking for the ID, since we only ever
1004+
// show agents or a workspace but never both.
1005+
return listTableModel.items.indexOfFirst { it.workspace.id == oldSelection.workspace.id }
10041006
}
10051007
}

src/test/kotlin/com/coder/gateway/views/steps/CoderWorkspacesStepViewTest.kt

-55
This file was deleted.

0 commit comments

Comments
 (0)