Skip to content

Commit 548bc2a

Browse files
committed
Fix connection
1 parent e9d85d4 commit 548bc2a

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

src/pages/RemoteProduct.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ export function RemoteProduct() {
7777
return () => window.removeEventListener('eca-toggle-sidebar', handler);
7878
}, []);
7979

80-
// --- Guard stale activeId ---
80+
// --- Guard stale / missing activeId ---
8181

8282
useEffect(() => {
83-
if (activeId && entries.length > 0 && !entries.find((e) => e.id === activeId)) {
84-
setActiveId(null);
83+
if (entries.length > 0 && !entries.find((e) => e.id === activeId)) {
84+
setActiveId(entries[entries.length - 1].id);
8585
}
8686
}, [entries, activeId]);
8787

@@ -160,8 +160,9 @@ export function RemoteProduct() {
160160
return;
161161
}
162162

163-
// Create a connection entry for each found port
164-
let firstNewId: string | null = null;
163+
// Create a connection entry for each found port, select the latest (highest port)
164+
let latestId: string | null = null;
165+
let latestPort = -1;
165166
setEntries((prev) => {
166167
const next = [...prev];
167168
for (const port of progress.found) {
@@ -172,17 +173,17 @@ export function RemoteProduct() {
172173
if (existing.password !== password || existing.protocol !== protocol) {
173174
Object.assign(existing, { password, protocol });
174175
}
175-
if (!firstNewId) firstNewId = existing.id;
176+
if (port > latestPort) { latestPort = port; latestId = existing.id; }
176177
} else {
177178
const id = crypto.randomUUID();
178179
next.push({ id, host: hostWithPort, password, protocol, status: 'idle' });
179-
if (!firstNewId) firstNewId = id;
180+
if (port > latestPort) { latestPort = port; latestId = id; }
180181
}
181182
}
182183
return next;
183184
});
184185

185-
if (firstNewId) setActiveId(firstNewId);
186+
if (latestId) setActiveId(latestId);
186187
setShowForm(false);
187188
setFormConnecting(false);
188189
// Clear discovery progress after a short delay to let user see the result

0 commit comments

Comments
 (0)