import { useToast } from '@/hooks/useToast'; import { useZaps } from '@/hooks/useZaps'; import type { WebLNProvider } from 'webln'; import type { ZapTarget } from '@/components/ZapButton'; import QRCode from 'qrcode'; import { Zap, Copy } from 'lucide-react'; import React, { useState, useEffect, useRef } from '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 { Textarea } from '@/components/ui/textarea'; import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group'; interface ZapModalProps { open: boolean; onOpenChange: (open: boolean) => void; target: ZapTarget; webln: WebLNProvider | null; } const presetAmounts = [1, 50, 100, 250, 1000]; export function ZapModal({ open, onOpenChange, target, webln }: ZapModalProps) { const { toast } = useToast(); const { zap, isZapping, invoice, setInvoice } = useZaps(target, webln, () => onOpenChange(false)); const [amount, setAmount] = useState(100); const [comment, setComment] = useState(''); const inputRef = useRef(null); const qrCodeRef = useRef(null); useEffect(() => { if (target) { setComment('Zapped with MKStack!'); } }, [target]); useEffect(() => { if (invoice && qrCodeRef.current) { QRCode.toCanvas(qrCodeRef.current, invoice, { width: 256 }); } }, [invoice]); const handleCopy = () => { if (invoice) { navigator.clipboard.writeText(invoice); toast({ title: 'Copied to clipboard!', }); } }; useEffect(() => { if (open) { setAmount(100); setInvoice(null); } }, [open, setInvoice]); const handleZap = () => { const finalAmount = typeof amount === 'string' ? parseInt(amount, 10) : amount; zap(finalAmount, comment); }; return ( {invoice ? 'Manual Zap' : 'Send a Zap'} {invoice ? (
Scan the QR code with a lightning-enabled wallet or copy the invoice below.
) : ( <>
Zaps are small Bitcoin payments that support the creator of this item.
If you enjoyed this, consider sending a zap!
)}
{invoice ? (
) : ( <>
{ if (value) { setAmount(parseInt(value, 10)); } }} className="grid grid-cols-5 gap-2" > {presetAmounts.map((presetAmount) => ( {presetAmount} ))}
OR
setAmount(e.target.value)} />