REF: Implement Suggestions

This commit is contained in:
Stefan Eißler 2024-12-28 20:58:41 +01:00 committed by Overtorment
parent 841f49ceb9
commit 28d9fa1d53

View file

@ -18,6 +18,7 @@ import { useStorage } from '../../hooks/context/useStorage';
import { useExtendedNavigation } from '../../hooks/useExtendedNavigation';
import BigNumber from 'bignumber.js';
import { LightningTransaction } from '../../class/wallets/types';
import dayjs from 'dayjs';
type LNDViewInvoiceRouteParams = {
walletID: string;
@ -69,88 +70,88 @@ const LNDViewInvoice = () => {
setOptions(
isModal
? {
headerStyle: {
backgroundColor: colors.customHeader,
},
gestureEnabled: false,
headerBackVisible: false,
// eslint-disable-next-line react/no-unstable-nested-components
headerRight: () => (
<TouchableOpacity
accessibilityRole="button"
// @ts-ignore - button is not suppprted .
style={styles.button}
onPress={() => {
// @ts-ignore: navigation
navigation?.getParent().pop();
}}
testID="NavigationCloseButton"
>
<Image source={closeImage} />
</TouchableOpacity>
),
}
headerStyle: {
backgroundColor: colors.customHeader,
},
gestureEnabled: false,
headerBackVisible: false,
// eslint-disable-next-line react/no-unstable-nested-components
headerRight: () => (
<TouchableOpacity
accessibilityRole="button"
// @ts-ignore - button is not suppprted .
style={styles.button}
onPress={() => {
// @ts-ignore: navigation
navigation?.getParent().pop();
}}
testID="NavigationCloseButton"
>
<Image source={closeImage} />
</TouchableOpacity>
),
}
: {
headerRight: () => { },
headerStyle: {
backgroundColor: colors.customHeader,
headerRight: () => {},
headerStyle: {
backgroundColor: colors.customHeader,
},
},
},
);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [colors, isModal]);
useEffect(() => {
if (wallet) {
setSelectedWalletID(walletID);
console.log('LNDViewInvoice - useEffect');
if (!invoice.ispaid) {
fetchInvoiceInterval.current = setInterval(async () => {
if (isFetchingInvoices) {
try {
// @ts-ignore - getUserInvoices is not set on TWallet
const userInvoices = await wallet.getUserInvoices(20);
// fetching only last 20 invoices
// for invoice that was created just now - that should be enough (it is basically the last one, so limit=1 would be sufficient)
// but that might not work as intended IF user creates 21 invoices, and then tries to check the status of invoice #0, it just wont be updated
const updatedUserInvoice = userInvoices.filter((filteredInvoice: LightningTransaction) =>
typeof invoice === 'object'
? filteredInvoice.payment_request === invoice.payment_request
: filteredInvoice.payment_request === invoice,
)[0];
if (typeof updatedUserInvoice !== 'undefined') {
setInvoiceStatusChanged(true);
setParams({ invoice: updatedUserInvoice });
setIsLoading(false);
if (updatedUserInvoice.ispaid) {
// we fetched the invoice, and it is paid :-)
setIsFetchingInvoices(false);
if (!wallet) {
return;
}
setSelectedWalletID(walletID);
console.log('LNDViewInvoice - useEffect');
if (!invoice.ispaid) {
fetchInvoiceInterval.current = setInterval(async () => {
if (isFetchingInvoices) {
try {
// @ts-ignore - getUserInvoices is not set on TWallet
const userInvoices = await wallet.getUserInvoices(20);
// fetching only last 20 invoices
// for invoice that was created just now - that should be enough (it is basically the last one, so limit=1 would be sufficient)
// but that might not work as intended IF user creates 21 invoices, and then tries to check the status of invoice #0, it just wont be updated
const updatedUserInvoice = userInvoices.filter((filteredInvoice: LightningTransaction) =>
typeof invoice === 'object'
? filteredInvoice.payment_request === invoice.payment_request
: filteredInvoice.payment_request === invoice,
)[0];
if (typeof updatedUserInvoice !== 'undefined') {
setInvoiceStatusChanged(true);
setParams({ invoice: updatedUserInvoice });
setIsLoading(false);
if (updatedUserInvoice.ispaid) {
// we fetched the invoice, and it is paid :-)
setIsFetchingInvoices(false);
fetchAndSaveWalletTransactions(walletID);
} else {
const currentDate = new Date();
const now = (currentDate.getTime() / 1000) | 0; // eslint-disable-line no-bitwise
const invoiceExpiration = updatedUserInvoice.timestamp + updatedUserInvoice.expire_time;
if (invoiceExpiration < now && !updatedUserInvoice.ispaid) {
// invoice expired :-(
fetchAndSaveWalletTransactions(walletID);
} else {
const currentDate = new Date();
const now = (currentDate.getTime() / 1000) | 0; // eslint-disable-line no-bitwise
const invoiceExpiration = updatedUserInvoice.timestamp + updatedUserInvoice.expire_time;
if (invoiceExpiration < now && !updatedUserInvoice.ispaid) {
// invoice expired :-(
fetchAndSaveWalletTransactions(walletID);
setIsFetchingInvoices(false);
triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
clearInterval(fetchInvoiceInterval.current);
fetchInvoiceInterval.current = undefined;
}
setIsFetchingInvoices(false);
triggerHapticFeedback(HapticFeedbackTypes.NotificationError);
clearInterval(fetchInvoiceInterval.current);
fetchInvoiceInterval.current = undefined;
}
}
} catch (error) {
console.log(error);
}
} catch (error) {
console.log(error);
}
}, 3000);
} else {
setIsFetchingInvoices(false);
clearInterval(fetchInvoiceInterval.current);
fetchInvoiceInterval.current = undefined;
}
}
}, 3000);
} else {
setIsFetchingInvoices(false);
clearInterval(fetchInvoiceInterval.current);
fetchInvoiceInterval.current = undefined;
}
// eslint-disable-next-line react-hooks/exhaustive-deps
@ -207,8 +208,8 @@ const LNDViewInvoice = () => {
const invoiceExpiration = invoice?.timestamp && invoice?.expire_time ? invoice.timestamp + invoice.expire_time : undefined;
if (invoice.ispaid || invoice.type === 'paid_invoice') {
let amount = 0;
let transactionDate;
let description;
let invoiceDate;
if (invoice.type === 'paid_invoice' && invoice?.value) {
amount = invoice.value;
} else if (invoice.type === 'user_invoice' && invoice.amt) {
@ -220,7 +221,7 @@ const LNDViewInvoice = () => {
description = invoice.memo;
}
if (invoice.timestamp) {
transactionDate = new Date(invoice.timestamp * 1000).toLocaleString();
invoiceDate = dayjs(invoice.timestamp * (String(invoice.timestamp).length === 10 ? 1000 : 1)).format('LLL');
}
return (
<View style={styles.root}>
@ -232,11 +233,9 @@ const LNDViewInvoice = () => {
shouldAnimate={false}
/>
<View style={styles.detailsRoot}>
{transactionDate && (
<Text style={[styles.detailsText, stylesHook.detailsText]}>
{loc.lndViewInvoice.date_time}: {transactionDate}
</Text>
)}
<Text style={[styles.detailsText, stylesHook.detailsText]}>
{loc.lndViewInvoice.date_time}: {invoiceDate}
</Text>
{invoice.payment_preimage && typeof invoice.payment_preimage === 'string' ? (
<TouchableOpacity accessibilityRole="button" style={styles.detailsTouch} onPress={navigateToPreImageScreen}>
<Text style={[styles.detailsText, stylesHook.detailsText]}>{loc.send.create_details}</Text>
@ -263,33 +262,34 @@ const LNDViewInvoice = () => {
);
}
// Invoice has not expired, nor has it been paid for.
return (
<ScrollView>
<View style={[styles.activeRoot, stylesHook.root]}>
<View style={styles.activeQrcode}>
<QRCodeComponent value={invoice.payment_request ?? 'not found'} size={qrCodeSize} />
</View>
<BlueSpacing20 />
<BlueText>
{loc.lndViewInvoice.please_pay} {invoice.amt} {loc.lndViewInvoice.sats}
</BlueText>
{'description' in invoice && (invoice.description?.length ?? 0) > 0 && (
if (invoice.payment_request) {
return (
<ScrollView>
<View style={[styles.activeRoot, stylesHook.root]}>
<View style={styles.activeQrcode}>
<QRCodeComponent value={invoice.payment_request} size={qrCodeSize} />
</View>
<BlueSpacing20 />
<BlueText>
{loc.lndViewInvoice.for} {invoice.description ?? ''}
{loc.lndViewInvoice.please_pay} {invoice.amt} {loc.lndViewInvoice.sats}
</BlueText>
)}
<CopyTextToClipboard truncated text={invoice.payment_request ?? 'not found'} />
<Button onPress={handleOnSharePressed} title={loc.receive.details_share} />
<BlueSpacing20 />
<Button
style={stylesHook.additionalInfo}
onPress={handleOnViewAdditionalInformationPressed}
title={loc.lndViewInvoice.additional_info}
/>
</View>
</ScrollView>
);
{'description' in invoice && (invoice.description?.length ?? 0) > 0 && (
<BlueText>
{loc.lndViewInvoice.for} {invoice.description ?? ''}
</BlueText>
)}
<CopyTextToClipboard truncated text={invoice.payment_request} />
<Button onPress={handleOnSharePressed} title={loc.receive.details_share} />
<BlueSpacing20 />
<Button
style={stylesHook.additionalInfo}
onPress={handleOnViewAdditionalInformationPressed}
title={loc.lndViewInvoice.additional_info}
/>
</View>
</ScrollView>
);
}
}
};