Skip to content

Commit 05f193d

Browse files
committed
fix(ui): auto-select first ready instance after refresh
1 parent c9b5bb1 commit 05f193d

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

packages/ui/src/stores/instances.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ function workspaceDescriptorToInstance(descriptor: WorkspaceDescriptor): Instanc
9292
}
9393
}
9494

95+
function ensureActiveInstanceSelected(): void {
96+
const current = activeInstanceId()
97+
const instanceMap = instances()
98+
if (current && instanceMap.has(current)) return
99+
100+
for (const [id, instance] of instanceMap.entries()) {
101+
if (instance.status === "ready") {
102+
setActiveInstanceId(id)
103+
return
104+
}
105+
}
106+
}
107+
95108
function upsertWorkspace(descriptor: WorkspaceDescriptor) {
96109
const mapped = workspaceDescriptorToInstance(descriptor)
97110
if (instances().has(descriptor.id)) {
@@ -102,6 +115,9 @@ function upsertWorkspace(descriptor: WorkspaceDescriptor) {
102115

103116
if (descriptor.status === "ready") {
104117
attachClient(descriptor)
118+
// If no tab is currently selected (common after UI refresh),
119+
// auto-select the first ready instance.
120+
ensureActiveInstanceSelected()
105121
}
106122
}
107123

@@ -225,15 +241,18 @@ async function hydrateInstanceData(instanceId: string) {
225241
}
226242
}
227243

228-
void (async function initializeWorkspaces() {
244+
void (async function initializeWorkspaces() {
229245
try {
230246
const workspaces = await serverApi.fetchWorkspaces()
231247
workspaces.forEach((workspace) => upsertWorkspace(workspace))
248+
// After a UI refresh, we may have instances but no active selection.
249+
ensureActiveInstanceSelected()
232250
} catch (error) {
233251
log.error("Failed to load workspaces", error)
234252
}
235253
})()
236254

255+
237256
serverEvents.on("*", (event) => handleWorkspaceEvent(event))
238257

239258
function handleWorkspaceEvent(event: WorkspaceEventPayload) {

0 commit comments

Comments
 (0)