import React, { useCallback, useEffect, useLayoutEffect, useState } from 'react'; import { FlatList, Linking } from 'react-native'; import { useNavigation } from '@react-navigation/native'; import I18n from '../../i18n'; import StatusBar from '../../containers/StatusBar'; import * as List from '../../containers/List'; import { DEFAULT_BROWSER_KEY } from '../../lib/methods/helpers/openLink'; import { isIOS } from '../../lib/methods/helpers'; import SafeAreaView from '../../containers/SafeAreaView'; import UserPreferences from '../../lib/methods/userPreferences'; import { events, logEvent } from '../../lib/methods/helpers/log'; import Item from './Item'; export type TValue = 'inApp' | 'systemDefault:' | 'googlechrome:' | 'firefox:' | 'brave:'; export interface IBrowsersValues { title: string; value: TValue; } const DEFAULT_BROWSERS: IBrowsersValues[] = [ { title: 'In_app', value: 'inApp' }, { title: isIOS ? 'Safari' : 'Browser', value: 'systemDefault:' } ]; const BROWSERS: IBrowsersValues[] = [ { title: 'Chrome', value: 'googlechrome:' }, { title: 'Firefox', value: 'firefox:' }, { title: 'Brave', value: 'brave:' } ]; const DefaultBrowserView = () => { const [browser, setBrowser] = useState(null); const [supported, setSupported] = useState([]); const navigation = useNavigation(); useLayoutEffect(() => { navigation.setOptions({ title: I18n.t('Default_browser') }); }, [navigation]); useEffect(() => { const getBrowser = UserPreferences.getString(DEFAULT_BROWSER_KEY); setBrowser(getBrowser); if (isIOS) { BROWSERS.forEach(browser => { const { value } = browser; Linking.canOpenURL(value).then(installed => { if (installed) { setSupported(supported => [...supported, browser]); } }); }); } }, []); const changeDefaultBrowser = useCallback((newBrowser: TValue) => { logEvent(events.DB_CHANGE_DEFAULT_BROWSER, { browser: newBrowser }); try { const browser = newBrowser || 'systemDefault:'; UserPreferences.setString(DEFAULT_BROWSER_KEY, browser); setBrowser(browser); } catch { logEvent(events.DB_CHANGE_DEFAULT_BROWSER_F); } }, []); return ( item.value} contentContainerStyle={List.styles.contentContainerStyleFlatList} renderItem={({ item }) => ( )} ListHeaderComponent={ <> } ListFooterComponent={List.Separator} ItemSeparatorComponent={List.Separator} /> ); }; export default DefaultBrowserView;