import { Platform , Linking , Alert } from 'react-native' ; import { appName } from '@frogpond/constants' ; import SafariView from 'react-native-safari-view' ; import { CustomTabs } from 'react-native-custom-tabs' ; function genericOpen ( url ) { return Linking . canOpenURL ( url ) . then ( ( isSupported ) => { if ( ! isSupported ) { console . warn ( 'cannot handle' , url ) ; } return Linking . openURL ( url ) ; } ) . catch ( ( err ) => { console . error ( err ) ; } ) ; } function iosOpen ( url ) { return SafariView . isAvailable ( ) . then ( ( ) => SafariView . show ( { url } ) ) . catch ( ( ) => genericOpen ( url ) ) ; } function androidOpen ( url ) { return CustomTabs . openURL ( url , { showPageTitle : true , enableUrlBarHiding : true , enableDefaultShare : true , } ) . catch ( ( ) => genericOpen ( url ) ) ; } export function openUrl ( url ) { let protocol = /^(.*?):/u . exec ( url ) ; if ( protocol && protocol . length ) { switch ( protocol [ 1 ] ) { case 'tel' : return genericOpen ( url ) ; case 'mailto' : return genericOpen ( url ) ; default : break ; } } switch ( Platform . OS ) { case 'android' : return androidOpen ( url ) ; case 'ios' : return iosOpen ( url ) ; default : return genericOpen ( url ) ; } } export function trackedOpenUrl ( { url } ) { return openUrl ( url ) ; } export function canOpenUrl ( url ) { if ( /^(?:about|data):/u . test ( url ) ) { return false ; } return true ; } export function openUrlInBrowser ( { url } ) { return promptConfirm ( url ) ; } function promptConfirm ( url ) { let app = appName ( ) ; let title = `Leaving ${ app }` ; let detail = `A web page will be opened in a browser outside of ${ app }.` ; Alert . alert ( title , detail , [ { text : 'Cancel' , onPress : ( ) => { } } , { text : 'Open' , onPress : ( ) => genericOpen ( url ) } , ] ) ; }