From ec904e6b4c9f75b48764922fc117dd6fbe3ad96f Mon Sep 17 00:00:00 2001 From: ekzyis Date: Thu, 5 Dec 2024 15:52:32 +0100 Subject: [PATCH] Move useInvoice and useQrPayment into own files (#1686) --- components/item-info.js | 2 +- components/poll.js | 2 +- components/{payment.js => use-invoice.js} | 52 ++--------------------- components/use-paid-mutation.js | 3 +- components/use-qr-payment.js | 47 ++++++++++++++++++++ wallets/payment.js | 2 +- 6 files changed, 56 insertions(+), 52 deletions(-) rename components/{payment.js => use-invoice.js} (55%) create mode 100644 components/use-qr-payment.js diff --git a/components/item-info.js b/components/item-info.js index b0dbafaf..63123860 100644 --- a/components/item-info.js +++ b/components/item-info.js @@ -22,7 +22,7 @@ import { DropdownItemUpVote } from './upvote' import { useRoot } from './root' import { MuteSubDropdownItem, PinSubDropdownItem } from './territory-header' import UserPopover from './user-popover' -import { useQrPayment } from './payment' +import useQrPayment from './use-qr-payment' import { useRetryCreateItem } from './use-item-submit' import { useToast } from './toast' import { useShowModal } from './modal' diff --git a/components/poll.js b/components/poll.js index dc694f80..34276047 100644 --- a/components/poll.js +++ b/components/poll.js @@ -5,7 +5,7 @@ import { useMe } from './me' import styles from './poll.module.css' import { signIn } from 'next-auth/react' import ActionTooltip from './action-tooltip' -import { useQrPayment } from './payment' +import useQrPayment from './use-qr-payment' import { useToast } from './toast' import { usePaidMutation } from './use-paid-mutation' import { POLL_VOTE, RETRY_PAID_ACTION } from '@/fragments/paidAction' diff --git a/components/payment.js b/components/use-invoice.js similarity index 55% rename from components/payment.js rename to components/use-invoice.js index f7e19af6..977aa2d7 100644 --- a/components/payment.js +++ b/components/use-invoice.js @@ -1,12 +1,10 @@ -import { useCallback } from 'react' import { useApolloClient, useMutation } from '@apollo/client' -import { CANCEL_INVOICE, INVOICE } from '@/fragments/wallet' -import Invoice from '@/components/invoice' -import { useShowModal } from './modal' -import { InvoiceCanceledError, InvoiceExpiredError } from '@/wallets/errors' +import { useCallback } from 'react' import { RETRY_PAID_ACTION } from '@/fragments/paidAction' +import { INVOICE, CANCEL_INVOICE } from '@/fragments/wallet' +import { InvoiceExpiredError, InvoiceCanceledError } from '@/wallets/errors' -export const useInvoice = () => { +export default function useInvoice () { const client = useApolloClient() const [retryPaidAction] = useMutation(RETRY_PAID_ACTION) @@ -60,45 +58,3 @@ export const useInvoice = () => { return { cancel, retry, isInvoice } } - -export const useQrPayment = () => { - const invoice = useInvoice() - const showModal = useShowModal() - - const waitForQrPayment = useCallback(async (inv, walletError, - { - keepOpen = true, - cancelOnClose = true, - persistOnNavigate = false, - waitFor = inv => inv?.satsReceived > 0 - } = {} - ) => { - return await new Promise((resolve, reject) => { - let paid - const cancelAndReject = async (onClose) => { - if (!paid && cancelOnClose) { - const updatedInv = await invoice.cancel(inv).catch(console.error) - reject(new InvoiceCanceledError(updatedInv)) - } - resolve(inv) - } - showModal(onClose => - reject(new InvoiceExpiredError(inv))} - onCanceled={inv => { onClose(); reject(new InvoiceCanceledError(inv, inv?.actionError)) }} - onPayment={(inv) => { paid = true; onClose(); resolve(inv) }} - poll - />, - { keepOpen, persistOnNavigate, onClose: cancelAndReject }) - }) - }, [invoice]) - - return waitForQrPayment -} diff --git a/components/use-paid-mutation.js b/components/use-paid-mutation.js index d3b1bf7f..f093ff45 100644 --- a/components/use-paid-mutation.js +++ b/components/use-paid-mutation.js @@ -1,6 +1,7 @@ import { useApolloClient, useLazyQuery, useMutation } from '@apollo/client' import { useCallback, useState } from 'react' -import { useInvoice, useQrPayment } from './payment' +import useQrPayment from '@/components/use-qr-payment' +import useInvoice from '@/components/use-invoice' import { InvoiceCanceledError, InvoiceExpiredError, WalletError, WalletPaymentError } from '@/wallets/errors' import { GET_PAID_ACTION } from '@/fragments/paidAction' import { useWalletPayment } from '@/wallets/payment' diff --git a/components/use-qr-payment.js b/components/use-qr-payment.js new file mode 100644 index 00000000..752414d9 --- /dev/null +++ b/components/use-qr-payment.js @@ -0,0 +1,47 @@ +import { useCallback } from 'react' +import Invoice from '@/components/invoice' +import { InvoiceCanceledError, InvoiceExpiredError } from '@/wallets/errors' +import { useShowModal } from '@/components/modal' +import useInvoice from '@/components/use-invoice' + +export default function useQrPayment () { + const invoice = useInvoice() + const showModal = useShowModal() + + const waitForQrPayment = useCallback(async (inv, walletError, + { + keepOpen = true, + cancelOnClose = true, + persistOnNavigate = false, + waitFor = inv => inv?.satsReceived > 0 + } = {} + ) => { + return await new Promise((resolve, reject) => { + let paid + const cancelAndReject = async (onClose) => { + if (!paid && cancelOnClose) { + const updatedInv = await invoice.cancel(inv).catch(console.error) + reject(new InvoiceCanceledError(updatedInv)) + } + resolve(inv) + } + showModal(onClose => + reject(new InvoiceExpiredError(inv))} + onCanceled={inv => { onClose(); reject(new InvoiceCanceledError(inv, inv?.actionError)) }} + onPayment={(inv) => { paid = true; onClose(); resolve(inv) }} + poll + />, + { keepOpen, persistOnNavigate, onClose: cancelAndReject }) + }) + }, [invoice]) + + return waitForQrPayment +} diff --git a/wallets/payment.js b/wallets/payment.js index 1ed290b3..157e06ea 100644 --- a/wallets/payment.js +++ b/wallets/payment.js @@ -1,7 +1,7 @@ import { useCallback } from 'react' import { useSendWallets } from '@/wallets' import { formatSats } from '@/lib/format' -import { useInvoice } from '@/components/payment' +import useInvoice from '@/components/use-invoice' import { FAST_POLL_INTERVAL } from '@/lib/constants' import { WalletsNotAvailableError, WalletSenderError, WalletAggregateError, WalletPaymentAggregateError,