BlueWallet/screen/settings/lightningSettings.js

160 lines
4.9 KiB
JavaScript
Raw Normal View History

/* global alert */
2020-07-15 13:32:59 -04:00
import React, { useState, useEffect, useCallback } from 'react';
import { View, TextInput, Linking, StyleSheet, Alert } from 'react-native';
2020-07-20 16:38:46 +03:00
import { Button } from 'react-native-elements';
2020-10-26 18:07:48 +02:00
import { useTheme, useNavigation, useRoute } from '@react-navigation/native';
2020-12-15 22:15:57 -05:00
import AsyncStorage from '@react-native-async-storage/async-storage';
2020-12-25 19:09:53 +03:00
import navigationStyle from '../../components/navigationStyle';
import { BlueButton, BlueButtonLink, BlueCard, BlueLoading, BlueSpacing20, BlueText, SafeBlueArea } from '../../BlueComponents';
import { AppStorage } from '../../class';
import { LightningCustodianWallet } from '../../class/wallets/lightning-custodian-wallet';
2020-07-20 16:38:46 +03:00
import loc from '../../loc';
2020-07-15 13:32:59 -04:00
import { BlueCurrentTheme } from '../../components/themes';
import DeeplinkSchemaMatch from '../../class/deeplink-schema-match';
2020-07-15 13:32:59 -04:00
const styles = StyleSheet.create({
root: {
flex: 1,
},
uri: {
flexDirection: 'row',
2020-07-15 13:32:59 -04:00
borderColor: BlueCurrentTheme.colors.formBorder,
borderBottomColor: BlueCurrentTheme.colors.formBorder,
borderWidth: 1,
borderBottomWidth: 0.5,
2020-07-15 13:32:59 -04:00
backgroundColor: BlueCurrentTheme.colors.inputBackgroundColor,
minHeight: 44,
height: 44,
alignItems: 'center',
borderRadius: 4,
},
uriText: {
flex: 1,
color: '#81868e',
marginHorizontal: 8,
minHeight: 36,
height: 36,
},
buttonStyle: {
backgroundColor: 'transparent',
},
});
2020-07-15 13:32:59 -04:00
const LightningSettings = () => {
const params = useRoute().params;
2020-07-15 13:32:59 -04:00
const [isLoading, setIsLoading] = useState(true);
const [URI, setURI] = useState();
const { colors } = useTheme();
2020-10-26 18:07:48 +02:00
const route = useRoute();
const navigation = useNavigation();
2018-11-04 21:21:07 +00:00
2020-07-15 13:32:59 -04:00
useEffect(() => {
AsyncStorage.getItem(AppStorage.LNDHUB)
.then(setURI)
.then(() => setIsLoading(false))
.catch(() => setIsLoading(false));
if (params?.url) {
Alert.alert(
loc.formatString(loc.settings.set_lndhub_as_default, { url: params?.url }),
'',
[
{
text: loc._.ok,
onPress: () => {
setLndhubURI(params?.url);
},
style: 'default',
},
{ text: loc._.cancel, onPress: () => {}, style: 'cancel' },
],
{ cancelable: false },
);
}
}, [params?.url]);
2018-11-04 21:21:07 +00:00
const setLndhubURI = value => {
if (DeeplinkSchemaMatch.getUrlFromSetLndhubUrlAction(value)) {
// in case user scans a QR with a deeplink like `bluewallet:setlndhuburl?url=https%3A%2F%2Flndhub.herokuapp.com`
value = DeeplinkSchemaMatch.getUrlFromSetLndhubUrlAction(value);
}
setURI(value.trim());
};
2020-07-15 13:32:59 -04:00
const save = useCallback(async () => {
setIsLoading(true);
try {
if (URI) {
await LightningCustodianWallet.isValidNodeAddress(URI);
// validating only if its not empty. empty means use default
2018-11-04 21:21:07 +00:00
}
2020-07-15 13:32:59 -04:00
await AsyncStorage.setItem(AppStorage.LNDHUB, URI);
2020-07-20 16:38:46 +03:00
alert(loc.settings.lightning_saved);
2020-07-15 13:32:59 -04:00
} catch (error) {
2020-07-20 16:38:46 +03:00
alert(loc.settings.lightning_error_lndhub_uri);
2020-07-15 13:32:59 -04:00
console.log(error);
}
setIsLoading(false);
}, [URI]);
2018-11-04 21:21:07 +00:00
2020-10-26 18:07:48 +02:00
const importScan = () => {
navigation.navigate('ScanQRCodeRoot', {
screen: 'ScanQRCode',
params: {
launchedBy: route.name,
onBarScanned: setLndhubURI,
showFileImportButton: true,
},
});
};
2020-07-15 13:32:59 -04:00
return (
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={styles.root}>
<BlueCard>
2020-11-22 03:04:04 -05:00
<BlueText>{loc.settings.lightning_settings_explain}</BlueText>
2020-07-15 13:32:59 -04:00
</BlueCard>
2018-12-26 22:18:01 +00:00
2020-07-15 13:32:59 -04:00
<Button
icon={{
name: 'github',
type: 'font-awesome',
color: colors.foregroundColor,
}}
2020-07-20 16:38:46 +03:00
onPress={() => Linking.openURL('https://github.com/BlueWallet/LndHub')}
2020-07-15 13:32:59 -04:00
titleStyle={{ color: colors.buttonAlternativeTextColor }}
title="github.com/BlueWallet/LndHub"
color={colors.buttonTextColor}
buttonStyle={styles.buttonStyle}
/>
2018-11-04 21:21:07 +00:00
2020-07-15 13:32:59 -04:00
<BlueCard>
<View style={styles.uri}>
<TextInput
placeholder={LightningCustodianWallet.defaultBaseUri}
value={URI}
onChangeText={setLndhubURI}
2020-07-15 13:32:59 -04:00
numberOfLines={1}
style={styles.uriText}
placeholderTextColor="#81868e"
editable={!isLoading}
textContentType="URL"
autoCapitalize="none"
2020-08-25 23:22:45 -04:00
autoCorrect={false}
2020-07-15 13:32:59 -04:00
underlineColorAndroid="transparent"
2021-03-01 13:20:01 +03:00
testID="URIInput"
2020-07-15 13:32:59 -04:00
/>
</View>
2018-11-04 21:21:07 +00:00
2021-03-01 13:20:01 +03:00
<BlueButtonLink title={loc.wallets.import_scan_qr} testID="ImportScan" onPress={importScan} />
2020-07-15 13:32:59 -04:00
<BlueSpacing20 />
2021-03-01 13:20:01 +03:00
{isLoading ? <BlueLoading /> : <BlueButton testID="Save" onPress={save} title={loc.settings.save} />}
2020-07-15 13:32:59 -04:00
</BlueCard>
</SafeBlueArea>
);
2018-11-04 21:21:07 +00:00
};
2020-07-15 13:32:59 -04:00
2021-02-15 11:03:54 +03:00
LightningSettings.navigationOptions = navigationStyle({}, opts => ({ ...opts, title: loc.settings.lightning_settings }));
2020-12-25 19:09:53 +03:00
2020-07-15 13:32:59 -04:00
export default LightningSettings;