|
| 1 | +import { EPaymentType } from 'beignet'; |
| 2 | +import React, { ReactElement, useCallback, useEffect } from 'react'; |
| 3 | +import { useTranslation } from 'react-i18next'; |
| 4 | +import { Image, StyleSheet, View } from 'react-native'; |
| 5 | + |
| 6 | +import { ActivityIndicator } from '../../components/ActivityIndicator'; |
| 7 | +import AmountToggle from '../../components/AmountToggle'; |
| 8 | +import BottomSheetNavigationHeader from '../../components/BottomSheetNavigationHeader'; |
| 9 | +import GradientView from '../../components/GradientView'; |
| 10 | +import SafeAreaInset from '../../components/SafeAreaInset'; |
| 11 | +import { useLightningMaxInboundCapacity } from '../../hooks/lightning'; |
| 12 | +import { GiftScreenProps } from '../../navigation/types'; |
| 13 | +import { useSheetRef } from '../../sheets/SheetRefsProvider'; |
| 14 | +import { dispatch } from '../../store/helpers'; |
| 15 | +import { addActivityItem } from '../../store/slices/activity'; |
| 16 | +import { updateSettings } from '../../store/slices/settings'; |
| 17 | +import { |
| 18 | + EActivityType, |
| 19 | + TLightningActivityItem, |
| 20 | +} from '../../store/types/activity'; |
| 21 | +import { createLightningInvoice } from '../../store/utils/lightning'; |
| 22 | +import { showSheet } from '../../store/utils/ui'; |
| 23 | +import { BodyM } from '../../styles/text'; |
| 24 | +import { giftOrder, giftPay, openChannel } from '../../utils/blocktank'; |
| 25 | +import { vibrate } from '../../utils/helpers'; |
| 26 | + |
| 27 | +const imageSrc = require('../../assets/illustrations/gift.png'); |
| 28 | + |
| 29 | +const Loading = ({ |
| 30 | + navigation, |
| 31 | + route, |
| 32 | +}: GiftScreenProps<'Loading'>): ReactElement => { |
| 33 | + const { code, amount } = route.params; |
| 34 | + const { t } = useTranslation('other'); |
| 35 | + const sheetRef = useSheetRef('gift'); |
| 36 | + const maxInboundCapacity = useLightningMaxInboundCapacity(); |
| 37 | + |
| 38 | + // biome-ignore lint/correctness/useExhaustiveDependencies: on mount |
| 39 | + const getGift = useCallback(async (): Promise<void> => { |
| 40 | + const getWithoutLiquidity = async (): Promise<void> => { |
| 41 | + const orderResult = await giftOrder(code); |
| 42 | + |
| 43 | + if (orderResult.isErr()) { |
| 44 | + if (orderResult.error.message.includes('GIFT_CODE_ALREADY_USED')) { |
| 45 | + navigation.navigate('Used', { amount }); |
| 46 | + } else { |
| 47 | + navigation.navigate('Error'); |
| 48 | + } |
| 49 | + |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + const { orderId } = orderResult.value; |
| 54 | + |
| 55 | + if (!orderId) { |
| 56 | + navigation.navigate('Error'); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + const openResult = await openChannel(orderId); |
| 61 | + |
| 62 | + if (openResult.isErr()) { |
| 63 | + navigation.navigate('Error'); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + const order = openResult.value; |
| 68 | + |
| 69 | + const activityItem: TLightningActivityItem = { |
| 70 | + id: order.channel?.fundingTx.id ?? '', |
| 71 | + activityType: EActivityType.lightning, |
| 72 | + txType: EPaymentType.received, |
| 73 | + status: 'successful', |
| 74 | + message: code, |
| 75 | + address: '', |
| 76 | + value: order.clientBalanceSat, |
| 77 | + confirmed: true, |
| 78 | + timestamp: new Date().getTime(), |
| 79 | + }; |
| 80 | + |
| 81 | + dispatch(addActivityItem(activityItem)); |
| 82 | + dispatch(updateSettings({ hideOnboardingMessage: true })); |
| 83 | + vibrate({ type: 'default' }); |
| 84 | + sheetRef.current?.close(); |
| 85 | + showSheet('receivedTx', { |
| 86 | + id: activityItem.id, |
| 87 | + activityType: EActivityType.lightning, |
| 88 | + value: activityItem.value, |
| 89 | + }); |
| 90 | + }; |
| 91 | + |
| 92 | + const getWithLiquidity = async (): Promise<void> => { |
| 93 | + const invoiceResult = await createLightningInvoice({ |
| 94 | + amountSats: 0, |
| 95 | + description: `blocktank-gift-code:${code}`, |
| 96 | + expiryDeltaSeconds: 3600, |
| 97 | + }); |
| 98 | + |
| 99 | + if (invoiceResult.isErr()) { |
| 100 | + navigation.navigate('Error'); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + const invoice = invoiceResult.value.to_str; |
| 105 | + const result = await giftPay(invoice); |
| 106 | + |
| 107 | + if (result.isErr()) { |
| 108 | + if (result.error.message.includes('GIFT_CODE_ALREADY_USED')) { |
| 109 | + navigation.navigate('Used', { amount }); |
| 110 | + } else { |
| 111 | + navigation.navigate('Error'); |
| 112 | + } |
| 113 | + |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + sheetRef.current?.close(); |
| 118 | + }; |
| 119 | + |
| 120 | + if (maxInboundCapacity >= amount) { |
| 121 | + await getWithLiquidity(); |
| 122 | + } else { |
| 123 | + await getWithoutLiquidity(); |
| 124 | + } |
| 125 | + }, []); |
| 126 | + |
| 127 | + useEffect(() => { |
| 128 | + getGift(); |
| 129 | + }, [getGift]); |
| 130 | + |
| 131 | + return ( |
| 132 | + <GradientView style={styles.root}> |
| 133 | + <BottomSheetNavigationHeader title={t('gift.claiming.title')} /> |
| 134 | + |
| 135 | + <View style={styles.content}> |
| 136 | + <AmountToggle amount={amount} /> |
| 137 | + |
| 138 | + <BodyM style={styles.text} color="secondary"> |
| 139 | + {t('gift.claiming.text')} |
| 140 | + </BodyM> |
| 141 | + |
| 142 | + <View style={styles.imageContainer}> |
| 143 | + <Image style={styles.image} source={imageSrc} /> |
| 144 | + </View> |
| 145 | + |
| 146 | + <View style={styles.footer}> |
| 147 | + <ActivityIndicator size={32} /> |
| 148 | + </View> |
| 149 | + </View> |
| 150 | + <SafeAreaInset type="bottom" minPadding={16} /> |
| 151 | + </GradientView> |
| 152 | + ); |
| 153 | +}; |
| 154 | + |
| 155 | +const styles = StyleSheet.create({ |
| 156 | + root: { |
| 157 | + flex: 1, |
| 158 | + }, |
| 159 | + content: { |
| 160 | + flex: 1, |
| 161 | + paddingHorizontal: 16, |
| 162 | + }, |
| 163 | + text: { |
| 164 | + marginTop: 32, |
| 165 | + }, |
| 166 | + imageContainer: { |
| 167 | + flexShrink: 1, |
| 168 | + justifyContent: 'center', |
| 169 | + alignItems: 'center', |
| 170 | + alignSelf: 'center', |
| 171 | + width: 256, |
| 172 | + aspectRatio: 1, |
| 173 | + marginTop: 'auto', |
| 174 | + }, |
| 175 | + image: { |
| 176 | + flex: 1, |
| 177 | + resizeMode: 'contain', |
| 178 | + }, |
| 179 | + footer: { |
| 180 | + marginTop: 'auto', |
| 181 | + marginBottom: 16, |
| 182 | + justifyContent: 'center', |
| 183 | + alignItems: 'center', |
| 184 | + }, |
| 185 | +}); |
| 186 | + |
| 187 | +export default Loading; |
0 commit comments