From fbe71334d627d21e827514bd6fd090ac2d35a764 Mon Sep 17 00:00:00 2001 From: daniel-vahn Date: Tue, 16 Apr 2024 13:17:23 +0200 Subject: [PATCH] connection test --- .../connectOverlay/ConnectOverlay.tsx | 54 ++++++++++++------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/src/components/connectOverlay/ConnectOverlay.tsx b/src/components/connectOverlay/ConnectOverlay.tsx index 2ec9ac8..12f04d6 100644 --- a/src/components/connectOverlay/ConnectOverlay.tsx +++ b/src/components/connectOverlay/ConnectOverlay.tsx @@ -42,27 +42,41 @@ const ConnectOverlay: React.FC = ({ // connect function const connectMetamask = async () => { - await axios.post(BRIDGE_URL + '/connect').then((response) => { - try { - WebApp.openLink(response.data.universalLink); - close(); - } catch (error) { - console.error(error); - } - }); - - // check if connected - await axios.get(BRIDGE_URL + '/is-connected').then((response) => { - try { - if (response.data.connected) { - onConnect(); - } else { - console.log('Not Connected'); + try { + const response = await axios.post(BRIDGE_URL + '/connect'); + WebApp.openLink(response.data.universalLink); + close(); + + const startTime = Date.now(); // Record start time + const timeout = 30000; // 30 seconds timeout + + // Function to check connection status + const checkConnection = async () => { + if (Date.now() - startTime > timeout) { + return; } - } catch (error) { - console.error(error); - } - }); + + try { + const statusResponse = await axios.get( + BRIDGE_URL + '/is-connected' + ); + if (statusResponse.data.connected) { + onConnect(); + } else { + console.log('Not Connected, checking again...'); + setTimeout(checkConnection, 1000); + } + } catch (error) { + console.error('Error checking connection:', error); + setTimeout(checkConnection, 1000); + } + }; + + // Start checking connection status + checkConnection(); + } catch (error) { + console.error('Error during initial connection:', error); + } }; // Toggle Wallets