Skip to content

Commit

Permalink
Merge pull request #26884 from software-mansion-labs/ts-migration/kow…
Browse files Browse the repository at this point in the history
…czarz/async-open-url-lib

[No QA][TS migration] Migrate 'asyncOpenURL' lib to TypeScript
  • Loading branch information
NikkiWines authored Sep 14, 2023
2 parents 62b8e71 + 39fe279 commit c413f70
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import {Linking} from 'react-native';
import AsyncOpenURL from './types';

export default function asyncOpenURL(promise, url) {
const asyncOpenURL: AsyncOpenURL = (promise, url) => {
if (!url) {
return;
}

promise.then((params) => {
Linking.openURL(typeof url === 'string' ? url : url(params));
});
}
};

export default asyncOpenURL;
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import {Linking} from 'react-native';
import AsyncOpenURL from './types';

/**
* Prevents Safari from blocking pop-up window when opened within async call.
*
* @param {Promise} promise
* @param {string} url
* @param {Boolean} shouldSkipCustomSafariLogic When true, we will use `Linking.openURL` even if the browser is Safari.
* @param shouldSkipCustomSafariLogic When true, we will use `Linking.openURL` even if the browser is Safari.
*/
export default function asyncOpenURL(promise, url, shouldSkipCustomSafariLogic) {
const asyncOpenURL: AsyncOpenURL = (promise, url, shouldSkipCustomSafariLogic) => {
if (!url) {
return;
}
Expand All @@ -22,8 +20,13 @@ export default function asyncOpenURL(promise, url, shouldSkipCustomSafariLogic)
const windowRef = window.open();
promise
.then((params) => {
if (!windowRef) {
return;
}
windowRef.location = typeof url === 'string' ? url : url(params);
})
.catch(() => windowRef.close());
.catch(() => windowRef?.close());
}
}
};

export default asyncOpenURL;
3 changes: 3 additions & 0 deletions src/libs/asyncOpenURL/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type AsyncOpenURL = <T>(promise: Promise<T>, url: string | ((params: T) => string), shouldSkipCustomSafariLogic?: boolean) => void;

export default AsyncOpenURL;

0 comments on commit c413f70

Please sign in to comment.