Skip to content

Commit 61d6405

Browse files
authored
Merge pull request #164 from argentlabs/fix/trpc-link-webwallet
fix: webwallet trpc link for popups
2 parents 9045e3f + 2f261ff commit 61d6405

File tree

2 files changed

+58
-22
lines changed

2 files changed

+58
-22
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
branches:
55
- develop
66
- main
7-
- beta-braavos-mobile
7+
- beta-trpc-link
88
- hotfix\/v[0-9]+.[0-9]+.[0-9]+
99

1010
jobs:

src/connectors/webwallet/helpers/trpc.ts

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,36 @@ export const setPopupOptions = ({
5353
popupParams = `width=${width},height=${height},top=${y},left=${x},toolbar=no,menubar=no,scrollbars=no,location=no,status=no,popup=1`
5454
}
5555

56+
const PopupManager = {
57+
currentPopup: null as Window | null,
58+
59+
createPopup(url: string, name: string, params: string): Window {
60+
// Close any existing popup
61+
if (this.currentPopup && !this.currentPopup.closed) {
62+
this.currentPopup.close()
63+
}
64+
65+
// Create popup immediately
66+
const popup = window.open(url, name, params)
67+
68+
if (!popup) {
69+
throw new Error("Popup blocked by browser")
70+
}
71+
72+
this.currentPopup = popup
73+
74+
// Monitor popup state
75+
const checkClosed = setInterval(() => {
76+
if (popup.closed) {
77+
clearInterval(checkClosed)
78+
this.currentPopup = null
79+
}
80+
}, 500)
81+
82+
return popup
83+
},
84+
}
85+
5686
// TODO: abstract AppRouter in order to have one single source of truth
5787
// At the moment, this is needed
5888
const appRouter = t.router({
@@ -195,29 +225,35 @@ export const trpcProxyClient = ({
195225
false: popupLink({
196226
listenWindow: window,
197227
createPopup: () => {
198-
let popup: Window | null = null
199-
const webwalletBtn = document.createElement("button")
200-
webwalletBtn.style.display = "none"
201-
webwalletBtn.addEventListener("click", () => {
202-
popup = window.open(
203-
`${popupOrigin}${popupLocation}`,
204-
"popup",
205-
popupParams,
206-
)
207-
})
208-
webwalletBtn.click()
209-
210-
// make sure popup is defined
211-
;(async () => {
212-
while (!popup) {
213-
await new Promise((resolve) => setTimeout(resolve, 100))
214-
}
215-
})()
228+
try {
229+
let popup: Window | null = null
230+
const button = document.createElement("button")
231+
button.style.position = "fixed"
232+
button.style.top = "-999px"
233+
button.style.left = "-999px"
234+
button.style.opacity = "0"
235+
button.style.pointerEvents = "none" // prevent click event
216236

217-
if (!popup) {
218-
throw new Error("Could not open popup")
237+
button.addEventListener("click", () => {
238+
popup = PopupManager.createPopup(
239+
`${popupOrigin}${popupLocation}`,
240+
"popup",
241+
popupParams,
242+
)
243+
})
244+
245+
document.body.appendChild(button)
246+
button.click()
247+
button.remove()
248+
249+
if (!popup) {
250+
throw new Error("Could not open popup")
251+
}
252+
return popup
253+
} catch (error) {
254+
console.error("Failed to create popup:", error)
255+
throw error
219256
}
220-
return popup
221257
},
222258
postOrigin: "*",
223259
}),

0 commit comments

Comments
 (0)