BlueWallet/screen/settings/lightningSettings.tsx

178 lines
5.6 KiB
TypeScript
Raw Normal View History

/* global alert */
2020-07-15 19:32:59 +02:00
import React, { useState, useEffect, useCallback } from 'react';
2021-03-19 03:30:01 +01:00
import { View, TextInput, Linking, StyleSheet, Alert, I18nManager } from 'react-native';
2020-07-20 15:38:46 +02:00
import { Button } from 'react-native-elements';
import { useNavigation, useRoute, RouteProp } from '@react-navigation/native';
2020-12-16 04:15:57 +01:00
import AsyncStorage from '@react-native-async-storage/async-storage';
2020-12-25 17:09:53 +01:00
import navigationStyle, { NavigationOptionsGetter } from '../../components/navigationStyle';
2020-12-25 17:09:53 +01:00
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 15:38:46 +02:00
import loc from '../../loc';
import { BlueCurrentTheme, useTheme } from '../../components/themes';
import DeeplinkSchemaMatch from '../../class/deeplink-schema-match';
import { isTorCapable } from '../../blue_modules/environment';
2020-07-15 19:32:59 +02:00
const styles = StyleSheet.create({
uri: {
flexDirection: 'row',
2020-07-15 19:32:59 +02:00
borderColor: BlueCurrentTheme.colors.formBorder,
borderBottomColor: BlueCurrentTheme.colors.formBorder,
borderWidth: 1,
borderBottomWidth: 0.5,
2020-07-15 19:32:59 +02: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',
2021-03-19 03:30:01 +01:00
flexDirection: I18nManager.isRTL ? 'row-reverse' : 'row',
},
2021-04-23 13:29:45 +02:00
torSupported: {
color: '#81868e',
},
});
type LightingSettingsRouteProps = RouteProp<
{
params?: {
url?: string;
};
},
'params'
>;
const LightningSettings: React.FC & { navigationOptions: NavigationOptionsGetter } = () => {
const params = useRoute<LightingSettingsRouteProps>().params;
2020-07-15 19:32:59 +02:00
const [isLoading, setIsLoading] = useState(true);
const [URI, setURI] = useState<string>();
const { colors } = useTheme();
2020-10-26 17:07:48 +01:00
const route = useRoute();
const navigation = useNavigation();
2018-11-04 22:21:07 +01:00
2020-07-15 19:32:59 +02:00
useEffect(() => {
AsyncStorage.getItem(AppStorage.LNDHUB)
.then(value => setURI(value ?? undefined))
2020-07-15 19:32:59 +02:00
.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]);
2018-11-04 22:21:07 +01:00
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());
};
2020-07-15 19:32:59 +02: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 22:21:07 +01:00
}
if (URI) {
await AsyncStorage.setItem(AppStorage.LNDHUB, URI);
} else {
await AsyncStorage.removeItem(AppStorage.LNDHUB);
}
2020-07-20 15:38:46 +02:00
alert(loc.settings.lightning_saved);
2020-07-15 19:32:59 +02:00
} catch (error) {
2020-07-20 15:38:46 +02:00
alert(loc.settings.lightning_error_lndhub_uri);
2020-07-15 19:32:59 +02:00
console.log(error);
}
setIsLoading(false);
}, [URI]);
2018-11-04 22:21:07 +01:00
2020-10-26 17:07:48 +01:00
const importScan = () => {
navigation.navigate('ScanQRCodeRoot', {
screen: 'ScanQRCode',
params: {
launchedBy: route.name,
onBarScanned: setLndhubURI,
showFileImportButton: true,
},
});
};
2020-07-15 19:32:59 +02:00
return (
<SafeBlueArea>
2020-07-15 19:32:59 +02:00
<BlueCard>
2020-11-22 09:04:04 +01:00
<BlueText>{loc.settings.lightning_settings_explain}</BlueText>
2020-07-15 19:32:59 +02:00
</BlueCard>
2018-12-26 23:18:01 +01:00
2020-07-15 19:32:59 +02:00
<Button
icon={{
name: 'github',
type: 'font-awesome',
color: colors.foregroundColor,
}}
2020-07-20 15:38:46 +02:00
onPress={() => Linking.openURL('https://github.com/BlueWallet/LndHub')}
2020-07-15 19:32:59 +02:00
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}
2020-07-15 19:32:59 +02:00
buttonStyle={styles.buttonStyle}
/>
2018-11-04 22:21:07 +01:00
2020-07-15 19:32:59 +02:00
<BlueCard>
<View style={styles.uri}>
<TextInput
value={URI}
2021-07-08 19:39:03 +02:00
placeholder={
loc.formatString(loc.settings.electrum_host, { example: '111.222.333.111' }) +
(isTorCapable ? ' (' + loc.settings.tor_supported + ')' : '')
2021-07-08 19:39:03 +02:00
}
onChangeText={setLndhubURI}
2020-07-15 19:32:59 +02:00
numberOfLines={1}
style={styles.uriText}
placeholderTextColor="#81868e"
editable={!isLoading}
textContentType="URL"
autoCapitalize="none"
2020-08-26 05:22:45 +02:00
autoCorrect={false}
2020-07-15 19:32:59 +02:00
underlineColorAndroid="transparent"
2021-03-01 11:20:01 +01:00
testID="URIInput"
2020-07-15 19:32:59 +02:00
/>
</View>
2021-04-26 01:15:52 +02:00
<BlueSpacing20 />
2021-03-01 11:20:01 +01:00
<BlueButtonLink title={loc.wallets.import_scan_qr} testID="ImportScan" onPress={importScan} />
2020-07-15 19:32:59 +02:00
<BlueSpacing20 />
2021-03-01 11:20:01 +01:00
{isLoading ? <BlueLoading /> : <BlueButton testID="Save" onPress={save} title={loc.settings.save} />}
2020-07-15 19:32:59 +02:00
</BlueCard>
</SafeBlueArea>
);
2018-11-04 22:21:07 +01:00
};
2020-07-15 19:32:59 +02:00
2021-02-15 09:03:54 +01:00
LightningSettings.navigationOptions = navigationStyle({}, opts => ({ ...opts, title: loc.settings.lightning_settings }));
2020-12-25 17:09:53 +01:00
2020-07-15 19:32:59 +02:00
export default LightningSettings;