Skip to content

Commit

Permalink
fix(ui): safari desktop deeplink flow improved
Browse files Browse the repository at this point in the history
  • Loading branch information
siandreev authored and thekiba committed Dec 19, 2023
1 parent c9b1bcd commit 7c10b17
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 45 deletions.
8 changes: 4 additions & 4 deletions packages/ui/src/app/utils/url-strategy-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function redirectToTelegram(
): void {
options = { ...options };
// TODO: Remove this line after all dApps and the wallets-list.json have been updated
const directLink = convertToDirectLink(universalLink);
const directLink = convertToTGDirectLink(universalLink);
const directLinkUrl = new URL(directLink);

if (!directLinkUrl.searchParams.has('startapp')) {
Expand Down Expand Up @@ -117,7 +117,7 @@ export function redirectToTelegram(
openLinkBlank(addReturnStrategy(directLinkUrl.toString(), options));
} else {
const link = addReturnStrategy(directLinkUrl.toString(), options);
const deepLink = convertToDeepLink(link);
const deepLink = convertToTGDeepLink(link);

openDeeplinkWithFallback(deepLink, () => openLinkBlank(link));
}
Expand Down Expand Up @@ -146,7 +146,7 @@ function addQueryParameter(url: string, key: string, value: string): string {
* @param universalLink
* TODO: Remove this method after all dApps and the wallets-list.json have been updated
*/
function convertToDirectLink(universalLink: string): string {
function convertToTGDirectLink(universalLink: string): string {
const url = new URL(universalLink);

if (url.searchParams.has('attach')) {
Expand All @@ -161,7 +161,7 @@ function convertToDirectLink(universalLink: string): string {
* Converts a direct link to a deep link.
* @param directLink
*/
function convertToDeepLink(directLink: string): string {
function convertToTGDeepLink(directLink: string): string {
const parsed = new URL(directLink);
const [, domain, appname] = parsed.pathname.split('/');
const startapp = parsed.searchParams.get('startapp');
Expand Down
31 changes: 1 addition & 30 deletions packages/ui/src/app/utils/web-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function openDeeplinkWithFallback(href: string, fallback: () => void): vo

fallback();
};
const fallbackTimeout = setTimeout(() => doFallback(), 1000);
const fallbackTimeout = setTimeout(() => doFallback(), 200);
window.addEventListener('blur', () => clearTimeout(fallbackTimeout), { once: true });

openLink(href, '_self');
Expand Down Expand Up @@ -240,32 +240,3 @@ export function toDeeplink(universalLink: string, deeplink: string): string {
const url = new URL(universalLink);
return deeplink + url.search;
}

/**
* Try to open deeplink with given fallback universal link that is opened if none app supports the deeplink
* @param deeplink
* @param universalLink
* @param options
*/
export function openDeeplinkWithUniversalFallback(deeplink: string, universalLink: string, options?: { notBlank?: boolean; onFallbackRun?: () => void }): void {
let blurred = false;
function blurHandler(): void {
blurred = true;
window.removeEventListener('blur', blurHandler);
}

window.addEventListener('blur', blurHandler);

openLink(deeplink);

setTimeout(() => {
if (!blurred) {
if (options?.notBlank) {
openLink(universalLink);
} else {
openLinkBlank(universalLink);
}
}
window.removeEventListener('blur', blurHandler);
}, 100);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ import {
} from 'src/app/components';
import { appState } from 'src/app/state/app.state';
import {
openDeeplinkWithUniversalFallback,
isBrowser,
openDeeplinkWithFallback,
openLinkBlank,
toDeeplink
} from 'src/app/utils/web-api';
Expand All @@ -61,6 +62,8 @@ export interface DesktopConnectionProps {
onBackClick: () => void;
}

let openDesktopDeeplinkAttempts = 0;

export const DesktopConnectionModal: Component<DesktopConnectionProps> = props => {
const [mode, setMode] = createSignal<'mobile' | 'desktop' | 'extension'>('mobile');
const [connectionErrored, setConnectionErrored] = createSignal(false);
Expand Down Expand Up @@ -121,26 +124,29 @@ export const DesktopConnectionModal: Component<DesktopConnectionProps> = props =

setMode('desktop');
const linkWithStrategy = addReturnStrategy(universalLink()!, appState.returnStrategy);
if (props.wallet.deepLink) {

// check because safari doesn't support deeplinks fallbacks. Ignore deeplinks in safari after first failed attempt
const haveTriedToOpenDeeplinkInSafari = isBrowser('safari') && openDesktopDeeplinkAttempts >= 1;
if (props.wallet.deepLink && !haveTriedToOpenDeeplinkInSafari) {
openDesktopDeeplinkAttempts++;
setLastSelectedWalletInfo({
...props.wallet,
openMethod: 'custom-deeplink'
});

openDeeplinkWithUniversalFallback(toDeeplink(linkWithStrategy, props.wallet.deepLink), linkWithStrategy, {
onFallbackRun: () => {
setLastSelectedWalletInfo({
...props.wallet,
openMethod: 'universal-link'
});
}
});
openDeeplinkWithFallback(toDeeplink(linkWithStrategy, props.wallet.deepLink), () => {
setLastSelectedWalletInfo({
...props.wallet,
openMethod: 'universal-link'
});
openLinkBlank(linkWithStrategy);
})
} else {
setLastSelectedWalletInfo({
...props.wallet,
openMethod: 'universal-link'
});
openLinkBlank(addReturnStrategy(universalLink()!, appState.returnStrategy));
openLinkBlank(linkWithStrategy);
}
};

Expand Down

0 comments on commit 7c10b17

Please sign in to comment.