diff --git a/package-lock.json b/package-lock.json index 1475bcfd..65e7966e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,7 @@ "@deriv-com/auth-client": "1.0.27", "@deriv-com/translations": "^1.3.9", "@deriv-com/ui": "^1.35.6", - "@deriv-com/utils": "^0.0.34", + "@deriv-com/utils": "^0.0.38", "@deriv/quill-icons": "^2.1.2", "@sendbird/chat": "^4.11.3", "@tanstack/react-query": "^5.28.14", @@ -3943,9 +3943,9 @@ ] }, "node_modules/@deriv-com/utils": { - "version": "0.0.34", - "resolved": "https://registry.npmjs.org/@deriv-com/utils/-/utils-0.0.34.tgz", - "integrity": "sha512-Hq2DWVKiSR4dTnVTy1lMwAt1F58DPAP0VGB2Nfa9mtPWOfOBGO6ZIeU7+fLGrATukc55ZfHDmfqwgnAi6Nqczg==" + "version": "0.0.38", + "resolved": "https://registry.npmjs.org/@deriv-com/utils/-/utils-0.0.38.tgz", + "integrity": "sha512-8PBW6IssZYo9JDIwPOKlz36772rExLO+FyI55mZAUz2yVr9dD+ZRUEXsNgIAcGQEGE0U+m5HOQLX/x6JiTqHkQ==" }, "node_modules/@deriv/api-types": { "version": "1.0.1454", diff --git a/package.json b/package.json index 1d8795fb..46f5dd4f 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "@deriv-com/auth-client": "1.0.27", "@deriv-com/translations": "^1.3.9", "@deriv-com/ui": "^1.35.6", - "@deriv-com/utils": "^0.0.34", + "@deriv-com/utils": "^0.0.38", "@deriv/quill-icons": "^2.1.2", "@sendbird/chat": "^4.11.3", "@tanstack/react-query": "^5.28.14", diff --git a/src/hooks/custom-hooks/useDerivAnalytics.ts b/src/hooks/custom-hooks/useDerivAnalytics.ts index 5257a4a6..74c07e96 100644 --- a/src/hooks/custom-hooks/useDerivAnalytics.ts +++ b/src/hooks/custom-hooks/useDerivAnalytics.ts @@ -1,10 +1,9 @@ import Cookies from 'js-cookie'; import { FIREBASE_INIT_DATA } from '@/constants'; -import getCountry from '@/utils/get-country'; import { Analytics } from '@deriv-com/analytics'; import { useWebsiteStatus } from '@deriv-com/api-hooks'; import { useDevice } from '@deriv-com/ui'; -import { LocalStorageConstants, LocalStorageUtils, WebSocketUtils } from '@deriv-com/utils'; +import { CountryUtils, LocalStorageConstants, LocalStorageUtils, WebSocketUtils } from '@deriv-com/utils'; import { useActiveAccount } from '../api/account'; /** @@ -48,7 +47,7 @@ const useDerivAnalytics = () => { attributes: { account_type: activeAccount?.account_type || 'unlogged', app_id: String(WebSocketUtils.getAppId()), - country: await getCountry(), + country: await CountryUtils.getCountry(), device_language: navigator?.language || 'en-EN', device_type: isMobile ? 'mobile' : 'desktop', domain: window.location.hostname, diff --git a/src/hooks/custom-hooks/useFreshchat.ts b/src/hooks/custom-hooks/useFreshchat.ts index 056b0054..74e83515 100644 --- a/src/hooks/custom-hooks/useFreshchat.ts +++ b/src/hooks/custom-hooks/useFreshchat.ts @@ -2,7 +2,7 @@ import { useEffect, useState } from 'react'; import { useScript } from 'usehooks-ts'; const useFreshChat = (token: string | null) => { - const scriptStatus = useScript('https://static.deriv.com/scripts/freshchat.js'); + const scriptStatus = useScript('https://static.deriv.com/scripts/freshchat/freshchat-1.0.1.js'); const [isReady, setIsReady] = useState(false); const language = localStorage.getItem('i18n_language') || 'EN'; diff --git a/src/utils/__tests__/get-country.spec.ts b/src/utils/__tests__/get-country.spec.ts deleted file mode 100644 index 97d6e492..00000000 --- a/src/utils/__tests__/get-country.spec.ts +++ /dev/null @@ -1,36 +0,0 @@ -import getCountry from '../get-country'; - -describe('getCountry', () => { - beforeEach(() => { - global.fetch = jest.fn() as jest.Mock; - }); - - afterEach(() => { - jest.resetAllMocks(); - }); - - it('should return the country code in lowercase when available', async () => { - (global.fetch as jest.Mock).mockResolvedValue({ - text: async () => 'loc=US\nother=info\n', - }); - - const country = await getCountry(); - expect(country).toBe('us'); - }); - - it('should return an empty string if the loc field is not present', async () => { - (global.fetch as jest.Mock).mockResolvedValue({ - text: async () => 'other=info\n', - }); - - const country = await getCountry(); - expect(country).toBe(''); - }); - - it('should return an empty string if the fetch fails', async () => { - (global.fetch as jest.Mock).mockRejectedValue(new Error('Fetch failed')); - - const country = await getCountry(); - expect(country).toBe(''); - }); -}); diff --git a/src/utils/get-country.ts b/src/utils/get-country.ts deleted file mode 100644 index f1a0c60f..00000000 --- a/src/utils/get-country.ts +++ /dev/null @@ -1,15 +0,0 @@ -import Cookies from 'js-cookie'; - -const getCountry = async () => { - try { - const response = await fetch('https://www.cloudflare.com/cdn-cgi/trace').catch(() => null); - const text = response ? await response.text().catch(() => '') : ''; - const entries = text ? text.split('\n').map(v => v.split('=', 2)) : []; - const data = entries.length ? Object.fromEntries(entries) : {}; - return data?.loc?.toLowerCase() || JSON.parse(Cookies.get('website_status') || '') || ''; - } catch { - return ''; - } -}; - -export default getCountry;