Skip to content

Instantly share code, notes, and snippets.

@irzhywau
Created January 5, 2022 13:09
Show Gist options
  • Save irzhywau/5ece1ba6eec5863ef353e82471d31050 to your computer and use it in GitHub Desktop.
Save irzhywau/5ece1ba6eec5863ef353e82471d31050 to your computer and use it in GitHub Desktop.
dapps browser connect attempts
// we are in an create-react-app context
export default () => {
const [mm, setSupport] = React.useState<MetamaskSupport | null>(null);
React.useEffect(() => {
let alreadyHandled = false;
// as the provider may be not available all the time on mobile, we need this side-effect implementation
// see https://docs.metamask.io/guide/mobile-best-practices.html#the-provider-window-ethereum
// also see https://github.com/MetaMask/detect-provider
const handleEthereum = () => {
if (alreadyHandled) {
return;
}
const { ethereum, elastos } = window;
setSupport({
supported: Boolean(ethereum),
elastos: Boolean(elastos),
chainId: parseInt(ethereum?.chainId || '0x0', 16),
});
// eslint-disable-next-line no-alert
alert(JSON.stringify({
supported: Boolean(ethereum),
elastos: Boolean(elastos),
chainId: parseInt(ethereum?.chainId || '0x0', 16),
}));
alreadyHandled = true;
};
window.addEventListener('ethereum#initialized', handleEthereum);
setTimeout(handleEthereum, 3000);
return () => {
window.removeEventListener('ethereum#initialized', handleEthereum);
};
}, []);
// ...
}
@irzhywau
Copy link
Author

irzhywau commented Jan 5, 2022

the result is not the same when I browse https://staging.ela.city and directly to the IP of the same server with the application port

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment