import { useState } from 'react'; import { Loader2 } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { satsToMsat } from '../format'; import { CopyField } from '../CopyField'; import { useWalletOperations } from '../useWalletData'; // Create a BOLT11 invoice. Receiving needs no key material, so this works while the wallet is locked. // // The amount is entered in sats and converted with satsToMsat, which appends three zeros to the digit // string rather than multiplying — the request field is a msat STRING and must stay one. type CreateInvoiceDialogProps = { open: boolean; onOpenChange: (open: boolean) => void; walletId: number }; export const CreateInvoiceDialog = ({ open, onOpenChange, walletId }: CreateInvoiceDialogProps) => { const { createInvoice } = useWalletOperations(walletId); const [amountSats, setAmountSats] = useState(''); const [memo, setMemo] = useState(''); const [expiryMinutes, setExpiryMinutes] = useState('60'); const [created, setCreated] = useState(null); const close = () => { setAmountSats(''); setMemo(''); setExpiryMinutes('60'); setCreated(null); onOpenChange(false); }; const submit = async (ev: React.FormEvent) => { ev.preventDefault(); const sats = Number(amountSats); const result = await createInvoice.mutateAsync({ // Omitted entirely for a zero-amount invoice, which is a legitimate "payer decides" request. amountMsat: amountSats && Number.isFinite(sats) && sats > 0 ? satsToMsat(sats) : undefined, memo: memo.trim() || undefined, expirySeconds: Number(expiryMinutes) > 0 ? Number(expiryMinutes) * 60 : undefined, }); setCreated(result.invoice.bolt11); }; return ( (next ? onOpenChange(true) : close())}> {created ? ( <> Invoice created Send this to whoever is paying you. ) : (
New invoice Leave the amount blank to let the payer choose.
setAmountSats(ev.target.value)} />
setMemo(ev.target.value)} />
setExpiryMinutes(ev.target.value)} className="w-32" />
)}
); };