BlueWallet/screen/settings/LightningSettings.tsx

188 lines
6.0 KiB
TypeScript
Raw Normal View History

2024-05-20 11:54:13 +02:00
import React, { useCallback, useEffect, useState } from 'react';
2024-05-27 04:03:35 +02:00
import { RouteProp, useRoute } from '@react-navigation/native';
2024-05-20 11:54:13 +02:00
import { Alert, I18nManager, Linking, ScrollView, StyleSheet, TextInput, View } from 'react-native';
2024-06-12 18:46:44 +02:00
import { Button as ButtonRNElements } from '@rneui/themed';
import DefaultPreference from 'react-native-default-preference';
import { BlueButtonLink, BlueCard, BlueLoading, BlueSpacing20, BlueText } from '../../BlueComponents';
import DeeplinkSchemaMatch from '../../class/deeplink-schema-match';
2024-05-20 11:54:13 +02:00
import { LightningCustodianWallet } from '../../class/wallets/lightning-custodian-wallet';
import presentAlert, { AlertType } from '../../components/Alert';
2023-11-15 13:07:54 +01:00
import { Button } from '../../components/Button';
2024-05-20 11:54:13 +02:00
import { useTheme } from '../../components/themes';
2024-05-27 04:03:35 +02:00
import { scanQrHelper } from '../../helpers/scan-qr';
2024-05-20 11:54:13 +02:00
import loc from '../../loc';
import triggerHapticFeedback, { HapticFeedbackTypes } from '../../blue_modules/hapticFeedback';
import { GROUP_IO_BLUEWALLET } from '../../blue_modules/currency';
import { clearLNDHub, getLNDHub, setLNDHub } from '../../helpers/lndHub';
2023-04-21 17:39:12 +02:00
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',
2021-03-19 03:30:01 +01:00
flexDirection: I18nManager.isRTL ? 'row-reverse' : 'row',
},
});
type LightingSettingsRouteProps = RouteProp<
{
params?: {
url?: string;
};
},
'params'
>;
2024-05-12 17:35:49 +02:00
const LightningSettings: React.FC = () => {
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 styleHook = StyleSheet.create({
uri: {
borderColor: colors.formBorder,
borderBottomColor: colors.formBorder,
backgroundColor: colors.inputBackgroundColor,
},
});
2018-11-04 22:21:07 +01:00
2020-07-15 19:32:59 +02:00
useEffect(() => {
const fetchURI = async () => {
try {
// Try fetching from DefaultPreference first as DefaultPreference uses truly native storage
const value = await getLNDHub();
setURI(value ?? undefined);
} catch (error) {
console.log(error);
}
};
const initialize = async () => {
setIsLoading(true);
await fetchURI().finally(() => {
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 },
);
}
});
};
// Call the initialize function
initialize();
}, [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 {
await DefaultPreference.setName(GROUP_IO_BLUEWALLET);
2020-07-15 19:32:59 +02:00
if (URI) {
const normalizedURI = new URL(URI.replace(/([^:]\/)\/+/g, '$1')).toString();
await LightningCustodianWallet.isValidNodeAddress(normalizedURI);
await setLNDHub(normalizedURI);
} else {
await clearLNDHub();
}
presentAlert({ message: loc.settings.lightning_saved, type: AlertType.Toast });
triggerHapticFeedback(HapticFeedbackTypes.NotificationSuccess);
2020-07-15 19:32:59 +02:00
} catch (error) {
triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
presentAlert({ message: 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 = () => {
2024-05-27 04:03:35 +02:00
scanQrHelper(route.name).then(data => {
if (data) {
setLndhubURI(data);
}
});
2020-10-26 17:07:48 +01:00
};
2020-07-15 19:32:59 +02:00
return (
2024-01-13 15:56:29 +01:00
<ScrollView automaticallyAdjustContentInsets contentInsetAdjustmentBehavior="automatic">
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
2023-11-15 13:07:54 +01:00
<ButtonRNElements
2020-07-15 19:32:59 +02:00
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, styleHook.uri]}>
2020-07-15 19:32:59 +02:00
<TextInput
value={URI}
placeholder={loc.formatString(loc.settings.lndhub_uri, { example: 'https://10.20.30.40:3000' })}
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 />
2023-11-15 14:05:23 +01:00
{isLoading ? <BlueLoading /> : <Button testID="Save" onPress={save} title={loc.settings.save} />}
2020-07-15 19:32:59 +02:00
</BlueCard>
2024-01-13 15:56:29 +01:00
</ScrollView>
2020-07-15 19:32:59 +02:00
);
2018-11-04 22:21:07 +01:00
};
2020-07-15 19:32:59 +02:00
export default LightningSettings;