From 05f193df7b6af200f1101af054539be69d031ae0 Mon Sep 17 00:00:00 2001 From: Shantur Rathore Date: Tue, 20 Jan 2026 19:28:56 +0000 Subject: [PATCH] fix(ui): auto-select first ready instance after refresh --- packages/ui/src/stores/instances.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/packages/ui/src/stores/instances.ts b/packages/ui/src/stores/instances.ts index 99a55fde..7b768c2b 100644 --- a/packages/ui/src/stores/instances.ts +++ b/packages/ui/src/stores/instances.ts @@ -92,6 +92,19 @@ function workspaceDescriptorToInstance(descriptor: WorkspaceDescriptor): Instanc } } +function ensureActiveInstanceSelected(): void { + const current = activeInstanceId() + const instanceMap = instances() + if (current && instanceMap.has(current)) return + + for (const [id, instance] of instanceMap.entries()) { + if (instance.status === "ready") { + setActiveInstanceId(id) + return + } + } +} + function upsertWorkspace(descriptor: WorkspaceDescriptor) { const mapped = workspaceDescriptorToInstance(descriptor) if (instances().has(descriptor.id)) { @@ -102,6 +115,9 @@ function upsertWorkspace(descriptor: WorkspaceDescriptor) { if (descriptor.status === "ready") { attachClient(descriptor) + // If no tab is currently selected (common after UI refresh), + // auto-select the first ready instance. + ensureActiveInstanceSelected() } } @@ -225,15 +241,18 @@ async function hydrateInstanceData(instanceId: string) { } } -void (async function initializeWorkspaces() { + void (async function initializeWorkspaces() { try { const workspaces = await serverApi.fetchWorkspaces() workspaces.forEach((workspace) => upsertWorkspace(workspace)) + // After a UI refresh, we may have instances but no active selection. + ensureActiveInstanceSelected() } catch (error) { log.error("Failed to load workspaces", error) } })() + serverEvents.on("*", (event) => handleWorkspaceEvent(event)) function handleWorkspaceEvent(event: WorkspaceEventPayload) {