From d03eb599a8a525b928a42a226f5d5543ad33167e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alexandre=20Co=CC=82te=CC=81?= <alex.cote@shopify.com>
Date: Fri, 21 Jun 2024 16:32:22 -0400
Subject: [PATCH] Avoid auto selecting instance in iFrame

Avoid auto selecting the outter frame instance when redux exist in
iFrame embedded on the page.
---
 .changeset/fluffy-steaks-promise.md                   |  5 +++++
 .../src/window/store/instanceSelectorMiddleware.ts    | 11 ++++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)
 create mode 100644 .changeset/fluffy-steaks-promise.md

diff --git a/.changeset/fluffy-steaks-promise.md b/.changeset/fluffy-steaks-promise.md
new file mode 100644
index 0000000000..7c9d27fa73
--- /dev/null
+++ b/.changeset/fluffy-steaks-promise.md
@@ -0,0 +1,5 @@
+---
+'remotedev-redux-devtools-extension': patch
+---
+
+Avoid auto-selecting instance with iFrames
diff --git a/extension/src/window/store/instanceSelectorMiddleware.ts b/extension/src/window/store/instanceSelectorMiddleware.ts
index e10879bc97..b72b6906da 100644
--- a/extension/src/window/store/instanceSelectorMiddleware.ts
+++ b/extension/src/window/store/instanceSelectorMiddleware.ts
@@ -13,7 +13,16 @@ function selectInstance(
 ) {
   const instances = store.getState().instances;
   if (instances.current === 'default') return;
-  const connections = instances.connections[tabId];
+
+  const connections = Object.entries(instances.connections).reduce<
+    (string | number)[]
+  >((acc, [key, connections]) => {
+    if (key.startsWith(String(tabId))) {
+      return [...acc, ...connections];
+    }
+    return acc;
+  }, []);
+
   if (connections && connections.length === 1) {
     next({ type: SELECT_INSTANCE, selected: connections[0] });
   }