Skip to content

Commit

Permalink
Merge pull request #4 from sagar-deriv/refactor/url
Browse files Browse the repository at this point in the history
refactor: update url config
  • Loading branch information
ashkan-deriv authored Dec 13, 2024
2 parents 716868e + 3d6f6c9 commit d0343b5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/hooks/useDerivWebSocket.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}

Expand Down
10 changes: 10 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ 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: {
port: 8443,
},
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
}
}
Expand Down

0 comments on commit d0343b5

Please sign in to comment.