2020-02-19 20:52:05 +00:00
|
|
|
import { Linking } from 'react-native';
|
2019-06-21 16:39:20 +00:00
|
|
|
import * as WebBrowser from 'expo-web-browser';
|
2020-02-19 20:52:05 +00:00
|
|
|
import parse from 'url-parse';
|
2019-06-21 16:39:20 +00:00
|
|
|
|
2020-08-19 17:14:22 +00:00
|
|
|
import UserPreferences from '../lib/userPreferences';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../constants/colors';
|
2019-06-21 16:39:20 +00:00
|
|
|
|
2020-02-19 20:52:05 +00:00
|
|
|
export const DEFAULT_BROWSER_KEY = 'DEFAULT_BROWSER_KEY';
|
|
|
|
|
|
|
|
const scheme = {
|
|
|
|
chrome: 'googlechrome:',
|
|
|
|
chromeSecure: 'googlechromes:',
|
|
|
|
firefox: 'firefox:',
|
|
|
|
brave: 'brave:'
|
|
|
|
};
|
|
|
|
|
|
|
|
const appSchemeURL = (url, browser) => {
|
|
|
|
let schemeUrl = url;
|
|
|
|
const parsedUrl = parse(url, true);
|
|
|
|
const { protocol } = parsedUrl;
|
|
|
|
const isSecure = ['https:'].includes(protocol);
|
|
|
|
|
|
|
|
if (browser === 'googlechrome') {
|
|
|
|
if (!isSecure) {
|
|
|
|
schemeUrl = url.replace(protocol, scheme.chrome);
|
|
|
|
} else {
|
|
|
|
schemeUrl = url.replace(protocol, scheme.chromeSecure);
|
|
|
|
}
|
|
|
|
} else if (browser === 'firefox') {
|
|
|
|
schemeUrl = `${ scheme.firefox }//open-url?url=${ url }`;
|
|
|
|
} else if (browser === 'brave') {
|
|
|
|
schemeUrl = `${ scheme.brave }//open-url?url=${ url }`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return schemeUrl;
|
|
|
|
};
|
|
|
|
|
|
|
|
const openLink = async(url, theme = 'light') => {
|
|
|
|
try {
|
2020-08-19 17:14:22 +00:00
|
|
|
const browser = await UserPreferences.getStringAsync(DEFAULT_BROWSER_KEY);
|
2020-02-19 20:52:05 +00:00
|
|
|
|
2021-07-12 19:19:25 +00:00
|
|
|
if (browser === 'inApp') {
|
2020-02-19 20:52:05 +00:00
|
|
|
await WebBrowser.openBrowserAsync(url, {
|
|
|
|
toolbarColor: themes[theme].headerBackground,
|
|
|
|
controlsColor: themes[theme].headerTintColor,
|
|
|
|
collapseToolbar: true,
|
|
|
|
showTitle: true
|
|
|
|
});
|
2021-07-12 19:19:25 +00:00
|
|
|
} else {
|
|
|
|
const schemeUrl = appSchemeURL(url, browser.replace(':', ''));
|
|
|
|
await Linking.openURL(schemeUrl);
|
2020-02-19 20:52:05 +00:00
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
try {
|
|
|
|
await Linking.openURL(url);
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-06-21 16:39:20 +00:00
|
|
|
|
|
|
|
export default openLink;
|