Skip to content

Commit

Permalink
Open store in new tab on failed load, adjust timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
NiftyAndy committed Aug 28, 2024
1 parent af65c3c commit 4ececd7
Showing 1 changed file with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ import Loading from './Loading';
const isAndroid = (userAgent: string) => /.*(Mobile|Android).*/.test(userAgent);
const isIOS = (userAgent: string) => /.*(iPhone|iPad|iPod).*/.test(userAgent);

const redirectToAppStore = (userAgent: string, refcode: string) => {
const redirectToAppStore = (userAgent: string, refcode: string, newTab = false) => {
let appStoreURL = 'https://niftysmashers.com';
if (isAndroid(userAgent)) appStoreURL = `https://niftysmashers.com/android`;
if (isIOS(userAgent)) appStoreURL = `https://niftysmashers.com/ios`;
window.location.href = `${appStoreURL}/?referral=${refcode}`;
if (newTab) window.open(`${appStoreURL}/?referral=${refcode}`, '_blank');
else window.location.href = `${appStoreURL}/?referral=${refcode}`;
};

let isPopupOpen = false;

const onBlurHandler = () => {
const loadingMsgElement = document.getElementById('loading-message');
if (loadingMsgElement) loadingMsgElement.innerText = ' onblur';
isPopupOpen = true;
};

// Attempt to launch App if installed. Fallback to App Store after a timeout
Expand All @@ -19,17 +28,23 @@ const redirectToNativeApp = (userAgent: string, refcode: string) => {

const clearTimeoutHandler = () => {
clearTimeout(timeoutId);
setTimeout(() => redirectToAppStore(userAgent, refcode), 10000);
const loadingMsgElement = document.getElementById('loading-message');
if (loadingMsgElement) loadingMsgElement.innerText = `timeout cleared, isPopupOpen: ${isPopupOpen}`;
if (!isAndroid(userAgent) || !isPopupOpen) setTimeout(() => redirectToAppStore(userAgent, refcode, true), 8000);
window.removeEventListener('beforeunload', clearTimeoutHandler);
// window.removeEventListener('blur', clearTimeoutHandler);
// window.removeEventListener('blur', onBlurHandler);
};

// Clear the timeout if the page unloads (app opens) or blurred (popup opens)
window.addEventListener('beforeunload', clearTimeoutHandler);
// window.addEventListener('blur', clearTimeoutHandler);
window.addEventListener('blur', onBlurHandler);

// Attempt to launch the app
window.location.href = `nifty://niftysmashers/invite?profile=${refcode}`;

if (window.confirm(`Open in App Store?`)) {
redirectToAppStore(userAgent, refcode, true);
}
};

type RequestParams = { game: string; refcode: string };
Expand Down

0 comments on commit 4ececd7

Please sign in to comment.