import React, { useState, useEffect, useCallback } from 'react'; import { View, TextInput, Linking, StyleSheet, Alert, I18nManager } from 'react-native'; import { Button as ButtonRNElements } from 'react-native-elements'; import { useNavigation, useRoute, RouteProp } from '@react-navigation/native'; import AsyncStorage from '@react-native-async-storage/async-storage'; import navigationStyle, { NavigationOptionsGetter } from '../../components/navigationStyle'; import { BlueButtonLink, BlueCard, BlueLoading, BlueSpacing20, BlueText } from '../../BlueComponents'; import { LightningCustodianWallet } from '../../class/wallets/lightning-custodian-wallet'; import loc from '../../loc'; import { useTheme } from '../../components/themes'; import DeeplinkSchemaMatch from '../../class/deeplink-schema-match'; import alert from '../../components/Alert'; import { requestCameraAuthorization } from '../../helpers/scan-qr'; import { Button } from '../../components/Button'; import SafeArea from '../../components/SafeArea'; const BlueApp = require('../../BlueApp'); const AppStorage = BlueApp.AppStorage; const styles = StyleSheet.create({ uri: { flexDirection: 'row', borderWidth: 1, borderBottomWidth: 0.5, minHeight: 44, height: 44, alignItems: 'center', borderRadius: 4, }, uriText: { flex: 1, color: '#81868e', marginHorizontal: 8, minHeight: 36, height: 36, }, buttonStyle: { backgroundColor: 'transparent', flexDirection: I18nManager.isRTL ? 'row-reverse' : 'row', }, }); type LightingSettingsRouteProps = RouteProp< { params?: { url?: string; }; }, 'params' >; const LightningSettings: React.FC & { navigationOptions: NavigationOptionsGetter } = () => { const params = useRoute().params; const [isLoading, setIsLoading] = useState(true); const [URI, setURI] = useState(); const { colors } = useTheme(); const route = useRoute(); const navigation = useNavigation(); const styleHook = StyleSheet.create({ uri: { borderColor: colors.formBorder, borderBottomColor: colors.formBorder, backgroundColor: colors.inputBackgroundColor, }, }); useEffect(() => { AsyncStorage.getItem(AppStorage.LNDHUB) .then(value => setURI(value ?? undefined)) .then(() => setIsLoading(false)) .catch(() => setIsLoading(false)); if (params?.url) { Alert.alert( loc.formatString(loc.settings.set_lndhub_as_default, { url: params.url }) as string, '', [ { text: loc._.ok, onPress: () => { params?.url && setLndhubURI(params.url); }, style: 'default', }, { text: loc._.cancel, onPress: () => {}, style: 'cancel' }, ], { cancelable: false }, ); } }, [params?.url]); const setLndhubURI = (value: string) => { // in case user scans a QR with a deeplink like `bluewallet:setlndhuburl?url=https%3A%2F%2Flndhub.herokuapp.com` const setLndHubUrl = DeeplinkSchemaMatch.getUrlFromSetLndhubUrlAction(value); setURI(typeof setLndHubUrl === 'string' ? setLndHubUrl.trim() : value.trim()); }; const save = useCallback(async () => { setIsLoading(true); try { if (URI) { await LightningCustodianWallet.isValidNodeAddress(URI); // validating only if its not empty. empty means use default } if (URI) { await AsyncStorage.setItem(AppStorage.LNDHUB, URI); } else { await AsyncStorage.removeItem(AppStorage.LNDHUB); } alert(loc.settings.lightning_saved); } catch (error) { alert(loc.settings.lightning_error_lndhub_uri); console.log(error); } setIsLoading(false); }, [URI]); const importScan = () => { requestCameraAuthorization().then(() => // @ts-ignore: Address types later navigation.navigate('ScanQRCodeRoot', { screen: 'ScanQRCode', params: { launchedBy: route.name, onBarScanned: setLndhubURI, showFileImportButton: true, }, }), ); }; return ( {loc.settings.lightning_settings_explain} Linking.openURL('https://github.com/BlueWallet/LndHub')} titleStyle={{ color: colors.buttonAlternativeTextColor }} title="github.com/BlueWallet/LndHub" // TODO: looks like there's no `color` prop on `Button`, does this make any sense? // color={colors.buttonTextColor} buttonStyle={styles.buttonStyle} /> {isLoading ? :