Rocket.Chat.ReactNative/app/lib/methods/helpers/openLink.ts

66 lines
1.8 KiB
TypeScript
Raw Normal View History

import { Linking } from 'react-native';
import * as WebBrowser from 'expo-web-browser';
import parse from 'url-parse';
import { themes } from '../../constants';
import { TSupportedThemes } from '../../../theme';
import UserPreferences from '../userPreferences';
import ensureSecureProtocol from './ensureSecureProtocol';
export const DEFAULT_BROWSER_KEY = 'DEFAULT_BROWSER_KEY';
const scheme = {
chrome: 'googlechrome:',
chromeSecure: 'googlechromes:',
firefox: 'firefox:',
brave: 'brave:'
};
const appSchemeURL = (url: string, browser: string): string => {
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: string, theme: TSupportedThemes = 'light'): Promise<void> => {
url = ensureSecureProtocol(url);
try {
Chore: Update react-native-mmkv-storage to 0.6.12 (#3634) * chore: updating mmkv library * feat: updating android ejson to use the getSecureKey new version * feat: updating IOS files to use the getSecureKey new version * feat: changing all mmkv methods to use sync calls * feat: changing mmkv methods from Screen lock * feat: changing all mmkv methods from login, ssl certificate and change/add server * feat: changing all mmkv methods from login, ssl certificate and change/add server * feat: changing all mmkv methods from logout * feat: changing all mmkv methods from e2e * fix: small fix at encryption and server drop down * feat: changing all mmkv methods from set theme * feat: changing all mmkv methods from openLink * fix: setting up mmkv to works property on Android * fix: fix an error to set the theme when open the app * refactor: change the react-native branch (temporary) * refactor: removing all `Async` from mmkv functions name * refactor: removing await from unnecessary functions, removing console.log and update cocoapods * refactor: removing unnecessary undefined from methods * feat: creating a custom hook for mmkv * refactor: changing the fetchPasscode to use the useUserPreferences hook * refactor: changing setTheme from app/index * refactor: small fix on setTheme * chore: update mmkv to 0.6.11 * chore: update mmkv to 0.6.11 * chore: minor tweak * chore: update mmkv to 0.6.12 * chore: mock NativeModules * chore: fix test mmkv * chore: removing custom MMKV JSI module, since is no more necessary after 0.6.11 version * feat: removing some async calls from mmkv after update from develop * feat: creating a function to get the initialTheme * feat: removing unnecessary try/catch * fix: fixing the blink white when open the app * feat: changing useMMKVStorage to create from mmkv lib * test: creating a mock for mmkv create function * chore: fix errors on tablet * minor tweak
2022-03-09 19:41:26 +00:00
const browser = UserPreferences.getString(DEFAULT_BROWSER_KEY);
if (browser === 'inApp') {
await WebBrowser.openBrowserAsync(url, {
toolbarColor: themes[theme].headerBackground,
controlsColor: themes[theme].headerTintColor,
// https://github.com/expo/expo/pull/4923
enableBarCollapsing: true,
showTitle: true
});
} else {
const schemeUrl = appSchemeURL(url, browser!.replace(':', ''));
await Linking.openURL(schemeUrl);
}
} catch {
try {
await Linking.openURL(url);
} catch {
// do nothing
}
}
};
export default openLink;