From 3d6f6c9bdf7656cac619e9b424a2b9c572c9ab20 Mon Sep 17 00:00:00 2001 From: Sagar Deriv Date: Fri, 13 Dec 2024 13:06:15 +0400 Subject: [PATCH] refactor: update url config --- src/hooks/useDerivWebSocket.js | 26 +++++++++++++++++++++++--- vite.config.js | 10 ++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/hooks/useDerivWebSocket.js b/src/hooks/useDerivWebSocket.js index f782fab..1d16af8 100644 --- a/src/hooks/useDerivWebSocket.js +++ b/src/hooks/useDerivWebSocket.js @@ -1,7 +1,23 @@ import { useState, useEffect, useCallback, useRef } from 'react' import useDerivAccounts from './useDerivAccounts' -const WS_URL = `${import.meta.env.VITE_WS_URL}?app_id=${import.meta.env.VITE_APP_ID}` +// Add console.log to debug env variables +console.log('Environment variables:', { + WS_URL: import.meta.env.VITE_WS_URL, + APP_ID: import.meta.env.VITE_APP_ID +}) + +// Construct WS_URL after ensuring env variables exist +const WS_URL = import.meta.env.VITE_WS_URL && import.meta.env.VITE_APP_ID + ? `${import.meta.env.VITE_WS_URL}?app_id=${import.meta.env.VITE_APP_ID}` + : null + +if (!WS_URL) { + console.error('WebSocket URL could not be constructed. Missing environment variables:', { + WS_URL: import.meta.env.VITE_WS_URL, + APP_ID: import.meta.env.VITE_APP_ID + }) +} // Singleton WebSocket instance let globalWs = null @@ -41,8 +57,12 @@ const useDerivWebSocket = () => { // Initialize WebSocket connection useEffect(() => { - if (!defaultAccount?.token) { - console.log('No default account or token available', defaultAccount) + if (!defaultAccount?.token || !WS_URL) { + console.log('Cannot initialize WebSocket:', { + hasToken: Boolean(defaultAccount?.token), + hasWsUrl: Boolean(WS_URL), + wsUrl: WS_URL + }) return } diff --git a/vite.config.js b/vite.config.js index 3ac5d7d..7870de8 100644 --- a/vite.config.js +++ b/vite.config.js @@ -4,6 +4,11 @@ import react from '@vitejs/plugin-react' export default defineConfig(({ mode }) => { const env = loadEnv(mode, process.cwd(), '') + console.log('Loaded environment variables:', { + APP_ID: env.VITE_APP_ID, + WS_URL: env.VITE_WS_URL + }) + return { plugins: [react()], server: { @@ -11,6 +16,11 @@ export default defineConfig(({ mode }) => { }, base: '/copy-trading/', define: { + 'import.meta.env.VITE_APP_ID': JSON.stringify(env.VITE_APP_ID), + 'import.meta.env.VITE_WS_URL': JSON.stringify(env.VITE_WS_URL), + 'import.meta.env.VITE_OAUTH_URL': JSON.stringify(env.VITE_OAUTH_URL), + 'import.meta.env.VITE_REDIRECT_URL': JSON.stringify(env.VITE_REDIRECT_URL), + 'import.meta.env.VITE_BASE_URL': JSON.stringify(env.VITE_BASE_URL), __APP_ENV__: env.APP_ENV } }