Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 0dcbcf2

Browse files
authored
Al-amin/handle_website_status_cookie_on_VPN_change (#2593)
* handle_website_status_cookie_on_VPN_change * revert changes * revert back my old changes * revert back my old changes * merged master
1 parent 8c40f2e commit 0dcbcf2

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/components/hooks/use-website-status.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect } from 'react'
1+
import { useState, useLayoutEffect } from 'react'
22
import { useCookieState } from './use-cookie-state'
33
import { BinarySocketBase } from 'common/websocket/socket_base'
44
import { getDateFromToday } from 'common/utility'
@@ -10,13 +10,13 @@ export const useWebsiteStatus = () => {
1010
const [website_status, setWebsiteStatus] = useCookieState(null, WEBSITE_STATUS_COUNTRY_KEY, {
1111
expires: getDateFromToday(COOKIE_EXPIRY_DAYS),
1212
})
13+
1314
const [is_loading, setLoading] = useState(true)
1415

15-
useEffect(() => {
16+
useLayoutEffect(() => {
1617
setLoading(true)
1718
if (!website_status) {
1819
const binary_socket = BinarySocketBase.init()
19-
2020
binary_socket.onopen = () => {
2121
binary_socket.send(JSON.stringify({ website_status: 1 }))
2222
}
@@ -26,13 +26,30 @@ export const useWebsiteStatus = () => {
2626

2727
if (!response.error) {
2828
const { clients_country, crypto_config } = response.website_status
29+
2930
setWebsiteStatus({ clients_country, crypto_config })
3031
}
3132
setLoading(false)
3233
binary_socket.close()
3334
}
3435
} else {
35-
setLoading(false)
36+
const binary_socket = BinarySocketBase.init()
37+
binary_socket.onopen = () => {
38+
binary_socket.send(JSON.stringify({ website_status: 1 }))
39+
}
40+
41+
binary_socket.onmessage = (msg) => {
42+
const response = JSON.parse(msg.data)
43+
44+
if (!response.error) {
45+
const { clients_country, crypto_config } = response.website_status
46+
if (clients_country !== website_status.clients_country) {
47+
setWebsiteStatus({ clients_country, crypto_config })
48+
}
49+
}
50+
binary_socket.close()
51+
setLoading(false)
52+
}
3653
}
3754
}, [website_status])
3855

0 commit comments

Comments
 (0)