Skip to content

Commit

Permalink
refactor: create liquid address on button click (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
secondl1ght authored Sep 19, 2024
1 parent b8aba42 commit 87cad11
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/views/wallet/Receive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ArrowLeft, ArrowUpDown, ChevronsUpDown } from 'lucide-react';
import Link from 'next/link';
import { useTranslations } from 'next-intl';
import { useQRCode } from 'next-qrcode';
import { useEffect, useMemo, useState } from 'react';
import { useMemo, useState } from 'react';
import { useLocalStorage } from 'usehooks-ts';

import { Button } from '@/components/ui/button-v2';
Expand Down Expand Up @@ -113,6 +113,8 @@ export const Receive = () => {
{ data: liquidData, loading: liquidLoading, error: liquidError },
] = useCreateOnchainAddressMutation({
variables: { input: { wallet_account_id: liquidAccountId } },
onCompleted: data =>
setReceiveString(data.wallets.create_onchain_address.address),
onError: err => {
const messages = handleApolloError(err);

Expand All @@ -124,21 +126,15 @@ export const Receive = () => {
},
});

useEffect(() => {
if (!liquidAccountId || liquidData) return;

createLiquidAddress();
}, [liquidAccountId, liquidData, createLiquidAddress]);

const liquidAddress = useMemo(() => {
const liquidAddressFormatted = useMemo(() => {
if (!liquidData?.wallets.create_onchain_address.address) {
return { address: '', formatted: '' };
return '';
}

const address = liquidData.wallets.create_onchain_address.address;
const formatted = (address.match(/.{1,6}/g) || []).join(' - ');

return { address, formatted };
return formatted;
}, [liquidData]);

const [createLightningInvoice, { loading: invoiceLoading }] =
Expand Down Expand Up @@ -181,9 +177,9 @@ export const Receive = () => {
: t('Wallet.amount-ln');
case 'Liquid Bitcoin':
case 'Tether USD':
return liquidAddress.formatted;
return liquidAddressFormatted;
}
}, [receive, bancoCode, receiveString, liquidAddress, t]);
}, [receive, bancoCode, receiveString, liquidAddressFormatted, t]);

const {
data: priceData,
Expand Down Expand Up @@ -263,7 +259,7 @@ export const Receive = () => {
break;
case 'Liquid Bitcoin':
case 'Tether USD':
setReceiveString(liquidAddress.address);
createLiquidAddress();
break;
}
}}
Expand Down

0 comments on commit 87cad11

Please sign in to comment.