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';
|
2024-09-19 03:30:34 +02:00
|
|
|
import DefaultPreference from 'react-native-default-preference';
|
2023-12-27 07:52:11 +01:00
|
|
|
import { BlueButtonLink, BlueCard, BlueLoading, BlueSpacing20, BlueText } from '../../BlueComponents';
|
2020-12-14 21:09:32 +01:00
|
|
|
import DeeplinkSchemaMatch from '../../class/deeplink-schema-match';
|
2024-05-20 11:54:13 +02:00
|
|
|
import { LightningCustodianWallet } from '../../class/wallets/lightning-custodian-wallet';
|
2024-09-19 03:30:34 +02:00
|
|
|
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';
|
2024-09-19 03:30:34 +02:00
|
|
|
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
|
|
|
|
2020-05-24 11:17:26 +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,
|
2020-06-13 22:27:17 +02:00
|
|
|
color: '#81868e',
|
2020-05-24 11:17:26 +02:00
|
|
|
marginHorizontal: 8,
|
|
|
|
minHeight: 36,
|
|
|
|
height: 36,
|
|
|
|
},
|
2020-06-01 14:54:23 +02:00
|
|
|
buttonStyle: {
|
|
|
|
backgroundColor: 'transparent',
|
2021-03-19 03:30:01 +01:00
|
|
|
flexDirection: I18nManager.isRTL ? 'row-reverse' : 'row',
|
2020-06-01 14:54:23 +02:00
|
|
|
},
|
2020-05-24 11:17:26 +02:00
|
|
|
});
|
|
|
|
|
2021-07-20 15:10:45 +02:00
|
|
|
type LightingSettingsRouteProps = RouteProp<
|
|
|
|
{
|
|
|
|
params?: {
|
|
|
|
url?: string;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
'params'
|
|
|
|
>;
|
|
|
|
|
2024-05-12 17:35:49 +02:00
|
|
|
const LightningSettings: React.FC = () => {
|
2021-07-20 15:10:45 +02:00
|
|
|
const params = useRoute<LightingSettingsRouteProps>().params;
|
2020-07-15 19:32:59 +02:00
|
|
|
const [isLoading, setIsLoading] = useState(true);
|
2021-07-20 15:10:45 +02:00
|
|
|
const [URI, setURI] = useState<string>();
|
2021-07-21 14:29:18 +02:00
|
|
|
const { colors } = useTheme();
|
2020-10-26 17:07:48 +01:00
|
|
|
const route = useRoute();
|
2021-10-04 04:53:50 +02:00
|
|
|
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(() => {
|
2024-09-19 03:30:34 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
2020-12-14 21:09:32 +01:00
|
|
|
|
2024-09-19 03:30:34 +02:00
|
|
|
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();
|
2020-12-14 21:09:32 +01:00
|
|
|
}, [params?.url]);
|
2018-11-04 22:21:07 +01:00
|
|
|
|
2021-07-20 15:10:45 +02: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-08-26 05:18:59 +02:00
|
|
|
};
|
2020-07-15 19:32:59 +02:00
|
|
|
const save = useCallback(async () => {
|
|
|
|
setIsLoading(true);
|
|
|
|
try {
|
2024-09-19 03:30:34 +02:00
|
|
|
await DefaultPreference.setName(GROUP_IO_BLUEWALLET);
|
2020-07-15 19:32:59 +02:00
|
|
|
if (URI) {
|
2024-09-19 03:30:34 +02:00
|
|
|
const normalizedURI = new URL(URI.replace(/([^:]\/)\/+/g, '$1')).toString();
|
|
|
|
|
|
|
|
await LightningCustodianWallet.isValidNodeAddress(normalizedURI);
|
|
|
|
|
|
|
|
await setLNDHub(normalizedURI);
|
2021-07-20 15:10:45 +02:00
|
|
|
} else {
|
2024-09-19 03:30:34 +02:00
|
|
|
await clearLNDHub();
|
2021-07-20 15:10:45 +02:00
|
|
|
}
|
2024-09-19 03:30:34 +02:00
|
|
|
|
|
|
|
presentAlert({ message: loc.settings.lightning_saved, type: AlertType.Toast });
|
|
|
|
triggerHapticFeedback(HapticFeedbackTypes.NotificationSuccess);
|
2020-07-15 19:32:59 +02:00
|
|
|
} catch (error) {
|
2024-09-19 03:30:34 +02:00
|
|
|
triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
|
2024-02-07 20:24:24 +01:00
|
|
|
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"
|
2021-07-20 15:10:45 +02:00
|
|
|
// 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>
|
2021-10-04 04:53:50 +02:00
|
|
|
<View style={[styles.uri, styleHook.uri]}>
|
2020-07-15 19:32:59 +02:00
|
|
|
<TextInput
|
|
|
|
value={URI}
|
2023-12-16 00:08:52 +01:00
|
|
|
placeholder={loc.formatString(loc.settings.lndhub_uri, { example: 'https://10.20.30.40:3000' })}
|
2020-08-26 05:18:59 +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 />
|
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;
|