Skip to content

Commit

Permalink
get domain
Browse files Browse the repository at this point in the history
  • Loading branch information
HowieFenng committed Dec 19, 2024
1 parent 7240183 commit 7a6a15f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
34 changes: 34 additions & 0 deletions packages/dex-widget/src/getDomain.ts
Original file line number Diff line number Diff line change
@@ -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<void | string> => {
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]);
}
1 change: 1 addition & 0 deletions packages/dex-widget/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './themeUtils';
export * from './types';
export * from './verifyParamsUtils';
export * from './widgetHelp';
export * from './getDomain';
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 (
<>
<FormControl fullWidth>
Expand Down

0 comments on commit 7a6a15f

Please sign in to comment.