diff --git a/packages/dex-widget/src/getDomain.ts b/packages/dex-widget/src/getDomain.ts new file mode 100644 index 0000000..39077ca --- /dev/null +++ b/packages/dex-widget/src/getDomain.ts @@ -0,0 +1,34 @@ +const domains = [ + 'https://www.okx.com', + 'https://www.okx.ac', +]; + +AbortSignal.timeout ??= function timeout(ms) { + const ctrl = new AbortController(); + setTimeout(() => ctrl.abort(), ms); + return ctrl.signal; +}; + +const checkDomain = (domain: string, timeout = 5000): Promise => { + if (!domain) { + return Promise.resolve(); + } + return fetch(domain, { signal: AbortSignal.timeout(timeout) }) + .then(response => { + if (response.ok) { + return domain; + } else { + const nextDomain = domains[domains.indexOf(domain) + 1]; + return checkDomain(nextDomain, timeout); + } + }) + .catch(() => { + const nextDomain = domains[domains.indexOf(domain) + 1]; + return checkDomain(nextDomain, timeout); + }); +}; + +// Start checking domains +export function getDomain() { + return checkDomain(domains[0]); +} \ No newline at end of file diff --git a/packages/dex-widget/src/index.ts b/packages/dex-widget/src/index.ts index 83db110..7d4a83f 100644 --- a/packages/dex-widget/src/index.ts +++ b/packages/dex-widget/src/index.ts @@ -11,3 +11,4 @@ export * from './themeUtils'; export * from './types'; export * from './verifyParamsUtils'; export * from './widgetHelp'; +export * from './getDomain'; \ No newline at end of file diff --git a/packages/widget-configurator/src/app/configurator/controls/BaseUrlControl.tsx b/packages/widget-configurator/src/app/configurator/controls/BaseUrlControl.tsx index f1d4687..84e045c 100644 --- a/packages/widget-configurator/src/app/configurator/controls/BaseUrlControl.tsx +++ b/packages/widget-configurator/src/app/configurator/controls/BaseUrlControl.tsx @@ -1,8 +1,9 @@ -import React, { Dispatch, SetStateAction } from 'react'; +import React, { Dispatch, SetStateAction, useEffect } from 'react'; import Select, { SelectChangeEvent } from '@mui/material/Select'; import FormControl from '@mui/material/FormControl'; import InputLabel from '@mui/material/InputLabel'; import MenuItem from '@mui/material/MenuItem'; +import { getDomain } from '@okxweb3/dex-widget'; const BASE_DEFAULT_URL = import.meta.env.VITE_BASE_DEFAULT_URL; @@ -20,6 +21,19 @@ export const BaseUrlControl = ({ state, widgetHandler, params }: { }); }; + useEffect(() => { + getDomain().then((domain) => { + if (domain) { + setBaseUrl(domain); + setTimeout(() => { + widgetHandler.current?.reload({ ...params, baseUrl: domain }); + }); + } + }); + + }, []); + + return ( <>